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
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.
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.
(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.
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With