The Celtix Enterprise 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.
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.
To get the JMS message header properties from the WebServiceContext
do the following:
Declare a protected WebServiceContext
field in your service's implementation class and annotate it with the JAX-WS @Resource
annotation.
Get the message context from the WebServiceContext
object using its getMessageContext()
method.
Get the JMS 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 8.3, “Getting JMS Message Headers in a Service” shows code for getting the JMS message headers from a service's message context:
Example 8.3. Getting JMS Message Headers in a Service
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 = "http://cxf.apache.org/hello_world_jms") public class GreeterImplTwoWayJMS implements HelloWorldPortType { @Resource protected WebServiceContext wsContext; ... public String greetMe(String me) { MessageContext mc = wsContext.getMessageContext(); JMSMessageHeadersType headers = (JMSMessageHeadersType) mc.get(JMSConstants.JMS_SERVER_HEADERS); ... } ... }
Once a message has been successfully retrieved from the JMS transport you can inspect the JMS header properties using the consumer's response context. In addition, you can see how long the client will wait for a response before timing out.
To get the JMS message headers from a consumer's response context do the following:
Get the proxy's invocation handler using the proxy's getInvocationHander()
method.
If the invocation handler is an instance of BindingProvider
, cast the invocation handler to a BindingProvider
object.
Get the response context using the binding provider's getResponseContext()
method.
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.
Cast the returned object to org.apache.cxf.transports.jms.context.JMSMessageHeaderType
.
Example 8.4, “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 8.4. Getting the JMS Headers from a Consumer Response Header
import org.apache.cxf.transports.jms.context.*; // Proxy greeter initialized previously InvocationHandler handler = Proxy.getInvocationHandler(greeter); // Invoke on greeter proxy BindingProvider bp= null; if (handler instanceof BindingProvider) { bp = (BindingProvider)handler; Map<String, Object> responseContext = bp.getResponseContext(); JMSMessageHeadersType responseHdr = (JMSMessageHeadersType) responseContext.get(JMSConstants.JMS_CLIENT_REQUEST_HEADERS); ... }
The code in Example 8.4, “Getting the JMS Headers from a Consumer Response Header” does the following:
Table 8.2, “JMS Header Properties” lists the standard properties in the JMS header that you can inspect.
Table 8.2. JMS Header Properties
Property Name | Property Type | Getter 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 Live | long |
getTimeToLive()
|
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 8.5, “Reading the JMS Header Properties” shows code for inspecting some of the JMS properties using the response context.
Example 8.5. Reading the JMS Header Properties
// JMSMessageHeadersType messageHdr retrieved previously System.out.println("Correlation ID: "+messageHdr.getJMSCorrelationID()); System.out.println("Message Priority: "+messageHdr.getJMSPriority()); System.out.println("Redelivered: "+messageHdr.getRedelivered()); JMSPropertyType prop = null; List<JMSPropertyType> optProps = messageHdr.getProperty(); Iterator<JMSPropertyType> iter = optProps.iterator(); while (iter.hasNext()) { prop = iter.next(); System.out.println("Property name: "+prop.getName()); System.out.println("Property value: "+prop.getValue()); }
The code in Example 8.5, “Reading the JMS Header Properties” does the following:
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 will need to reset them each time you invoke an operation on the service proxy.
Note | |
---|---|
You cannot set header properties in a service. |
Table 8.3, “Settable JMS Header Properties” lists the properties in the JMS header that you can set using the consumer endpoint's request context.
Table 8.3. Settable JMS Header Properties
Property Name | Property Type | Setter Method |
---|---|---|
Correlation ID | string |
setJMSCorralationID()
|
Delivery Mode | int |
setJMSDeliveryMode()
|
Priority | int |
setJMSPriority()
|
Time To Live | long |
setTimeToLive()
|
To set these properties do the following:
Create an org.apache.cxf.transports.jms.context.JMSMessageHeadersType
object.
Populate the values you wish to set using the appropriate setter methods from Table 8.3, “Settable JMS Header Properties”.
Set the values into 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.
You can also set optional properties into 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
of org.apache.cxf.transports.jms.context.JMSPropertyType
. To add optional properties to the JMS header do the following:
Create a JMSPropertyType
object.
Set the property's name field using setName()
.
Set the property's value field using setValue()
.
Add the property to the JMS message header to the JMS message header using JMSMessageHeadersType.getProperty().add(JMSPropertyType)
.
Repeat the procedure until all of the properties have been added to the message header.
In addition to the JMS header properties, you can set the amount of time a consumer endpoint will wait 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 to consumer to wait as the second argument.
Example 8.6, “Setting JMS Properties using the Request Context” shows code for setting some of the JMS properties using the request context.
Example 8.6. Setting JMS Properties using the Request Context
import org.apache.cxf.transports.jms.context.*; // Proxy greeter initialized previously InvocationHandler handler = Proxy.getInvocationHandler(greeter); BindingProvider bp= null; if (handler instanceof BindingProvider) { bp = (BindingProvider)handler; Map<String, Object> requestContext = bp.getRequestContext(); JMSMessageHeadersType requestHdr = new JMSMessageHeadersType(); requestHdr.setJMSCorrelationID("WithBob"); requestHdr.setJMSExpiration(3600000L); JMSPropertyType prop = new JMSPropertyType; prop.setName("MyProperty"); prop.setValue("Bluebird"); requestHdr.getProperty().add(prop); requestContext.put(JMSConstants.CLIENT_REQUEST_HEADERS, requestHdr); requestContext.put(JMSConstants.CLIENT_RECEIVE_TIMEOUT, new Long(1000)); }
The code in Example 8.6, “Setting JMS Properties using the Request Context” does the following: