Transactions 不使用';在使用Spring集成提交事务之前,无法触发
我正在使用Spring集成,我有以下场景。今天,当实体被保存、更新或删除时,我让Dao将事件发送到发布-订阅通道。基本上是这样实现的: 事件网关接口:Transactions 不使用';在使用Spring集成提交事务之前,无法触发,transactions,spring-integration,Transactions,Spring Integration,我正在使用Spring集成,我有以下场景。今天,当实体被保存、更新或删除时,我让Dao将事件发送到发布-订阅通道。基本上是这样实现的: 事件网关接口: public interface DaoEventGateway { @Gateway void sendEvent(EntityEvent event); } A刀: public class ADao { private DaoEventGateway gateway; public void save(A aEntit
public interface DaoEventGateway {
@Gateway
void sendEvent(EntityEvent event);
}
A刀:
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中所做的工作解耦,并使其异步化。然而,监听器可以依赖于他们收到的关于已经在数据库中的通知。因此,我不希望在提交事务之前发送(或至少不接收)消息
有没有一个标准的方法?对于如何实现这一点,人们有什么建议?您可以使用挂起状态将记录写入数据库。然后根据挂起状态轮询数据库中的挂起记录。此过程有效地解耦了当前的同步处理,并有助于实现您想要的目标。您正在寻找的是新的吗?候选版本已经发布了,所以我想最终版本应该很快就会发布,尽管我不确定要多久。谢谢分享。然而,读了这篇文章,听起来我不需要那种特殊的同步。我想我可以使用现有的同步。我要寻找的是在发布-订阅通道和服务激活器之间用异步通道桥接发布-订阅通道。我确实需要将事务与通道同步,但我对已经存在的事务通道持开放态度。我只是找不到任何我想要的例子。