2013-04-04 79 views
7

我正在使用IBM.XMS lib與WebSphereMQ交談。異步消費者和使用TransactionScope

當使用同步方法接收消息,例如:

using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions)) 
{ 
     message = consumer.Receive(1000); 

     if (message != null) 
     { 
      //Do work here 
      scope.Complete(); 
     } 
} 

但是,如果我想使用異步方法:

consumer.MessageListener = delegate(IMessage msg) 
{ 
    //Do work here 
    //But where do I put TransactionScope? 
}; 

我不知道如何來包裝MessageListener回調在TransactionScope之內。

有誰知道如何做到這一點?

+0

如果消費者實例已從會話中創建,則會話可能已創建,以便在委託運行期間存在(Transaction.Current)周圍的環境事務。 – 2013-04-11 10:26:49

回答

1

由於消息偵聽器在與創建TransactionScope的線程不同的線程上運行,消息偵聽器(又名異步使用者)不能用於TransactionScope。您只能使用TransactionScope內的同步接收/發送。

This link says「The XA transactions are not supported in asynchronous consumers。」

+0

謝謝!如果下一個版本支持異步使用者的XA事務,那將是非常好的;-) IBM是否有某種uservoice? – 2013-04-16 04:55:32

+0

是的。您可以在https://www.ibm.com/developerworks/rfe/?BRAND_ID=181提交RFE。在此頁面上點擊「submit RFE」鏈接。 – Shashi 2013-04-16 05:12:20

+0

很酷,我已經提交了它(http://www.ibm.com/developerworks/rfe/execute?use_case=viewRfe&CR_ID=33621),請將它投票:) – 2013-04-16 06:14:43

1

在使用DependentClone創建DependentTransaction時,可能值得您一邊調查。

「一個從屬交易是一個交易,其結果取決於它被克隆的交易結果。」

「DependentTransaction是使用DependentClone方法創建的Transaction對象的一個​​克隆,它的唯一目的是允許應用程序停下來並確保事務在事務仍在執行時不能提交(for例如,在工作者線程上)。「

編輯:只是在相關的SO問題清單看準了這一點:related question and answer看看他們提供的MSDN鏈接:非常值得一讀Managing Concurrency with DependentTransaction

從MSDN鏈接採取以上(爲簡便起見):

public class WorkerThread 
{ 
    public void DoWork(DependentTransaction dependentTransaction) 
    { 
     Thread thread = new Thread(ThreadMethod); 
     thread.Start(dependentTransaction); 
    } 

    public void ThreadMethod(object transaction) 
    { 
     DependentTransaction dependentTransaction = transaction as DependentTransaction; 
     Debug.Assert(dependentTransaction != null); 

     try 
     { 
      using(TransactionScope ts = new TransactionScope(dependentTransaction)) 
      { 
       /* Perform transactional work here */ 
       ts.Complete(); 
      } 
     } 
     finally 
     { 
      dependentTransaction.Complete(); 
      dependentTransaction.Dispose(); 
     } 
    } 

//Client code 
using(TransactionScope scope = new TransactionScope()) 
{ 
    Transaction currentTransaction = Transaction.Current; 
    DependentTransaction dependentTransaction;  
    dependentTransaction = currentTransaction.DependentClone(DependentCloneOption.BlockCommitUntilComplete); 
    WorkerThread workerThread = new WorkerThread(); 
    workerThread.DoWork(dependentTransaction); 

    /* Do some transactional work here, then: */ 
    scope.Complete(); 
} 
+0

我需要在'TransactionScope'中登記的消息的實際出隊,我不明白這將如何解決任何問題! – 2013-04-12 05:37:22

+0

爲什麼不使用MessageQueueTransaction? http://msdn.microsoft.com/en-us/library/system.messaging.messagequeuetransaction(v=vs.71).aspx – 2013-04-12 07:54:33

+0

'MessageQueueTransaction'與'MessageQueue',MSMQ一起使用。我沒有使用MSMQ,我正在使用WebSphereMQ – 2013-04-13 07:19:11

相關問題