Sending and Receiving Messages Using a Simple Web Application (original) (raw)

This section assumes that you are familiar with the basics of JavaServer Faces technology, described in Part IX, "The Web Tier."

The example, websimplemessage, is under the_tut-install_/jms/examples/ directory. It uses sending and receiving Facelets pages as well as corresponding backing beans. When a user enters a message in the text field of the sending page and clicks a button, the backing bean for the page sends the message to a queue and displays it on the page. When the user goes to the receiving page and clicks another button, the backing bean for that page receives the message synchronously and displays it.

Figure 49-2 The websimplemessage Application

Diagram showing a web application in which a managed bean sends a message to a queue, and another managed bean receives the message from the queue.

The websimplemessage Facelets Pages

The Facelets pages for the example are as follows.

The websimplemessage Managed Beans

The two managed beans for the example are as follows.

@JMSDestinationDefinition(  
        name = "java:comp/jms/webappQueue",  
        interfaceName = "javax.jms.Queue",  
        destinationName = "PhysicalWebappQueue")  
@Named  
@RequestScoped  
public class SenderBean {  

The sendMessage method injects a JMSContext (using the default connection factory) and the queue, creates a producer, sends the message the user typed on the Facelets page, and creates a FacesMessage to display on the Facelets page:

@Inject  
private JMSContext context;  
@Resource(lookup = "java:comp/jms/webappQueue")  
private Queue queue;  
private String messageText;  
...  
public void sendMessage() {  
    try {  
        String text = "Message from producer: " + messageText;  
        context.createProducer().send(queue, text);  
        FacesMessage facesMessage =  
                new FacesMessage("Sent message: " + text);  
        FacesContext.getCurrentInstance().addMessage(null, facesMessage);  
    } catch (Throwable t) {  
        logger.log(Level.SEVERE,  
                "SenderBean.sendMessage: Exception: {0}",  
                t.toString());  
    }  
}  
@Inject  
private JMSContext context;  
@Resource(lookup = "java:comp/jms/webappQueue")  
private Queue queue;  
...  
public void getMessage() {  
    try {  
        JMSConsumer receiver = context.createConsumer(queue);  
        String text = receiver.receiveBody(String.class);  
        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);  
        }  
    } catch (Throwable t) {  
        logger.log(Level.SEVERE,  
                "ReceiverBean.getMessage: Exception: {0}",  
                t.toString());  
    }  
}  

Running the websimplemessage Example

You can use either NetBeans IDE or Maven to build, package, deploy, and run the websimplemessage application.

The following topics are addressed here:

Creating Resources for the websimplemessage Example

This example uses an annotation-defined queue and the preconfigured default connection factory java:comp/DefaultJMSConnectionFactory.

To Package and Deploy websimplemessage Using NetBeans IDE

  1. Make sure that GlassFish Server has been started (seeStarting and Stopping GlassFish Server).
  2. From the File menu, choose Open Project.
  3. In the Open Project dialog box, navigate to:
  4. Select the websimplemessage folder.
  5. Click Open Project.
  6. In the Projects tab, right-click the websimplemessage project and select Build.
    This command builds and deploys the project.

To Package and Deploy websimplemessage Using Maven

  1. Make sure that GlassFish Server has been started (seeStarting and Stopping GlassFish Server).
  2. In a terminal window, go to:
tut-install/examples/jms/websimplemessage/  
  1. To compile the source files and package and deploy the application, use the following command:

To Run the websimplemessage Example

  1. In a web browser, enter the following URL:
http://localhost:8080/websimplemessage  
  1. Enter a message in the text field and click Send Message.
    If, for example, you enter "Hello, Duke", the following appears below the buttons:
Sent message: Message from producer: Hello, Duke  
  1. Click Go to Receive Page.
  2. Click Receive Message.
    The following appears below the buttons:
Reading message: Message from producer: Hello, Duke  
  1. Click Send Another Message to return to the sending page.
  2. After you have finished running the application, undeploy it using either the Services tab of NetBeans IDE or the mvn cargo:undeploycommand.