Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

A Producer Web Client Application

A web producer client works like the stand-alone client. It can send a message to a queue or publish a message to a topic. In this example, I demonstrate how you can use the JMS API in a web component. You can optimize this sample application by separating the business logic layer and the presentation layer with standard JavaBeans.

When you run the web client in Listing 1, the JSP application sends a message to the queue type destination "queue_Montreal" and displays some informative messages about the progress of sending the message.

In the JSP part of the client application, the page import directive must include classes for the JMS application such as javax.naming.*, javax.rmi.*, java.util.*, and javax.jms.*.

The JMS part of the application consists of six parts:

  • Creating a JNDI InitialContext object

  • Looking up a connection factory and a queue

  • Creating a connection, or session

  • Creating a sender and text message

  • Sending the message

  • Closing the connection

Listing 1 The Sender Web Client Source Code

1. <%@page contentType="text/html"%>
2. <%@ page import= "javax.naming.*, javax.rmi.*, java.util.*,
javax.jms.* " %>
3. <html>
4. <head><title>
5. This JSP Page sends a message to the queue without using a bean
6. </title></head>
7. <body>
8. <%
10.	out.println("This JavaServer Page as a web component SENDS a
message to a queue");
11.  out.println("<BR>");
11.	out.println("to demonstrate how you can combine JMS with web
components");
13.  out.println("<BR>");
14.  out.println("<BR>");
15.
16.  // Creating JMS object variables
17.  String        cityQueueName = "queue_Montreal";
18.  Context         jndiContext = null;
19.  QueueConnectionFactory queueConnectionFactory = null;
20.  QueueConnection     queueConnection = null;
21.  QueueSession      queueSession = null;
22.  Queue          queue = null;
23.  QueueSender       queueSender = null;
24.  TextMessage       message = null;
25.
26.  out.println("Queue name is " + cityQueueName);
27.  out.println("<BR>");
28.
29.  // Creating a JNDI InitialContext object
30.  try {
31.   jndiContext = new InitialContext();
32.  } catch (NamingException e) {
33.	out.println("Error while creating JNDI " +
"context: " + e.toString());
34.  }
35.
36.  // Looking up connection factory and queue
37.  try {
38.   queueConnectionFactory = (QueueConnectionFactory)
39.   jndiContext.lookup("QueueConnectionFactory");
40.   queue = (Queue) jndiContext.lookup(cityQueueName);
41.  } catch (NamingException e) {
42.	out.println("JNDI lookup queue failed: " +
e.toString());
43.  }
44.
45.  // Creating connection, session (not transacted)
46.  try {
47.   queueConnection =
48.    queueConnectionFactory.createQueueConnection();
49.   queueSession =
50.   queueConnection.createQueueSession(false,
51.    Session.AUTO_ACKNOWLEDGE);
52.
53.   // Creating sender and text message
54.   queueSender = queueSession.createSender(queue);
54.   message = queueSession.createTextMessage();
55.	message.setText("**SENDING A MESSAGE TO DEMONSTRATE USING JMS
FROM WEB CLIENT**");
56.   out.println("Message to be sent by regular JSP client: ");
57.   out.println("<BR>");
58.   out.println(message.getText());
59.   out.println("<BR>");
60.
61.   // sending the message
62.   queueSender.send(message);
63.   // sending a non-text control message to end sending
64.   queueSender.send(queueSession.createMessage());
65.	out.println("Sending the message to " + cityQueueName + " by a
web client is done");
66.   out.println("<BR>");
67.
68.  } catch (JMSException e) {
69.	out.println("Error while sending the message: " +
e.toString());
70.  } finally {
71.
72.   // closing the connection
73.   if (queueConnection != null) {
74.     try {
75.       queueConnection.close();
76.     } catch (JMSException e) {}
77.   }
78.  }
80. %>
81. </body>
82. </html>

Running a Sender Web Client

To run a JSP file on the JMS server side, you must start the JMS server, and the queue type destination ("queue_Montreal" for this example) must be created administratively. For examples in this article, I use the J2EE Reference Implementation as the JMS server. This article focuses on the two-tier model because the J2EE Reference Implementation contains a JMS server. If you try this example with any other JMS server, please refer to your JMS server administrator and technical documents.

To start the JMS server integrated with J2EE server, type j2ee –verbose at the command prompt. After the J2EE server startup is complete, you are ready to run the JMS client. If you did not create the destination, type j2eeadmin –addJmsDestination queue_Montreal queue at another command prompt.

After you have started the JMS provider and have created the destination successfully, you can call the sample JSP file in Listing 1 from a web browser on your computer. The web component in Listing 1 is a very simple JSP file. It immediately sends a message to the destination whenever your browser calls the JSP on the web server.

This example is for demonstration purposes only. Depending on your project, you can modify and enhance this sample application. For example, you might need to present the user with a JSP page containing an HTML form to capture a message that the user wants to send. When the user submits the form, the JSP page captures the input data in the doPost() (or doGet() wherever applicable) method. The user's input data is then sent by the JSP to the destination as a JMS message. Figure 2 illustrates the result of calling the sample web producing client application.

Figure 2Figure 2 Running a web producer client.

To run a JSP file, you need a web server that supports Java servlets and JSP technology. A J2EE server has a built-in web server that runs JSP files. If you use a JMS provider that does not have a proper web server to run JSP files, use Tomcat, which is a free add-on product for the Apache Web Server. If you do not want to use the Apache Web Server with Tomcat, the Tomcat server itself contains a built-in internal web server.

  • + Share This
  • 🔖 Save To Your Account