我的流程设置是这样的:交易完成不确定性
<int:channel id="channel1">
<int:queue/>
</int:channel>
<int:channel id="channel2">
<int:queue/>
</int:channel>
<int:chain id="chain1" input-channel="channel1" output-channel="channel2">
<int:poller>
<int:transactional propagation="REQUIRES_NEW"/>
</int:poller>
<Authorzier/>
<JMS_put1/>
<DB_update_state1/>
</int:chain>
<int:chain id="chain2" input-channel="channel2" output-channel="nullChannel">
<int:poller>
<int:transactional propagation="REQUIRES_NEW"/>
</int:poller>
<Transformer/>
<JMS_put2/>
<DB_update_state2/>
</int:chain>
在某些情况下的链2的交易链1的交易前完成
现在,我有DB_state_1在数据库中。
如何在链1的消息发送到output-channel
之前强制链1的事务完成?
我知道我可以使用TransactionSynchronization
发送消息到channel2 afterCommit()
,但我认为必须有一个更优雅的解决方案。
EDIT CURRENT WORKAROUND
@ServiceActivator
public void sendToDestinationFlow(Message<?> message) {
TxSenderSyncer s = new TxSenderSyncer(message, this.channel, this.errorChannel);
TransactionSynchronizationManager.registerSynchronization(s);
}
private static class TxSenderSyncer implements TransactionSynchronization {
private Message<?> message;
private MessageChannel channel;
private MessageChannel errorChannel;
public TxSenderSyncer(Message<?> message, MessageChannel channel, MessageChannel errorChannel) {
this.message = message;
this.channel = channel;
this.errorChannel = errorChannel;
}
@Override
public void afterCompletion(int paramInt) {
if (paramInt == STATUS_ROLLED_BACK) {
errorChannel.send(MessageBuilder.withPayload(new MessagingException(message, "Transaction rolled back")).build());
} else {
channel.send(message);
}
}
}
我已经编辑我的例子。通常,带有代表事务的轮询器的链具有其他处理程序,其中一些也是事务性资源。所以我需要在消息被发送到链的'output-channel'之前完成链的完整事务。 –
请在我的回答中查看'EDIT'。 –
这个解决方案的工作原理,但我们有另一个问题[见这篇文章](http://stackoverflow.com/questions/39097422/dataaccessexception-not-wrapped-in-messagingexception)。目前我们正在使用'EDIT CURRENT WORKAROUND'下的问题中插入的解决方法,并会很感激您的意见。 –