Escolar Documentos
Profissional Documentos
Cultura Documentos
In this section, you modify the JSF managed beans (SenderBean and ReceiverBean). You modify SenderBean to
generate JMS code to send a message to the configured JMS Queue,jms/myQueue. You modify ReceiverBean to
retrieve JMS messages from the JMS Queue,jms/myQueue.
1 de 6 28/7/17 12:05
Java EE 7: Creating a JMS 2.0 Producer and Receiver http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/JMS2....
2. In the Send JMS Message dialog box, accept the default values for Project Destinations and Connection
Factory and click OK.
2 de 6 28/7/17 12:05
Java EE 7: Creating a JMS 2.0 Producer and Receiver http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/JMS2....
3. Scroll to the top of the file and verify the resource declarations of the Queue and JMSContext instances.
The following code injects a JMSContext and uses it to send a message. There is no code to create and
close the JMSContext. Instead, we simply declare a field of type JMSContext and add the @Inject
annotation, which tells the container to create the JMSContext when it is needed. An injected
JMSContext is created and closed automatically by the application server. The
@JMSConnectionFactory annotation is used to specify the connection factory. In this case, the default
connection factory is used.
3 de 6 28/7/17 12:05
Java EE 7: Creating a JMS 2.0 Producer and Receiver http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/JMS2....
5. Examine the code in sendJMSMessageToMyQueue. As you can see, the amount of code you have to write
compared to JMS 1.1 is reduced. Here's why:
Instead of creating separate Connection and Session objects, you create a single JMSContext
object.
You do not have to create a TextMessage object and set its body to be the specified string.
Instead, you simply pass the string into the send method. The JMS provider automatically creates a
TextMessage and sets its body to the supplied string.
One feature of the simplified API is that its methods do not declare checked exceptions. If an error
condition is encountered, a JMSRuntimeException is thrown. This new exception is a subclass
of RuntimeException, which means that it does not need to be explicitly caught by the calling
method or declared in its throws clause. Compare this with the classic API, in which almost every
method is declared to throw a JMSException that the calling method must either catch or throw
itself.
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.jms.JMSRuntimeException;
import javax.jms.Queue;
4 de 6 28/7/17 12:05
Java EE 7: Creating a JMS 2.0 Producer and Receiver http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/JMS2....
@Inject
private JMSContext context;
@Resource(lookup = "jms/myQueue")
private Queue queue;
The following code injects a JMSContext and uses it to send a message. There is no code to create and
close JMSContext. Instead, we simply declare a field of type JMSContext and add the @Inject
annotation, which tells the container to create the JMSContext when it is needed. An injected
JMSContext is created and closed automatically by the application server.
if (text != null) {
FacesMessage facesMessage =
new FacesMessage("Reading message: " + text);
FacesContext.getCurrentInstance().addMessage(null,
facesMessage);
} else {
FacesMessage facesMessage =
new FacesMessage("No message received after 1 second");
FacesContext.getCurrentInstance().addMessage(null,
facesMessage);
5 de 6 28/7/17 12:05
Java EE 7: Creating a JMS 2.0 Producer and Receiver http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/JMS2....
System.out.println(t.toString());
}
}
Instead of creating separate Connection and Session objects, we create a single JMSContext
object.
In JMS 1.1 we need to call connection.start() to start delivery of messages to the consumer.
In the JMS 2.0 simplified API, the connection is automatically started.
There's no need to receive a Message object, cast it to a TextMessage, and then call getText to
extract the message body. Instead, we call receiveBody, which returns the message body
directly.
6 de 6 28/7/17 12:05