4 Replies Latest reply on Jul 20, 2007 5:30 AM by joshiashutosh

    Adding properties to a received message

    joshiashutosh

      Hi,
      I am using Jboss Messaging for my project. One of my requirements is that I wish to add some properties to the message received from one queue and put it on another queue.
      The problem is that properties of a received message are read-only and adding properties gives a MessageNotWriteableException.
      The two ways I can think of, of dealing with this problem are:
      1. Create a new message, copy all properties of the old message to the new message and then add new properties as follows:
      ObjectMessage newMessage = null;
      String name = null;
      Object value = null;
      try {
      newMessage = producerSession.createObjectMessage();
      Enumeration propNames = message.getPropertyNames();
      while (propNames.hasMoreElements()) {
      name = propNames.nextElement();
      value = message.getObjectProperty(name);
      newMessage.setObjectProperty(name, value);
      }
      }
      catch (JMSException jmse) {
      logger.warn("Unable to set message property " + name + " !");
      }
      message = null;
      message = newMessage;

      2. Save the message properties (say, in a hash), call clearProperties, put the properties back and add the new properties.

      Is there a better way of adding properties? If not, which of the above methods is better?

      Thanks,
      --Ashutosh

        • 1. Re: Adding properties to a received message
          timfox

          Yes, this is a pain isn't it?

          We have the JMS spec to blame for that.

          Either of your two methods will work.

          Actually we have exactly this problem in the message bridge where we have to forward a message from one source to a a target.

          In that case we use method 2)

          Take a look at the message bridge code for an example:

          See http://anonsvn.jboss.org/repos/messaging/tags/JBossMessaging_1_4_0_CR1/src/main/org/jboss/jms/server/bridge/Bridge.java
          search for addMessageIDInHeader

          • 2. Re: Adding properties to a received message
            joshiashutosh

            Thanks for the response, Tim.
            I think I too will go with method 2.
            I looked at the Bridge code and am myself using an identical one. One problem I am facing is that it gives an exception while setting the property JMSXDeliveryCount. It seems to be an internal property and Jboss Messaging doesn't allow adding it.
            But in the Bridge code if such an exception occurs in setObjectProperty, and since the exception is not handled in the method, is it possible that some properties may not be added?

            • 3. Re: Adding properties to a received message
              timfox

               

              "joshiashutosh" wrote:
              Thanks for the response, Tim.
              I think I too will go with method 2.
              I looked at the Bridge code and am myself using an identical one. One problem I am facing is that it gives an exception while setting the property JMSXDeliveryCount. It seems to be an internal property and Jboss Messaging doesn't allow adding it.
              But in the Bridge code if such an exception occurs in setObjectProperty, and since the exception is not handled in the method, is it possible that some properties may not be added?


              That's strange, JMSDeliveryCout should be settable. Are you sure it's not the other JMS provider that's throwing the exception?

              Anyway, I have changed the code slighly so it will only attempt to set JMSXGroupID and JMSXGroupSeq

              • 4. Re: Adding properties to a received message
                joshiashutosh

                No. I am using only Jboss Messaging.
                Thanks again for the help.