HomeDocumentation > Developer's guide > Tutorials > Web applications > Web Application for JMS access

This application is a simple JMS application where in a user sends information to the administrator for updation . As we go through the tutorial we will try to understand the basic 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 3.3.1.1 (Eclipse Classic package of Europa distribution), which is platform specific
  • Web Tools Platform (WTP) 2.0.1
  • Data Tools Platform (DTP) 1.5.1
  • Eclipse Modeling Framework (EMF) 2.3.1
  • Graphical Editing Framework (GEF) 3.3.1

Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the following sections:

The application development will take you through the following

Creating a Dynamic Web Project

  1. Launch Eclipse. Select File->New->Project.





  2. Select Web->Dynamic Web Project. Select Next.





  3. On the next screen give the name of the project as WebJMS.





  4. 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 the Service provider. In our case we will be using Active MQ as the provider.
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.

  1. Start the server and Launch the administrative console.
  2. Under Services. Select JMS Resources.





  3. Under Create a new JMS Resource Group:. Select For ActiveMQ.





  4. On the next screen suggest a Resource Group Name. In our case we are using WebJMS. All other values can be taken as default.





  5. Select Next once done.





  6. Select Add Connection Factory on the next page.





  7. In the drop down box select javax.jms.QueueConnectionFactory. Select Next.





  8. Next give the Connection Factory Name as jms/TestConnectionFactory. Keep default for all other fields. Select Next.





  9. Select Add Destination on the next screen.





  10. Select JMS Destination Type as javax.jms.Queue. Select Next.





  11. Name the Message Destination Name as jms/TestQueue. Select Next.





  12. On the next screen Select Deploy Now. This will deploy the created Plan.





  13. Under JMS resources you can see the newly created connection factory and queue.





Adding producer, consumer and UI code to the application

  1. Right click on WebJMS project and create a new servlet.





  2. Name the servlet as UserServlet and package as webjms. This is the producer in the application.





  3. Select Next and later Finish.





  4. Add the following code to UserServlet.java
    UserServlet.java

    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.
  5. Similarly create a second servlet AdminServlet. This is the consumer in the application.








    Add the following code to AdminServlet.java
    AdminServlet.java
  6. Right Click on WebContent and create a jsp.





  7. Name the jsp as index.jsp. Select Next.





  8. Select Finish.





    Add the following code to index.jsp
    index.jsp
    This will create the producer, consumer and user interface required by the application.

Modifying the deployment plan

Modify the deployment plan as shown below

geronimo-web.xml

The deployment plan has been modified to include active-mq dependencies and resource references for queue(TestQueue) and Connection Factory(Test Connection Factory).
<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

Warning

Icon

Due to some limitation with GEP you need to export the project. This issue will be fixed soon.

  1. Right Click on WebJMS project and select Export->war file.





  2. Browse to a convenient location to save the exported project.





  3. Once done select save. This will save the file as WebJMS.war.





  4. Select Finish.





  5. Start the server and Launch Administrative console.


  6. Under Applications. Select Deploy New.





  7. In the Install New Applications window. Select Browse.





  8. Browse to the directory where the application was exported. Select Open.





  9. Once done. Select Install.





  10. You will get the following message once the application is successfully deployed.





  11. Launch the application using http://localhost:8080/WebJMS. Fill in the required information and Select Submit.





  12. Once your request is successfully sent to administrator you will get the following message.





  13. Once the administrator login he/she will receive the request sent by user. Launch the AdminServlet using the following http://localhost:8080/WebJMS/AdminServlet.