我有一个场景,其中两个 jms 出站 channel 适配器从 pub-sub channel 读取数据,我希望它们参与事务。换句话说,我想把这条信息写给他们两个,或者不写给任何人。即使我将 session 事务设置为 true,但这似乎并没有发生。我的配置如下:
<int:publish-subscribe-channel id="test.pubsub" ignore-failures="false" ></int:publish-subscribe-channel>
<jms:outbound-channel-adapter channel="test.pubsub" order="1" destination-name="${outbound.queue}" session-transacted="true"
connection-factory="${connection.factory}"></jms:outbound-channel-adapter>
<jms:outbound-channel-adapter id="jmsOutputMirror" session-transacted="true"
destination-name="${outbound.queue.mirror}"
connection-factory="${connection.factory}"
channel="test.pubsub" order="2">
</jms:outbound-channel-adapter>
它们都从 CachingConnectionFactory 获取连接。
最佳答案
session-transaction
在那里对你没有帮助:
/**
* Set the transaction mode that is used when creating a JMS {@link Session}.
* Default is "false".
* <p>Note that within a JTA transaction, the parameters passed to
* {@code create(Queue/Topic)Session(boolean transacted, int acknowledgeMode)}
* method are not taken into account. Depending on the Java EE transaction context,
* the container makes its own decisions on these values. Analogously, these
* parameters are not taken into account within a locally managed transaction
* either, since the accessor operates on an existing JMS Session in this case.
* <p>Setting this flag to "true" will use a short local JMS transaction
* when running outside of a managed transaction, and a synchronized local
* JMS transaction in case of a managed transaction (other than an XA
* transaction) being present. This has the effect of a local JMS
* transaction being managed alongside the main transaction (which might
* be a native JDBC transaction), with the JMS transaction committing
* right after the main transaction.
* @see javax.jms.Connection#createSession(boolean, int)
*/
public void setSessionTransacted(boolean sessionTransacted) {
this.sessionTransacted = sessionTransacted;
}
如果您对它们的调用没有包装到 TX。由于您很幸运并在同一线程中调用两个适配器,因此只需将消息包装发送到 test.pubsub
即可。进入TX。例如一些@Transactional
@Gateway
在那条 channel 的前面。或任何其他可能的<tx:advice>
钩子(Hook)。您甚至可以考虑这个解决方案:Keep transaction within Spring Integration flow
关于java - 同一事务中的出站 channel 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36850405/