1

I am able to set correlation id for IBM mq but unable to set message id for the message the message id I am setting is being overridden by the MQ how to set this message id below one is the code I am trying please help me on this task. Is there any thing I need do in the code???

 public static void main(String args[]) 
    {

    try{
       MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
          cf.setHostName("xxx");
          cf.setPort(4444);
          cf.setTransportType(1);
          cf.setQueueManager("xxxx");
          cf.setChannel("CLIENT.xyZ");

          MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
          MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

          MQQueue queue = (MQQueue) session.createQueue("WW.ESB.ENTRY.SERVICE.IN");
          queue.setBooleanProperty(WMQConstants.WMQ_MQMD_WRITE_ENABLED, true);
 queue.setIntProperty(WMQConstants.WMQ_MQMD_MESSAGE_CONTEXT, WMQConstants.WMQ_MDCTX_SET_IDENTITY_CONTEXT);
          MQQueueSender sender =  (MQQueueSender) session.createSender(queue);

          true);




          File f=new File("C:/InputPayloads/Payloads/test4.xml");
          JMSTextMessage message = (JMSTextMessage) session.createTextMessage(FileUtils.readFileToString(f)); 
          message.setStringProperty("JMS_IBM_MQMD_UserIdentifier", "avada2");


          // Hex-string 010203040506070801020304050607080102030405060708
          byte[] customMessageId = new byte[24];
          for (int i = 0; i < 24; i++) {
            customMessageId[i] = (byte) ((i % 8) + 1);
          }

           message.setObjectProperty(WMQConstants.JMS_IBM_MQMD_MSGID, customMessageId);


          message.setStringProperty("xxx", "SH_TEST04");
          message.setStringProperty("yyy", "JP");
          message.setStringProperty("zzz", "1");
          connection.start();

          System.out.println("before Sent message:\\n" + message);

          sender.send(message);
          System.out.println("Sent message:\\n" + message);

          sender.close();
          session.close();
          connection.close();
    }catch(Exception e)
    {
        System.out.println(e);
    }
}

} I am getting below error

com.ibm.msg.client.jms.DetailedJMSSecurityException: JMSWMQ2008: Failed to open MQ queue 'WW.zzz.xxx.yyy.zz'.

JMS attempted to perform an MQOPEN, but IBM MQ reported an error. Use the linked exception to determine the cause of this error. Check that the specified queue and queue manager are defined correctly.

due to this line

kushma gonna
  • 236
  • 3
  • 19
  • Setting the MsgId in your application is a bad idea. Let the queue manager generate a unique MsgId for you. If you need to set a value use the CorrelationId or simply set a message property. – Roger Oct 19 '18 at 15:43
  • @Roager I said the same thing to my manager but he was saying it was mandatory with out saying reason – kushma gonna Oct 19 '18 at 15:47
  • So, your manager's idea may introduce messages with duplicate MsgIds in the future because of a future coding bug. IBM says that if you let the queue manager generate the MsgId then it will be generated to be unique. Tell your manager that his/her idea goes against IBM's MQ Best Practices. – Roger Oct 19 '18 at 15:57

1 Answers1

2

The JMS Spec indicates that the message ID must be set by the JMS provider and that it must either be unique or null, i.e. you can't set it yourself.

However, you can use an IBM MQ specific extension to set the Message ID yourself, bearing in mind that you are now breaking the JMS Spec.

To do so, you need to set JMS_IBM_MQMD_MsgId, whose value is then copied into JMSMessageID (i.e. you can't set it directly).

Now you know the name of the attribute to set, see this other question for more details and a code example in an answer from an IBM MQ JMS expert (@Calanais).

Further reading

Morag Hughson
  • 7,255
  • 15
  • 44
  • I replaced the above code message.setJMSMessageID("98765432110111213141516123456783"); with byte[] customMessageId = new byte[24]; for (int i = 0; i < 24; i++) { customMessageId[i] = (byte) ((i % 8) + 1); }message.setObjectProperty(WMQConstants.JMS_IBM_MQMD_MSGID, customMessageId); but still there is no effect – kushma gonna Oct 19 '18 at 10:58
  • If you run the sample SimpleWMQMDWrite.java referenced in the answer I pointed you at, does that work? How does your code differ from that sample? – Morag Hughson Oct 21 '18 at 19:26
  • I updated it is same as the code I am running after your reference I had updated the code – kushma gonna Oct 22 '18 at 09:19
  • I'm not sure if that answers my question or not - does the sample work for you? – Morag Hughson Oct 22 '18 at 10:45
  • The SimpleWMQMDWrite.java sample does not work for you? – Morag Hughson Oct 22 '18 at 10:50
  • 1
    Comparing your code to the sample, I see you have set the boolean property WMQ_MQMD_WRITE_ENABLED on the sender, not on the queue. – Morag Hughson Oct 22 '18 at 10:50
  • I had and changed the code as you mentioned. changed the object "sender" to "queue" but getting the error shown above – kushma gonna Oct 22 '18 at 11:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182262/discussion-between-kushma-gonna-and-morag-hughson). – kushma gonna Oct 22 '18 at 12:18