40.4. Working with JMS Message Properties

download PDF


The Apache CXF JMS transport has a context mechanism that can be used to inspect a JMS message's properties. The context mechanism can also be used to set a JMS message's properties.

40.4.1. Inspecting JMS Message Headers


Consumers and services use different context mechanisms to access the JMS message header properties. However, both mechanisms return the header properties as a org.apache.cxf.transports.jms.context.JMSMessageHeadersType object.

Getting the JMS Message Headers in a Service

To get the JMS message header properties from the WebServiceContext object, do the following:
  1. Obtain the context as described in the section called “Obtaining a context”.
  2. Get the message headers from the message context using the message context's get() method with the parameter org.apache.cxf.transports.jms.JMSConstants.JMS_SERVER_HEADERS.
Example 40.12, “Getting JMS Message Headers in a Service Implementation” shows code for getting the JMS message headers from a service's message context:

Example 40.12. Getting JMS Message Headers in a Service Implementation

import org.apache.cxf.transport.jms.JMSConstants;
import org.apache.cxf.transports.jms.context.JMSMessageHeadersType;
@WebService(serviceName = "HelloWorldService", 
                           portName = "HelloWorldPort",
                           endpointInterface = "org.apache.cxf.hello_world_jms.HelloWorldPortType",
                           targetNamespace = "")
  public class GreeterImplTwoWayJMS implements HelloWorldPortType
    protected WebServiceContext wsContext;

    public String greetMe(String me)
      MessageContext mc = wsContext.getMessageContext();
      JMSMessageHeadersType headers = (JMSMessageHeadersType) mc.get(JMSConstants.JMS_SERVER_HEADERS);

Getting JMS Message Header Properties in a Consumer

Once a message is successfully retrieved from the JMS transport you can inspect the JMS header properties using the consumer's response context. In addition, you can set or check the length of time the client will wait for a response before timing out, as described in the section called “Client Receive Timeout”.
To get the JMS message headers from a consumer's response context do the following:
  1. Get the response context as described in the section called “Obtaining a context”.
  2. Get the JMS message header properties from the response context using the context's get() method with org.apache.cxf.transports.jms.JMSConstants.JMS_CLIENT_RESPONSE_HEADERS as the parameter.
Example 40.13, “Getting the JMS Headers from a Consumer Response Header” shows code for getting the JMS message header properties from a consumer's response context.

Example 40.13. Getting the JMS Headers from a Consumer Response Header

import org.apache.cxf.transports.jms.context.*;
// Proxy greeter initialized previously
1BindingProvider  bp = (BindingProvider)greeter;
2Map<String, Object> responseContext = bp.getResponseContext();
3JMSMessageHeadersType responseHdr = (JMSMessageHeadersType)
Casts the proxy to a BindingProvider.
Gets the response context.
Retrieves the JMS message headers from the response context.

40.4.2. Inspecting the Message Header Properties

Standard JMS Header Properties

Table 40.3, “JMS Header Properties” lists the standard properties in the JMS header that you can inspect.
Table 40.3. JMS Header Properties
Property NameProperty TypeGetter Method
Correlation ID string getJMSCorralationID()
Delivery Mode int getJMSDeliveryMode()
Message Expiration long getJMSExpiration()
Message ID string getJMSMessageID()
Priority int getJMSPriority()
Redelivered boolean getJMSRedlivered()
Time Stamp long getJMSTimeStamp()
Type string getJMSType()
Time To LivelonggetTimeToLive()

Optional Header Properties

In addition, you can inspect any optional properties stored in the JMS header using JMSMessageHeadersType.getProperty(). The optional properties are returned as a List of org.apache.cxf.transports.jms.context.JMSPropertyType. Optional properties are stored as name/value pairs.


Example 40.14, “Reading the JMS Header Properties” shows code for inspecting some of the JMS properties using the response context.

Example 40.14. Reading the JMS Header Properties

// JMSMessageHeadersType messageHdr retrieved previously
1System.out.println("Correlation ID: "+messageHdr.getJMSCorrelationID());
2System.out.println("Message Priority: "+messageHdr.getJMSPriority());
3System.out.println("Redelivered: "+messageHdr.getRedelivered());
JMSPropertyType prop = null;
4List<JMSPropertyType> optProps = messageHdr.getProperty();
5Iterator<JMSPropertyType> iter = optProps.iterator();
6while (iter.hasNext())
  prop =;
   System.out.println("Property name: "+prop.getName());
   System.out.println("Property value: "+prop.getValue());
Prints the value of the message's correlation ID.
Prints the value of the message's priority property.
Prints the value of the message's redelivered property.
Gets the list of the message's optional header properties.
Gets an Iterator to traverse the list of properties.
Iterates through the list of optional properties and prints their name and value.

40.4.3. Setting JMS Properties


Using the request context in a consumer endpoint, you can set a number of the JMS message header properties and the consumer endpoint's timeout value. These properties are valid for a single invocation. You must reset them each time you invoke an operation on the service proxy.
Note that you cannot set header properties in a service.

JMS Header Properties

Table 40.4, “Settable JMS Header Properties” lists the properties in the JMS header that can be set using the consumer endpoint's request context.
Table 40.4. Settable JMS Header Properties
Property NameProperty TypeSetter Method
Correlation ID string setJMSCorralationID()
Delivery Mode int setJMSDeliveryMode()
Priority int setJMSPriority()
Time To Live long setTimeToLive()
To set these properties do the following:
  1. Create an org.apache.cxf.transports.jms.context.JMSMessageHeadersType object.
  2. Populate the values you want to set using the appropriate setter methods described in Table 40.4, “Settable JMS Header Properties”.
  3. Set the values to the request context by calling the request context's put() method using org.apache.cxf.transports.jms.JMSConstants.JMS_CLIENT_REQUEST_HEADERS as the first argument, and the new JMSMessageHeadersType object as the second argument.

Optional JMS Header Properties

You can also set optional properties to the JMS header. Optional JMS header properties are stored in the JMSMessageHeadersType object that is used to set the other JMS header properties. They are stored as a List object containing org.apache.cxf.transports.jms.context.JMSPropertyType objects. To add optional properties to the JMS header do the following:
  1. Create a JMSPropertyType object.
  2. Set the property's name field using setName().
  3. Set the property's value field using setValue().
  4. Add the property to the JMS message header using JMSMessageHeadersType.getProperty().add(JMSPropertyType).
  5. Repeat the procedure until all of the properties have been added to the message header.

Client Receive Timeout

In addition to the JMS header properties, you can set the amount of time a consumer endpoint waits for a response before timing out. You set the value by calling the request context's put() method with org.apache.cxf.transports.jms.JMSConstants.JMS_CLIENT_RECEIVE_TIMEOUT as the first argument and a long representing the amount of time in milliseconds that you want the consumer to wait as the second argument.


Example 40.15, “Setting JMS Properties using the Request Context” shows code for setting some of the JMS properties using the request context.

Example 40.15. Setting JMS Properties using the Request Context

import org.apache.cxf.transports.jms.context.*;
 // Proxy greeter initialized previously
1InvocationHandler handler = Proxy.getInvocationHandler(greeter);

BindingProvider bp= null;
2if (handler instanceof BindingProvider)
3  bp = (BindingProvider)handler;
4  Map<String, Object> requestContext = bp.getRequestContext();
5  JMSMessageHeadersType requestHdr = new JMSMessageHeadersType();
6  requestHdr.setJMSCorrelationID("WithBob");
7  requestHdr.setJMSExpiration(3600000L);
8  JMSPropertyType prop = new JMSPropertyType;
9  prop.setName("MyProperty");
10  requestHdr.getProperty().add(prop);

11  requestContext.put(JMSConstants.CLIENT_REQUEST_HEADERS, requestHdr);

12  requestContext.put(JMSConstants.CLIENT_RECEIVE_TIMEOUT, new Long(1000));
Gets the InvocationHandler for the proxy whose JMS properties you want to change.
Checks to see if the InvocationHandler is a BindingProvider.
Casts the returned InvocationHandler object into a BindingProvider object to retrieve the request context.
Gets the request context.
Creates a JMSMessageHeadersType object to hold the new message header values.
Sets the Correlation ID.
Sets the Expiration property to 60 minutes.
Creates a new JMSPropertyType object.
Sets the values for the optional property.
Adds the optional property to the message header.
Sets the JMS message header values into the request context.
Sets the client receive timeout property to 1 second.
Red Hat logoGithubRedditYoutubeTwitter


Try, buy, & sell


About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.