本文介绍了IBM MQ事务和.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用.net C#(IBM MQ版本9.1.5)从队列中拉出消息.因此,我没有任何问题连接到队列并获取消息.我已经读到了交易的概念分布式交易.

I have used .net C# (IBM MQ version 9.1.5) to pull messages from the queue. So I have no issues connecting to the queue and getting messages.I have read that there is the concept of transactions Distributed Transactions.

我尝试了以下操作:

var getMessageOptions = new MQGetMessageOptions();
getMessageOptions = new MQGetMessageOptions();
getMessageOptions.Options += MQC.MQGMO_WAIT + MQC.MQGMO_SYNCPOINT;
getMessageOptions.WaitInterval = 20000;  // 20 seconds wait

Transaction oldAmbient = Transaction.Current;
using (var tx = new CommittableTransaction())
{
  try
  {
    int i = queue.CurrentDepth;
    Log.Information($"Current queue depth is {i} message(s)");
    var message = new MQMessage();
    queue.Get(message, getMessageOptions);

    string messageStr = message.ReadString(message.DataLength);
    Log.Information(messageStr);

    tx.Commit();
  }
  catch (MQException e) when (e.Reason == 2033)
  {
   // Report exceptions other than "no messages in the queue"
   Log.Information("No messages in the queue");
   tx.Rollback();
  }
  catch (Exception ex)
  {
   Log.Error($"Exception when trying to capture a message from the queue: {ex.Message}");
   tx.Rollback();
  }

我收到2035的错误代码.

I am getting an error code of 2035.

正在恢复事务," SYSTEM.DOTNET.XARECOVERY.QUEUE存放在哪里,它在队列中吗?我需要为此启用权限吗?

Looking at the documents on Recovering Transactions, where does the "SYSTEM.DOTNET.XARECOVERY.QUEUE" live, is it on the queuemanger?Do I need to get permissions enabled on this?

我还看到提到了Microsoft Distributed Transaction Manager,这是我们需要在本地主机上运行才能使分布式事务正常工作的东西吗?

Also I see that Microsoft Distributed Transaction Manager is mentioned, is this something that we need to have running on the local host in order for distributed transactions to work?

推荐答案

如果正在使用MQ分布式事务功能,则运行该应用程序的用户应具有"SYSTEM.DOTNET.XARECOVERY.QUEUE"的权限.未完成的"SYSTEM.DOTNET.XARECOVERY.QUEUE"队列将未完成的交易信息作为消息保存在该队列中,以后可用于解决交易.

If MQ Distributed transactions feature is being used then the user running the application should have the authority to "SYSTEM.DOTNET.XARECOVERY.QUEUE".If a transaction is incomplete "SYSTEM.DOTNET.XARECOVERY.QUEUE" queue holds the information of incomplete transaction as message in that queue,which later can be used to resolve the transaction.

根据您在方案中添加的评论,即",我们只想将邮件保存到文件中.我的想法是,如果出现问题,我可以回滚该交易.".如果MQ是唯一的资源管理器,那么您不必使用分布式事务.也可以使用在同步点下获取消息来代替分布式事务.如果使用多个资源管理器,则分布式事务将很有用.

Based on your scenario which you had put in comments i.e "we want to just save the message to a file. My thinking is if there is a problem with that, I could roll back the transaction." .If MQ is the only resource manager then you don't have to use Distributed transactions. Getting a message under syncpoint can also be used instead of Distributed Transactions. Distributed Transactions will be useful if more than one resource manager is being used.

要在同步点下获取消息,可以通过更新主机名,通道,端口,队列和队列管理器名称来使用以下示例代码:

To get a message under syncpoint following sample code can be used by updating hostname,channel,port,queue and queue manager name:

    var getMessageOptions = new MQGetMessageOptions();
    getMessageOptions = new MQGetMessageOptions();
    getMessageOptions.Options += MQC.MQGMO_WAIT + MQC.MQGMO_SYNCPOINT;
    getMessageOptions.WaitInterval = 20000;  // 20 seconds wait
    Hashtable props = new Hashtable();
    props.Add(MQC.HOST_NAME_PROPERTY, "localhost");
    props.Add(MQC.CHANNEL_PROPERTY, "DOTNET.SVRCONN");
    props.Add(MQC.PORT_PROPERTY, 3636);
    MQQueueManager qm = new MQQueueManager("QM", props);
    MQQueue queue = qm.AccessQueue("Q1", MQC.MQOO_INPUT_AS_Q_DEF);

    try
    {
        var message = new MQMessage();
        queue.Get(message, getMessageOptions);
        //to commit the message
        qm.Commit();
        string messageStr = message.ReadString(message.DataLength);
    }
    catch (MQException e) when (e.Reason == 2033)
    {
       // Report exceptions other than "no messages in the queue"
         Log.Information("No messages in the queue");
    }
    catch (Exception ex)
    {
      Log.Error($"Exception when trying to capture a message from the queue:
    }

这篇关于IBM MQ事务和.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 08:42