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

  • JMSMessageId after message is sent

    Hi guys,

    I want to know if there is a way to get a JMSMessageId using JMSTemplate after message is delivered. I am setting MessageID before sending the message but somehow JMS is overwriting my MessageID and on broker we are receiving a different messageID. so i want to get the JMSMessageID after message is delivered so i can correlate response message with this ID because broker is setting the request MessageID in the response correlation ID.

    thanks,
    Muhwas

  • #2
    The JMS spec describes how a JMS provider must configure the JMSMessageID after a message is sent - so you cannot provide one yourself.

    If you want to specify some kind of correlation ID on a message, use setJMSCorrelationID()

    http://activemq.apache.org/how-shoul...-with-jms.html

    Comment


    • #3
      Hi,

      just to clarify.
      After sending the Message you can get the MessageID through message.getJMSMessageID()

      Regards
      agim

      Comment


      • #4
        Retrieving JMSMessageID with JmsTemplate.send method

        Hello Muhwas,

        Unfortunately, the JmsTemplate's send methods (send(...) and convertAndSend(...) ) don't return the sent JMS Message to allow developers to retrieve the fields generated by the JMS provider during the send operation (JMSMessageID ...).

        Here is a workaround with a MessagePostProcessor that keeps a reference on the sent JMS Message. Thanks to this reference, you can retrieve any message property or field after sending the message.

        Code:
        public class SpringFrameworkJmsSenderSample {
        
            /**
             * <code>MessagePostProcessor</code> in charge of holding a reference on the sent message.
             * <p>
             * Useful to retrieve the JMS properties that has been generated by the JMS Provider (e.g.
             * jmsMessageID ...)
             */
            public static class ReferenceHolderMessagePostProcessor implements MessagePostProcessor {
                protected Message sentMessage;
        
                /**
                 * Return the sent message.
                 */
                public Message getSentMessage() {
                    return sentMessage;
                }
        
                public Message postProcessMessage(Message message) throws JMSException {
                    // keep a reference on the messge
                    this.sentMessage = message;
                    return message;
                }
        
            }
        
            protected JmsTemplate jmsTemplate;
        
            public void setJmsTemplate(JmsTemplate jmsTemplate) {
                this.jmsTemplate = jmsTemplate;
            }
        
            /**
             * Send a JMS Message with the given <code>message</code> and retrieves the generated
             * JMSMessageID.
             */
            public void simpleSend(Object message) throws Exception {
        
                ReferenceHolderMessagePostProcessor messagePostProcessor = new ReferenceHolderMessagePostProcessor();
                this.jmsTemplate.convertAndSend(message, messagePostProcessor);
        
                Message sentMessage = messagePostProcessor.getSentMessage();
                System.out.println("Generated JMSMessageID" + sentMessage.getJMSMessageID());
            }
        }
        Hope this helps,

        Cyrille

        Comment


        • #5
          Hi,

          you could also use any callback (Producer or Sessioncallback) to get the message properties after sending them.

          just my 2 cents

          regards
          agim

          Comment


          • #6
            Try this...

            public String sendMessage(final String message, final Destination destination) throws Exception {
            RequestSendMessageCreator messageCreator = new RequestSendMessageCreator();
            messageCreator.setMessage(message);
            jmsTemplate.send(destination, messageCreator);
            return messageCreator.getMessageId();
            }

            public String receiveMessage(final String messageId, final Destination destination) throws Exception {
            String receiveSelector = new String("JMSCorrelationID = " + "\'"
            + messageId + "\'");
            javax.jms.Message msg = jmsTemplate.receiveSelected(destination, receiveSelector);
            return null != msg ? ((javax.jms.TextMessage) msg).getText(): "";
            }


            private class RequestSendMessageCreator implements org.springframework.jms.core.MessageCreator {
            private String message;
            private javax.jms.TextMessage textMessage;

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

            public String getMessageId() throws Exception {
            return textMessage.getJMSMessageID();
            }

            public void setTextMessage(javax.jms.TextMessage textMessage) {
            this.textMessage = textMessage;
            }

            public javax.jms.TextMessage getTextMessage() {
            return textMessage;
            }

            public javax.jms.Message createMessage(javax.jms.Session session) throws javax.jms.JMSException {
            setTextMessage(session.createTextMessage(message)) ;
            return getTextMessage();
            }
            }

            Jeff
            jgilbert

            Comment


            • #7
              Hi,

              I was looking for a way to solve the same problem. I've tried the following approach, and it works for me.

              Code:
              public class Sender {
                  private JmsTemplate jmsTemplate;
              
                  public void setJmsTemplate(JmsTemplate jmsTemplate) {
                      this.jmsTemplate = jmsTemplate;        
                  }
              
                  public String sendTextMessage(final String text) throws JMSException {
                      final AtomicReference<TextMessage> message = new AtomicReference<TextMessage>();
              
                      jmsTemplate.send(new MessageCreator() {
              
                          @Override
                          public Message createMessage(Session session) throws JMSException {
                              message.set(session.createTextMessage(text));
                              return message.get();
                          }
                      });
              
                      return message.get().getJMSMessageID();
              
                  }
              }

              Comment

              Working...
              X