我正在使用 Spring Integration,并且有以下场景。今天,当实体被保存、更新或删除时,我让我的 Dao 向发布-订阅频道发送一个事件。它基本上是这样实现的:
事件网关接口:
public interface DaoEventGateway {
@Gateway
void sendEvent(EntityEvent event);
}
一个道:
public class ADao {
private DaoEventGateway gateway;
public void save(A aEntity) {
... do some stuff to save it.
fireEvent(aEntity, EntityType.SAVE);
}
protected void fireEvent(A entity, EventType eventType) {
if (eventGateway != null) {
EntityEvent event =
new EntityEvent(entity, eventType);
eventGateway.sendEvent(event);
}
}
}
事件的某种监听器:
public class AEventLoggingService {
public void receiveEvent(Event event) {
A event = event.getEntity();
Long id = event.getId();
... look up some associations based on the id ...
... log something about the event ...
}
}
配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd">
<int:publish-subscribe-channel id="aEventChannel" />
<int:gateway id="aEventGateway" service-interface="com.factorlab.persistence.DaoEventGateway"
default-request-channel="aEventChannel">
<int:method name="sendEvent" request-channel="aEventChannel" />
</int:gateway>
<bean id="aDao" class="com.factorlab.ADao">
<property name="eventGateway" ref="aEventGateway" />
</bean>
<int:service-activator input-channel="aEventChannel"
ref="aEventLoggingService" method="receiveEvent" />
</beans>
现在一切正常,除了性能,因为我认为所有订阅者都在与发布者相同的线程(和相同的事务)中操作。
我想将侦听器完成的工作与 DAO 中完成的工作分离,并使其异步。但是,侦听器可以依赖于他们收到关于已经在数据库中的通知的东西。所以,我不希望在事务提交之前发送(或至少不接收)消息。
对此有标准方法吗?人们对如何实现这一目标有什么建议?