简体   繁体   English

将MQ消息放入队列

[英]Putting MQ message in a queue

I have a software where i drop MQMessages through C# software. 我有一个通过C#软件删除MQMessages的软件。 First: i receive a message from MQ as XML message string, i do some Data processing and then i prepare XML file and drop it in the response Queue. 首先:我从MQ接收到作为XML消息字符串的消息,我进行了一些数据处理,然后准备XML文件并将其放入响应队列。 i set the Correlation Id of the Request message as same as the response message as well as the MessageId like this: 我将Request消息的Correlation ID设置为与响应消息以及MessageId如下所示:

MQMessage _msg = new MQMessage();
_msg.Encoding = 273;
_msg.CharacterSet = 37;
_msg.Format = "MQSTR";
_msg.CorrelationId = message.CorrelationId;
_msg.MessageId = message.MessageId;

where message is the message i receive (Request message) and _msg is the response message that i put (Response message) 其中message是我收到的消息(请求消息), _msg是我_msg的响应消息(Response消息)

In IBM side when i receive the response message, I check the correlation Id with the correlationId of the request message and it fails. 在IBM端,当我收到响应消息时,我用请求消息的correlationId来检查相关性Id,但它失败了。

Here is how i put the message in the queue: 这是我将消息放入队列的方式:

 qmgr.AccessQueue(message.ReplyToQueueName.Trim(), MQC.MQOO_OUTPUT 
 + MQC.MQOO_FAIL_IF_QUIESCING, QueueManagerName, DynamicQueueName, "").Put(_msg);

What is missing in my code ? 我的代码中缺少什么?

What is the world are you doing? 你在做什么?

i set the Correlation Id of the Request message as same as the response message as well as the MessageId like this: 我将Request消息的Correlation ID设置为与响应消息以及MessageId相同,如下所示:

Bad, very bad design and goes against the MQ messaging pattern. 错误的设计,非常糟糕,并且违反了MQ消息传递模式。

(1) Do not set the CCSID and Encoding, you should use the default values and let MQ do the work. (1)不要设置CCSID和编码,您应该使用默认值并让MQ完成工作。

(2) The proper messaging pattern is to set the response message's CorrelationId with the request message's MessageId. (2)正确的消息传递模式是将响应消息的CorrelationId设置为请求消息的MessageId。 That is how you tie things together. 这就是您将事物捆绑在一起的方式。

Your code should look like: 您的代码应如下所示:

MQMessage outMsg = new MQMessage();
outMsg.Encoding = MQC.MQENC_NATIVE;
outMsg.CharacterSet = MQC.MQCCSI_DEFAULT;
outMsg.Format = MQC.MQFMT_STRING;
outMsg.MessageId = MQC.MQMI_NONE;
outMsg.CorrelationId = inMsg.MessageId;

What in the world is this? 这到底是什么?

qmgr.AccessQueue(message.ReplyToQueueName.Trim(), MQC.MQOO_OUTPUT 
 + MQC.MQOO_FAIL_IF_QUIESCING, QueueManagerName, DynamicQueueName, "").Put(_msg);

OMG. 我的天啊。 Let me count the ways you have messed this up. 让我数一数您搞砸的方式。 You are creating a temporary dynamic queue based on the name in the 'message.ReplyToQueueName' field, then you put the message to the temporary dynamic queue and finally when the program ends (or you actually close the queue), the queue is deleted (along with your message). 您正在基于'message.ReplyToQueueName'字段中的名称创建一个临时动态队列,然后将消息放入临时动态队列,最后在程序结束时(或实际上关闭队列),该队列被删除(以及您的消息)。

This is the proper way to do it: 这是执行此操作的正确方法:

MQQueue outQ = null;
MQPutMessageOptions pmo = new MQPutMessageOptions();
try
{
   outQ = qmgr.AccessQueue( inMsg.ReplyToQueueName.Trim(),
                            MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING );

   outQ.Put(outMsg, pmo);
}
catch (MQException mqe)
{
   System.Console.WriteLine("MQException CC=" + mqe.CompletionCode + " : RC=" + mqe.ReasonCode);
}
finally
{
   try
   {
      if (outQ != null)
         outQ.Close();  // Close the Queue
   }
   catch (MQException mqe)
   {
      System.Console.WriteLine("MQException CC=" + mqe.CompletionCode + " : RC=" + mqe.ReasonCode);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM