java - 同一事务中的出站 channel 适配器

标签 java spring spring-integration

我有一个场景,其中两个 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/

相关文章:

java - 为什么(我的)Java 比 C++ 快 25 倍?

java - 尝试部署到 AZURE 时出错无法执行目标 org.apache.maven.plugins :maven-surefire-plugin:2. 22.2:test

java - 修改 Spring 消息文本而不重新启动应用程序?

spring - @Async 和 @Transactional : not working

xml - eclipse sts 4 中没有集成图 View ?

java - Spring批处理从2.1.8.RELEASE升级到3.0.0.RELEASE抛出异常

java - 使用 selenium webdriver 进行自动化但无法单击元素

Java UDF 与用户定义方法相同吗?

java - 如何从主类之外访问 Spring bean

spring-integration - 通过拒绝确认重读来自 Kafka 主题的消息