Você está na página 1de 6

Java EE 7: Creating a JMS 2.0 Producer and Receiver http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/JMS2....

Java EE 7: Creating a Java Message Service 2.0 Producer and Receiver

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.

Modifying the SenderBean


1. Perform the following steps:

a. Open SenderBean in the editor.


b. Right-click in the bottom of the file, just before the closing brace, to open the NetBeans Code
Generator feature.
c. Select Send JMS Message from the Generate list.

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.

4. Perform the following steps:

a. Scroll to the bottom of the file to see the sendJMSMessageToMyQueue(String messageData)


method.
b. Delete this method and then add the modified method as follows:

public void sendJMSMessageToMyQueue() {


try {
String text = "Message from producer: " + messageText;

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....

new FacesMessage("Sent message: " + text);


FacesContext.getCurrentInstance().addMessage(null,
facesMessage);
} catch (JMSRuntimeException t) {
System.out.println(t.toString());
}
}

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.

Modifying the ReceiverBean


1. Open ReceiverBean in the Editor.

2. Import the following packages.

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....

3. Add the following code after the class declaration:

@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.

4. Add a getMessage method.

public void getMessage() {


try {
JMSConsumer receiver = context.createConsumer(queue);
String text = receiver.receiveBody(String.class, 1000);

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());
}
}

Note: please fix import package errors.

As with sending a message, the amount of code we have written is reduced.

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

Você também pode gostar