Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy JMS Message properties

Tags:

properties

jms

Is there an easy way of copying off the properties from one JMS message to another?

I can imagine something like this:

private void copyMessageProperties (Message msg1, Message msg2) throws JMSException {
    Enumeration srcProperties = msg1.getPropertyNames();
    while (srcProperties.hasMoreElements()) {
        String propertyName = (String) srcProperties.nextElement ();

        // Now try to read and set
        try {
            Object obj = msg1.getObjectProperty (propertyName);
            msg2.setObjectProperty (propertyName, obj);
            continue;
        } catch (Exception e) {}
        try {
            String str = msg1.getStringProperty (propertyName);
            msg2.setStringProperty (propertyName, str);
            continue;
            ...
        }
    }
}

But that is seriously ugly. There must be another way

like image 704
Shaun Avatar asked Sep 17 '10 15:09

Shaun


People also ask

What are JMS properties?

JMS properties comprise message headers and message properties. MessageHeader properties are set by the JMS client sending the message. You can view these after the message is received. You can also set MessageProperties on the outgoing messages on the Input tab of the activity that sends messages.

What are the 3 sub categories of message properties?

A JMS message has three parts: a header, properties, and a body. Only the header is required. The following sections describe these parts. For complete documentation of message headers, properties, and bodies, see the documentation of the Message interface in the API documentation.

How do I read a JMS header?

(String) This header is set by the application for use by other applications. (Integer) This header is set by the JMS provider and denotes the delivery mode. (Long) A value of zero means that the message does not expire. Any other value denotes the expiration time for when the message is removed from the queue.

What is JMSCorrelationID?

The JMSCorrelationID provides a header for associating the current message with some previous message or application-specific ID. In most cases, the JMSCorrelationID will be used to tag a message as a reply to a previous message.


1 Answers

Here's the solution I ended up with...

@SuppressWarnings("unchecked")
private static HashMap<String, Object> getMessageProperties(Message msg) throws JMSException 
{
   HashMap<String, Object> properties = new HashMap<String, Object> ();
   Enumeration srcProperties = msg.getPropertyNames();
   while (srcProperties.hasMoreElements()) {
       String propertyName = (String)srcProperties.nextElement ();
       properties.put(propertyName, msg.getObjectProperty (propertyName));
   }
   return properties;
}

private static void setMessageProperties(Message msg, HashMap<String, Object> properties) throws JMSException {
    if (properties == null) {
        return;
    }
    for (Map.Entry<String, Object> entry : properties.entrySet()) {
        String propertyName = entry.getKey ();
        Object value = entry.getValue ();
        msg.setObjectProperty(propertyName, value);
    }
}
like image 150
Shaun Avatar answered Sep 25 '22 07:09

Shaun