Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
864 views
in Technique[技术] by (71.8m points)

java - Setting IBM MQ custom property in JMS doesn't work

Tried setting an IBM MQ custom property in JMS while sending messages. It doesn't work.

I'm looking for an equivalent of the following in JMS/Apache camel.

mQMessage.setStringProperty( "customProperty", "123" );

Tried the following 3 options:

1) exchange.getIn().setHeader( "customProperty", "123" );
2) exchange.getIn().setProperty( "customProperty", "123" );
3) mQQueueConnectionFactory.setStringProperty( "customProperty", "123" );

The following code to read the property throws error because the property doesn't exist it seems. mQMessage.getStringProperty( "messageGlobalSequenceNumber" )

throws the following error:

com.ibm.mq.MQException: MQJE001: Completion Code '2', Reason '2471'.
        at com.ibm.mq.MQMessage.getProperty(MQMessage.java:5694)
        at com.ibm.mq.MQMessage.getStringProperty(MQMessage.java:6949)
        at com.ibm.mq.MQMessage.getStringProperty(MQMessage.java:6925)
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Are you sure that the property that you are trying to retrieve actually exists for that message? Because Reason Code of 2471 (MQRC_PROPERTY_NOT_AVAILABLE) clearly says that the named property does not exist.

The correct way to create a message property in JMS (for IBM MQ) is as follows:

/**
 * Send a message to a queue.
 * @param session
 * @param myQ
 * @throws JMSException
 */
private void sendMsg(QueueSession session, Queue myQ) throws JMSException
{
   QueueSender sender = null;

   try
   {
      TextMessage msg = session.createTextMessage();
      msg.setText("This is a test message.");
      msg.setStringProperty("MyProp01", "somevalue");

      sender = session.createSender(myQ);
      sender.send(msg);
   }
   finally
   {
      try
      {
         if (sender != null)
            sender.close();
      }
      catch (Exception ex)
      {
         System.out.println("sender.close() : " + ex.getLocalizedMessage());
      }
   }
}

Did you use an MQ tool to check the property values of the message? I ran the above code then checked the message on the queue with MQ Visual Edit and here is a screen-shot:

enter image description here

Or a screen-shot of the opened selected message showing the Named Properties (aka message properties):

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...