Announcement Announcement Module
Collapse
No announcement yet.
Retrieving JMSMessageId after sending message Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Retrieving JMSMessageId after sending message

    Hello,
    i need to be able to retrivet the JMSMessageID of a message i am sending in order to be able to retrive and answer later on.

    Fetching the id inside the JmsTemplate.send() method does not seem to work. probably because the message has not actually been sent at the time.

    This sends the message ok, but does not have an message id yet:

    jmsTemplate102.send(new MessageCreator() {
    public Message createMessage(Session session) throws JMSException {
    BytesMessage message=session.createBytesMessage();
    message.writeBytes(data.toString().getBytes());

    system.out.println(message.getJMSMessageID(); //This prints null
    return message;

    Then i thought i would use the JmsTemplate102.execute(ProducerCallback) method, but when i do this:

    jmsTemplate102.execute(new ProducerCallback() {
    public Object doInJms(Session session, MessageProducer producer) throws JMSException {
    BytesMessage message =session.createBytesMessage();
    message.writeBytes("...".getBytes());
    ((QueueSender)producer).send(message);
    System.out.println("message id " + message.getJMSMessageId());
    return null;
    }
    });

    I get an exception from the JMS provider the the queue is unknown.
    I then find that the QueueSender.getQueue() returns null. ie. the Queue is not defined for the MessageProducer.
    Please note that i can send perfectly well using the JmsTemplate102.send() method.

    Can someone help me get this to work, or suggest an alternative solution.

    sincerely
    Morten

  • #2
    I don't see where you are setting the queue. Are you setting a default Queue on the JmsTemplate? If not you need to supply it in the ProducerCallback.

    But to solve your original problem, I don't think you can use JMSMessageID. I think only the message in the queue has it. What you need to do is set a JMSCorrelationID on your message that you make up. And when you reply set that same value in the reply message.

    Comment


    • #3
      Hi Bill,
      yes i set the default queue in the spring config of the jmsTemplate.

      The way i understand the message/correlation message paradigm is that the client sends the message, storing the messageid. Then the server recieves the message, produces a new one, with the original messageid as correlationid and sends it back.
      then the client can use a messageselector to get the message that matches its original request (ie. the correlationid =original messageid).

      I actually got it working by storing a reference to the message and then examining the message through that reference after i sent it. Then the messageid is there. Its not pretty, but it works.

      The jmsTemplate.send() method should probably return the message to facilitate this kind of postprocessing.

      regarding the ProducerCallback, i suspect there is a bug somewhere in spring, since it works fine using the plain send with the default queue.

      sincerely
      Morten

      Comment


      • #4
        The example I have seen sets JMSCorrelationID generated by the program before sending the message. See here.

        You could do it either way. You should consider create a JIRA issue requesting that send return the message it sent. The only reservation I have to this approach is if the behavior of message ids is not reliable for all providers. I guess it depends on what the spec says about this.

        Comment


        • #5
          Originally posted by wpoitras
          The example I have seen sets JMSCorrelationID generated by the program before sending the message. See here.

          You could do it either way. You should consider create a JIRA issue requesting that send return the message it sent. The only reservation I have to this approach is if the behavior of message ids is not reliable for all providers. I guess it depends on what the spec says about this.
          Hi bill, thanks for the link. It might be a better way to do as you suggest.
          Thanks for the help.

          sincerely
          Morten

          Comment


          • #6
            Solution to getJmsMessageId in spring ater message is sent

            Hi,

            I am sure there may be multiple of ways of getting around the problem. I was able to getJmsMessageId by doing the following:



            I have a separate MessageCreator class called RequestSendMessageCreator.



            public class RequestSendMessageCreator implements MessageCreator {

            //message is of type javax.jms.Message
            private Message message = null;

            //Generate getter and setter for the message
            public String getTextMessage() {
            return textMessage;
            }

            public void setTextMessage(String message) {
            this.textMessage = message;
            }


            public Message createMessage(Session session) throws JMSException {
            message = session.createTextMessage("Sending test text message");
            return message;
            }

            }



            In my driver sender I do the following:


            RequestSendMessageCreator messageCreator = new SendMessageCreator();

            //Send the message
            jmsTemplate.send(messageCreator);

            try {
            //now access the message id from the messageCreator
            String msgId = messageCreator.getMessage().getJMSMessageID();
            System.out.println("Message id created " + msgId);
            } catch (Exception e){
            //Deal with the exception
            }


            I hope this helps.


            Thanks
            Bhabani

            Comment


            • #7
              Originally posted by Bhabani Dash View Post
              Solution to getJmsMessageId in spring ater message is sent

              Hi,

              I am sure there may be multiple of ways of getting around the problem. I was able to getJmsMessageId by doing the following:



              I have a separate MessageCreator class called RequestSendMessageCreator.



              public class RequestSendMessageCreator implements MessageCreator {

              //message is of type javax.jms.Message
              private Message message = null;

              //Generate getter and setter for the message
              public String getTextMessage() {
              return textMessage;
              }

              public void setTextMessage(String message) {
              this.textMessage = message;
              }


              public Message createMessage(Session session) throws JMSException {
              message = session.createTextMessage("Sending test text message");
              return message;
              }

              }



              In my driver sender I do the following:


              RequestSendMessageCreator messageCreator = new SendMessageCreator();

              //Send the message
              jmsTemplate.send(messageCreator);

              try {
              //now access the message id from the messageCreator
              String msgId = messageCreator.getMessage().getJMSMessageID();
              System.out.println("Message id created " + msgId);
              } catch (Exception e){
              //Deal with the exception
              }


              I hope this helps.


              Thanks
              Bhabani
              Bhabani, excellent. this is exactly what I was looking for. Thanks a lot!

              Comment

              Working...
              X