当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - WebSphere MQ check string with mq message

问题描述:

I want to read the first MQ message off a queue (without removing it) and save this message into my database. Then if everything is done I need to permanently remove this message off the MQ queue. What I do know, is to just delete the first message in the queue. But how do I know if the same message that I saved into the database is the same one I will delete from the MQ?

Thanks,

Michael

Edit:

Now tried to handle the actions with a TransactionScope in C#(Console Application):

using (TransactionScope scope = new TransactionScope){

//MQ Connection ( read message of queue)

Hashtable properties = new Hashtable();

properties.Add(MQC.HOST_NAME_PROPERTY, _settings.MQServer);

properties.Add(MQC.PORT_PROPERTY, _settings.MQPort);

properties.Add(MQC.CHANNEL_PROPERTY, _settings.MQChannelName);

properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_XACLIENT);

MQQueueManager _MQHandler = new MQQueueManager(_settings.MQManager, properties);

MQQueue = _MQHandler.AccessQueue(ReceiveQueueName,

MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING);

MQQueueMessage = new MQMessage();

MQQueueMessage.Format = MQC.MQFMT_STRING;

MQQueueGetMessageOptions = new MQGetMessageOptions();

MQQueueGetMessageOptions.Options += MQC.MQGMO_SYNCPOINT; //new

MQQueue.Get(MQQueueMessage, MQQueueGetMessageOptions);

//DB Connection (save message in database)

if ( DBsuccess ) {

_MQHandler.Commit();

scope.Complete();

}else{

_MQHandler.Backout();

}

}

This seems to do what I want :) But some question:

- If I make a syncpoint at a Put/Get, then call the Backout(), will the MQ only rollback to this syncpoint?

- Is the MQ blocked for other MQ managers, if a message is a unit of work?

网友答案:

So to clarify some terminology, the message is on the queue, not the channel - the channel just gives you access into a queue manager. So in effect you want to 'get' the first message (inside a unit of work with GMO_SYNCPOINT), store it into a database, and if the storing works, remove the message from a queue (via a commit). To do this, you would get the message from the queue in a unit of work, and only 'commit' the get (ie make it happen) and the database update at the same time. Generally speaking, you would use transaction coordination, to ensure the database update and the get either both happen (commit) or neither happen (rollback). Hopefully you can use the o/s DTC (distributed transaction coordinator) to make both MQ and the database participate in the same transaction - you also need to look at whether you want managed or unmanaged connections as they are different implementations - See the info center http://www-01.ibm.com/support/knowledgecenter/SSFKSJ_8.0.0/com.ibm.mq.dev.doc/q029290_.htm

分享给朋友:
您可能感兴趣的文章:
随机阅读: