Work with message properties
We can work with the message properties to affect subsequent processing.
Before starting this task, we should read about the properties supported by the SIMessage interface in Message properties support for mediations.
There are two different types of message properties:
- System properties (including JMS headers, JMSX properties, and JMS_IBM_properties)
- User properties.
We can work with message properties to affect which messages a later mediation should process, or to affect processing by a downstream application or mediation. The rule set in the selector field during mediation configuration tests values in the message properties.
We can access, modify and clear properties using the SIMessage interface (see SIMessage.) There are three different sets of methods:
- These properties operate on system properties, plus user properties if the name is qualified with a prefix user.:
- getMessageProperty
- setMessageProperty
- deleteMessageProperty
- clearMessageProperties
- These properties operate on user properties only, without the need for the prefix user.:
- getUserProperty
- setUserProperty
- deletUserProperty
- clearUserProperties
- getUserPropertyNames returns a list of the names of the user properties in the message.
Typically, we can work with message properties in the following way, when programming a mediation:
Tasks
- Locate the point in your mediation handler where you insert the functional mediation code, in the method handle (MessageContext context). The interface is MessageContext, and we should cast this to SIMessageContext unless we are only interested in the methods provided by MessageContext.
- Get the SIMessage from the MessageContext object. For example, SIMessage message = ((SIMessageContext)context).getSIMessage();
- Build your mediation header function in a similar way to these examples, using the reference information in Message properties support for mediations to help:
- Get a user property of the message. For instance, String task = (String)msg1.getUserProperty("task");. In this case, the task string might refer to an operation that the mediation should perform.
- Set a user property, where message Properties are stored as name-value pairs. The setUserProperty method might only be used to set user properties, so the name passed into the method should not include the "user." prefix. For example, msg1.setUserProperty("background","green");
- Delete a user property from the message. For instance, msg1.deleteUserProperty("task");
Example
Mediation function code to work with message properties might look similar to the code snippet in this example:
String task = (String)msg1.getUserProperty("task"); if (task != null) { if (task.equals("addColor")) { msg1.setMessageProperty(SIProperties.JMS_IBM_Format, "colorful"); msg1.setUserProperty("background","green"); msg1.setUserProperty("foreground","purple"); msg1.setUserProperty("depth",new Integer(3)); msg1.deleteUserProperty("task"); } else { msg1.clearUserProperties(); } }