原题目:Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程十九(Spring中国教育管理中心)

Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程十九
7.5.应用@TransactionalEventListener
应用事务时,可能须要注册一个侦听器,以便在事务提交之前或之后或产生回滚之后执行某些操作。

Spring Data for Apache Geode 使创立侦听器变得容易,这些侦听器将在具备@
TransactionalEventListener正文的事务的特定阶段被调用 。带正文的办法@TransactionalEventListener(如下所示)将在指定的被告诉从事务办法公布的事件的,phase。

事务提交后事件侦听器

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
public void handleAfterCommit(MyEvent event) {

// do something after transaction is committed

}
为了调用上述办法,您必须从事务中公布一个事件,如下所示:

公布交易事件

@Service
class MyTransactionalService {

@Autowired
private final ApplicationEventPublisher applicationEventPublisher;

@Transactional
public <Return-Type> someTransactionalServiceMethod() {

// Perform business logic interacting with and accessing multiple transactional resources atomically, then...applicationEventPublisher.publishEvent(new MyApplicationEvent(...));

}

...
}
该@
TransactionalEventListener注解容许你指定交易phase在事件处理办法将被调用。选项包含:AFTER_COMMIT,AFTER_COMPLETION,AFTER_ROLLBACK,和BEFORE_COMMIT。如果未指定,则phase默认为AFTER_COMMIT。如果您心愿即便不存在事务也能调用侦听器,您能够设置fallbackExecution为true。

7.6.主动交易事件公布
从 Spring Data for Apache Geode 开始Neumann/2.3,当初能够启用主动事务事件公布。

应用@
EnableGemfireCacheTransactions正文,将enableAutoTransactionEventPublishing属性设置为true。默认值为false。

启用主动交易事件公布

@EnableGemfireCacheTransactions(enableAutoTransactionEventPublishing = true)
class GeodeConfiguration { ... }
而后,您能够创立带@
TransactionalEventListener正文的 POJO 办法来处理事务阶段AFTER_COMMIT或AFTER_ROLLBACK事务阶段的事务事件。

@Component
class TransactionEventListeners {

@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)public void handleAfterCommit(TransactionApplicationEvent event) {    ...}@TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)public void handleAfterRollback(TransactionApplicationEvent event) {    ...}

}
仅反对
TransactionPhase.AFTER_COMMIT和TransactionPhase.AFTER_ROLLBACK。 TransactionPhase.BEFORE_COMMIT不反对,因为 1) SDG 适配了 Apache GeodeTransactionListener 和TransactionWriter接口来实现主动事务事件公布,以及 2) 当 Apache Geode TransactionWriter.beforeCommit(:TransactionEvent)被调用时,它曾经在AbstractPlatformTransactionManager.triggerBeforeCommit(:TransactionStatus)调用之后, @TranactionalEventListener 在事务生命周期中调用带正文的 POJO 办法。

应用主动事务事件公布,您无需
applicationEventPublisher.publishEvent(..)在应用程序@Transactional @Service办法中显式调用该 办法。

然而,如果您依然心愿在“提交之前”接管事务事件,那么您依然必须
applicationEventPublisher.publishEvent(..)在您的应用程序@Transactional @Service办法中调用该 办法。

7.7.间断查问 (CQ)
Apache Geode 提供的一项弱小性能是 间断查问(或 CQ)。

简而言之,CQ 容许开发人员创立和注册 OQL 查问,而后在增加到 Apache Geode 的新数据与查问谓词匹配时主动收到告诉。Spring Data for Apache Geode 通过
org.springframework.data.gemfire.listener包及其侦听器容器为 CQ 提供专门的反对;在性能和命名上与Spring Framework 中的 JMS 集成十分类似;事实上,相熟 Spring 中 JMS 反对的用户应该会有宾至如归的感觉。

基本上,Apache Geode 的 Spring Data 容许 POJO 上的办法成为 CQ 的端点。只需定义查问并批示应调用的办法,以便在匹配时收到告诉。Apache Geode 的 Spring Data 负责其余的工作。这与 Java EE 的音讯驱动 bean 格调十分类似,但对基类或接口实现没有任何要求,基于 Apache Geode。

目前,仅在 Apache Geode 的客户端/服务器拓扑中反对间断查问。此外,应用的客户端池须要启用订阅。 无关更多信息,请参阅 Apache Geode 文档。

7.7.1.间断查问侦听器容器
Spring Data for Apache Geode 通过应用 SDG 来解决 CQ 四周的基础设施,简化了 CQ 事件的创立、注册、生命周期和分派,
SDGContinuousQueryListenerContainer代表用户实现了所有沉重的工作。相熟 EJB 和 JMS 的用户应该会发现相熟的概念,因为它的设计尽可能靠近Spring Framework及其音讯驱动的 POJO (MDP) 中提供的反对。

SDGContinuousQueryListenerContainer充当事件(或音讯)侦听器容器;它用于从注册的 CQ 接管事件并调用注入其中的 POJO。侦听器容器负责音讯接管的所有线程并分派到侦听器中进行解决。它充当 EDP(事件驱动的 POJO)和事件提供者之间的中介,负责 CQ 的创立和注册(接管事件)、资源获取和开释、异样转换等。这容许您作为应用程序开发人员编写与接管事件(并对其做出反馈)相干的(可能很简单)业务逻辑,并将样板 Apache Geode 基础设施问题委托给框架。

侦听器容器是齐全可定制的。开发人员能够抉择应用 CQ 线程来执行分派(同步交付)或通过定义适合的
java.util.concurrent.Executor(或 Spring 的TaskExecutor)的异步办法的新线程(来自现有池 )。依据负载、侦听器的数量或运行时环境,开发人员应该更改或调整执行器以更好地满足她的需要。特地是在托管环境(例如应用服务器)中,强烈建议抉择一个适合的TaskExecutor 来利用其运行时。

7.7.2.在ContinuousQueryListener与ContinuousQueryListenerAdapter

ContinuousQueryListenerAdapter班是Spring数据为Apache的Geode CQ反对的最初一个组件。简而言之,类容许您将简直所有实现类公开为具备起码束缚的 EDP。
ContinuousQueryListenerAdapter实现ContinuousQueryListener接口,一个简略的监听器接口,相似于 Apache Geode 的CqListener。

思考以下接口定义。留神各种事件处理办法及其参数:

public interface EventDelegate {

 void handleEvent(CqEvent event); void handleEvent(Operation baseOp); void handleEvent(Object key); void handleEvent(Object key, Object newValue); void handleEvent(Throwable throwable); void handleQuery(CqQuery cq); void handleEvent(CqEvent event, Operation baseOp, byte[] deltaValue); void handleEvent(CqEvent event, Operation baseOp, Operation queryOp, Object key, Object newValue);

}
package example;

class DefaultEventDelegate implements EventDelegate {

// implementation elided for clarity...

}
特地要留神EventDelegate接口的上述实现齐全没有Apache Geode 依赖项。它的确是一个 POJO,咱们能够并且将通过以下配置将其制成 EDP。

该类不用实现接口;一个接口只是用来更好地展现合约和实现之间的解耦。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

   xmlns:gfe="https://www.springframework.org/schema/geode"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsdhttps://www.springframework.org/schema/geode https://www.springframework.org/schema/geode/spring-geode.xsd

">

<gfe:client-cache/><gfe:pool subscription-enabled="true">   <gfe:server host="localhost" port="40404"/></gfe:pool><gfe:cq-listener-container>   <!-- default handle method -->   <gfe:listener ref="listener" query="SELECT * FROM /SomeRegion"/>   <gfe:listener ref="another-listener" query="SELECT * FROM /AnotherRegion" name="myQuery" method="handleQuery"/></gfe:cq-listener-container><bean id="listener" class="example.DefaultMessageDelegate"/><bean id="another-listener" class="example.DefaultMessageDelegate"/>

...
<beans>
Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程十九
下面的例子展现了听众能够领有的几种不同的模式;至多,须要侦听器援用和理论查问定义。然而,能够为生成的间断查问指定一个名称(用于监督)以及办法的名称(默认为handleEvent)。指定的办法能够有各种参数类型,EventDelegate接口列出了容许的类型。

下面的示例应用 Spring Data for Apache Geode 命名空间来申明事件侦听器容器并主动注册侦听器。残缺的bean定义如下所示:

<!-- this is the Event Driven POJO (MDP) -->
<bean id="eventListener" class="org.springframework.data.gemfire.listener.adapter.ContinuousQueryListenerAdapter">

<constructor-arg>    <bean class="gemfireexample.DefaultEventDelegate"/></constructor-arg>

</bean>

<!-- and this is the event listener container... -->
<bean id="gemfireListenerContainer" class="org.springframework.data.gemfire.listener.ContinuousQueryListenerContainer">

<property name="cache" ref="gemfireCache"/><property name="queryListeners">  <!-- set of CQ listeners -->  <set>    <bean class="org.springframework.data.gemfire.listener.ContinuousQueryDefinition" >           <constructor-arg value="SELECT * FROM /SomeRegion" />           <constructor-arg ref="eventListener"/>    </bean>  </set></property>

</bean>
Spring认证中国教育管理中心-Apache Geode 的 Spring 数据教程十九
每次接管到事件时,适配器都会主动在 Apache Geode 事件和所需的办法参数之间通明地执行类型转换。任何由办法调用引起的异样都会被容器捕捉并解决(默认状况下,被记录)。