Ce contenu n'est pas disponible dans la langue sélectionnée.
42.4. Working with JMS Message Properties
Abstract
42.4.1. Inspecting JMS Message Headers Copier lienLien copié sur presse-papiers!
Getting the JMS Message Headers in a Service Copier lienLien copié sur presse-papiers!
WebServiceContext object, do the following:
- Obtain the context as described in the section called “Obtaining a context”.
- Get the message headers from the message context using the message context's
get()method with the parameterorg.apache.cxf.transports.jms.JMSConstants.JMS_SERVER_HEADERS.
Example 42.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 = "http://cxf.apache.org/hello_world_jms")
public class GreeterImplTwoWayJMS implements HelloWorldPortType
{
@Resource
protected WebServiceContext wsContext;
...
@WebMethod
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 Copier lienLien copié sur presse-papiers!
- Get the response context as described in the section called “Obtaining a context”.
- Get the JMS message header properties from the response context using the context's
get()method withorg.apache.cxf.transports.jms.JMSConstants.JMS_CLIENT_RESPONSE_HEADERSas the parameter.
Example 42.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)
responseContext.get(JMSConstants.JMS_CLIENT_RESPONSE_HEADERS);
...
}
42.4.2. Inspecting the Message Header Properties Copier lienLien copié sur presse-papiers!
Standard JMS Header Properties Copier lienLien copié sur presse-papiers!
| 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() |
Optional Header Properties Copier lienLien copié sur presse-papiers!
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 Copier lienLien copié sur presse-papiers!
Example 42.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 = iter.next();
System.out.println("Property name: "+prop.getName());
System.out.println("Property value: "+prop.getValue());
}
- 1
- Prints the value of the message's correlation ID.
- 2
- Prints the value of the message's priority property.
- 3
- Prints the value of the message's redelivered property.
- 4
- Gets the list of the message's optional header properties.
- 5
- Gets an
Iteratorto traverse the list of properties. - 6
- Iterates through the list of optional properties and prints their name and value.
42.4.3. Setting JMS Properties Copier lienLien copié sur presse-papiers!
Abstract
JMS Header Properties Copier lienLien copié sur presse-papiers!
| Property Name | Property Type | Setter Method |
|---|---|---|
| Correlation ID | string | setJMSCorralationID() |
| Delivery Mode | int | setJMSDeliveryMode() |
| Priority | int | setJMSPriority() |
| Time To Live | long | setTimeToLive() |
- Create an
org.apache.cxf.transports.jms.context.JMSMessageHeadersTypeobject. - Populate the values you want to set using the appropriate setter methods described in Table 42.4, “Settable JMS Header Properties”.
- Set the values to the request context by calling the request context's
put()method usingorg.apache.cxf.transports.jms.JMSConstants.JMS_CLIENT_REQUEST_HEADERSas the first argument, and the newJMSMessageHeadersTypeobject as the second argument.
Optional JMS Header Properties Copier lienLien copié sur presse-papiers!
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:
- Create a
JMSPropertyTypeobject. - Set the property's name field using
setName(). - Set the property's value field using
setValue(). - Add the property 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.
Client Receive Timeout Copier lienLien copié sur presse-papiers!
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 Copier lienLien copié sur presse-papiers!
Example 42.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");
prop.setValue("Bluebird");
10 requestHdr.getProperty().add(prop);
11 requestContext.put(JMSConstants.CLIENT_REQUEST_HEADERS, requestHdr);
12 requestContext.put(JMSConstants.CLIENT_RECEIVE_TIMEOUT, new Long(1000));
}
- 1
- Gets the
InvocationHandlerfor the proxy whose JMS properties you want to change. - 2
- Checks to see if the
InvocationHandleris aBindingProvider. - 3
- Casts the returned
InvocationHandlerobject into aBindingProviderobject to retrieve the request context. - 4
- Gets the request context.
- 5
- Creates a
JMSMessageHeadersTypeobject to hold the new message header values. - 6
- Sets the Correlation ID.
- 7
- Sets the Expiration property to 60 minutes.
- 8
- Creates a new
JMSPropertyTypeobject. - 9
- Sets the values for the optional property.
- 10
- Adds the optional property to the message header.
- 11
- Sets the JMS message header values into the request context.
- 12
- Sets the client receive timeout property to 1 second.