Java Reference
In-Depth Information
NetBeans generates a private variable called myQueue of the type javax.jms.Queue .
This variable is annotated with the @Resource annotation, which binds the myQueue
variable to the JMS queue we created in the previous section.
NetBeans also adds a private variable named context of the type JMSContext . This
variable is annotated with the @Inject annotation, which results in the application
server (GlassFish, in our case) injecting an instance of JMSContext to this variable
at runtime. The context variable is also annotated with @JMSConnectionFactory ,
which binds the context variable to a JMS connection factory.
In previous versions of Java EE, we had to create a JMS connection
factory in addition to a JMS destination. Java EE 7 introduces a default
connection factory that we can use in our JMS code. The NetBeans
generated code takes advantage of this new Java EE 7 feature.
Finally, NetBeans adds a method to actually send the JMS message to the queue.
The name of the generated method depends on the name of the queue. In our
case, we named our queue myQueue ; therefore, NetBeans named the method
sendJMSMessageToMyQueue() .
This method uses the simplified JMS 2.0 API added in Java EE 7. The body of the
method invokes the createProducer() method of the generated JMSContext
instance. This method returns an instance of javax.jms.JMSProducer . Then, the
method invokes the send() method of JMSProducer ; this method sends the message
to the queue. The first parameter of the send() method is the JMS destination that
will receive the message; the second parameter is a string that contains the message.
There are several message types we can send to a JMS queue (all standard JMS
message types are described later in the chapter). The most common message type
is javax.jms.TextMessage . In previous versions of the JMS API, we had to
explicitly use this interface to send JMS messages containing simple strings. The
new JMS 2.0 API generates an instance of a class implementing this interface
behind the scenes when we send a string as a message; this simplifies our work
as application developers.
After generating the JMS code with a few simple clicks, we need to add a few simple
modifications to our code. To do this, simply invoke the generated code with the
message we wish to send:
package com.ensode.jmsintro;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
Search WWH ::

Custom Search