2016-04-25 78 views
2

我有兩個jms出站通道適配器從pub-sub通道讀取的情況,我希望他們參與事務。換句話說,我想把這個信息寫給他們兩個,或者一個也不寫。儘管我將會話事務設置爲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獲得它們的連接。

回答

2

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。或者任何其他可能的<tx:advice>掛鉤。你甚至可以考慮這個解決方案:Keep transaction within Spring Integration flow

+0

只是爲了我的理解:問題不在於我的流中的組件不能共享事務,而是我需要指定事務的邊界是什麼。對於每一個流量,Spring都不會在「開始」 – neesh

+1

M-m-m處開始交易。如果你不這樣說,Spring不會開始交易。所有關於'sessionTransacted'的知識都取決於JMS會話中的本地TX以進行特定的調用。在這兩個JMS調用之前,全局事務必須由上游控制。 –

相關問題