Java Reference
In-Depth Information
@Resource(name = "jms/acmeConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(lookup = "jms/Queue")
Queue inboundQueue;
public void sendMessage() {
JMSContext context = connectionFactory.createContext();
StringBuilder message = new StringBuilder();
message.append("Java EE 7 Is the Best!");
context.createProducer().send(inboundQueue, message.toString());
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
FacesMessage.SEVERITY_INFO, "Message sent",
"Message sent"));
When utilizing the simplified API, there are a few shortcuts that can be made, as compared to the standard API,
also known as “Classic.” In comparing the “Classic” versus Simplified APIs, the simplified API enables developers
to produce the same results as the standard API with much less code. A JMSContext object is obtained via a call to
the ConnectionFactory 's createContext method, and it can be used to begin a chain of method invocations that
will result in the sending of a message in just one line of code. To break it down a bit, after the JMSContext has been
obtained, its createProducer method can be called, chaining a call to the send method, passing the Queue and the
message to be sent.
JMS message implementations may vary between the different application server products. However, all JMS
messages types share some common characteristics. For instance, all JMS messages implement the javax.jms.
Message interface. Messages are composed of a header, properties, and a body. The header of a message contains
values that are utilized by clients and providers for routing and identification purposes; properties provide message
filtering; and the body portion of the message carries the actual message content. The message header is used for
linking messages to one another, and a field named the JMSCorrelationID contains this content. Message objects
contain the ability to support application-defined property values. The properties can be set via a construct known as
message selectors, and they are responsible for filtering messages. For more detailed information regarding message
properties, please see the online documentation that can be found at
javax/jms/Message.html . The body varies across the different message types, as listed in Table 10-2 .
Another means of sending messages in JMS involves the creation of a QueueSession to produce a QueueSender .
Although this technique does not utilize the simplified API, we'll show a brief example using JMS 2.0 to demonstrate
how this is performed using resource injection, rather than InitialContext to gain access to server resources. The
following example demonstrates the domain-specific API for point-to-point messaging, and the sending of a message
to a Queue resource.
@Resource(name = "jms/QueueConnFactory")
private QueueConnectionFactory queueConnFactory;
@Resource(lookup = "jms/Queue")
Queue queue;
* Create and send a message using QueueSession
* @throws NamingException
* @throws JMSException
Search WWH ::

Custom Search