|Accessing JDBC in Web applications||Developing Web applications||Developing a Hello World Web application|
This application is a simple JMS application wherein a user sends information to the administrator for upgrade. As we go through the tutorial we will try to understand the basics of Servlets and JMS.
To run this tutorial, as a minimum you will be required to have installed the following prerequisite software:
- Sun JDK 5.0+ (J2SE 1.5)
- Eclipse IDE for Java EE Developers, which is platform specific
- Apache Geronimo Eclipse Plugin 2.1.x
- Apache Geronimo Server 2.1.x
Geronimo version 2.1.x, Java 1.5 runtime, and Eclipse Ganymede are used in this tutorial but other versions can be used instead (e.g., Geronimo version 2.2, Java 1.6, Eclipse Europa)
Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:
Creating a dynamic Web Project
- Launch Eclipse. Select File -> New -> Project.
- Select Web -> Dynamic Web Project. Select Next.
- On the next screen give the name of the project as WebJMS.
- Select default values for all other fields. Finally select Finish.
Creating Connection Factory and Destination
In simple terms a Connection Factory is an object which is used by a client to connect to a Service provider. In our case we will be using Active MQ as the provider. A Destination is an object which is used by client to provide the target to messages produced and source of the messages to be consumed. In our case the target is going to be a queue.
Let us see how we can use the administrative console to create a Connection Factory and Message Destination.
- Start the server and Launch the administrative console.
For this tutorial you should start the server and launch the administrative console outside of Eclipse (due to limitations with the Eclipse web browser)
- Select JMS Resources under Services.
- Under Create a new JMS Resource Group: select For ActiveMQ.
- On the next screen enter a Resource Group Name. In our case we are using WebJMS. All other values can be taken as default.
- Select Next once done.
- Select Add Connection Factory on the next page.
- In the drop down box select javax.jms.QueueConnectionFactory. Select Next.
- Next give the Connection Factory Name as jms/TestConnectionFactory. Keep default for all other fields. Select Next.
- Select Add Destination on the next screen.
- Select JMS Destination Type as javax.jms.Queue. Select Next.
- Name the Message Destination Name as jms/TestQueue. Select Next.
- On the next screen Select Deploy Now. This will deploy the created plan.
- Under JMS resources you can see the newly created connection factory and queue.
Adding producer, consumer and UI code to the application
- Right click on WebJMS project and create a new servlet.
- Name the servlet as UserServlet and package as webjms. This is the producer in the application.
- Select Next and later Finish.
- Add the following code to
Lets try to understand the code:
- @Resource(name="jms/TestConnectionFactory")- This is a resource injection wherein connection factory has been injected.
- @Resource(name="jms/TestQueue")- a queue previously created has been injected. Resource injection is useful in the sense that we need not include the entries in a deployment descriptor.
- Servlets follow a request response model wherein a request is send to servlet and a response is generated. The function protected void doGet(....,....) follows a request response model.
- PrintWriter out = response.getWriter()- This statement returns a PrintWriter object which is used to send HTML content to client page.
- connection = connectionFactory.createConnection()- Creates a connection to jms/TestConnectionFactory
- Session session = connection.createSession(..,..)- A session is a context for producing and consuming messages. Use this statement we create a new session.
- MessageProducer producer = session.createProducer(queue)- A client uses message producer to send messages to a destination. Queue name is passed to createProducer method provided by session object.
- TextMessage message = session.createTextMessage()- A TextMessage is used to send a message of java.lang.String type.
- Similarly create a second servlet AdminServlet. This is the consumer in the application.
Add the following code to AdminServlet.java:
- Right click on WebContent and create a JSP.
- Name the JSP as
index.jsp. Select Next.
- Select Finish.
Add the following code to
index.jsp: This will create the producer, consumer and user interface required by the application.
Modifying the deployment plan:
Modify the geronimo-web.xml deployment plan as shown below:
The deployment plan has been modified to include active-mq dependencies and resource references for queue TestQueue and Connection Factory Test Connection Factory. You may have to update version number of each dependency to comply with the Geronimo version you are using.
<sys:dependency> – Defines the dependency of the application on ActiveMQ.
<nam:resource-ref> – This tag is basically used to define connections that can be JDBC Connection, Java Mail connection or JMS Connection Factory. In our case we are using it for JMS Connection Factory.
<nam:resource-env-ref> – This tag is basically used to define a resource. In our case we have defind the message destination that is the TestQueue.
Deploy and Run
- Right click on WebJMS project and select Run as - > Run on server.
- Launch the application using http://localhost:8080/WebJMS. Fill in the required information and select Submit.
- Once your request is successfully sent to the administrator you will get the following message.
- Once the administrator logs in they will receive the request sent by the user. Launch the AdminServlet using the following http://localhost:8080/WebJMS/AdminServlet.