You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

This application is a simple JMS application where in a user sends information to the administrator for updation.

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. Name the project as WebMDB. Select Next.





  4. Next window displays the various project facets. Keep the default values and select Next.





  5. Keep the default values for the next window. Select Next->Finish.








  6. Right click on WebMDB project and create a new servlet.





  7. Name the servlet as UserServlet and package as webmdb. This is the producer in the application.





  8. Select Next and later Finish.





  9. Add the following code to UserServlet.java
    UserServlet.java
    package webmdb;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Enumeration;
    
    import javax.annotation.Resource;
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.JMSException;
    import javax.jms.MessageProducer;
    import javax.jms.Queue;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class for Servlet: UserServlet
     *
     */
     public class UserServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    	 @Resource(name="jms/TestConnectionFactory")
    	 private ConnectionFactory connectionFactory;
    	 @Resource(name="jms/TestQueue")
    	 private Queue queue;
    	 static final long serialVersionUID = 1L;
       
       
        /* (non-Java-doc)
    	 * @see javax.servlet.http.HttpServlet#HttpServlet()
    	 */
    	public UserServlet() {
    		super();
    	}   	
    	
    	/* (non-Java-doc)
    	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		PrintWriter out = response.getWriter();
    		if(connectionFactory == null) {
    			out.println("Connection Factory lookup has failed");
    			return;
    		}
    		
    		if(queue == null) {
    			out.println("Queue lookup has failed");
    			return;
    		}
    		
    		
    		
    		Connection connection = null;
    		try {
    			connection = connectionFactory.createConnection();
    			connection.start();
    			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    			MessageProducer producer = session.createProducer(queue);
    			TextMessage message = session.createTextMessage();
    			Enumeration arr=request.getParameterNames();
    			while(arr.hasMoreElements())
    			{
    				String fields= (String)arr.nextElement();
    				String paramname[]=request.getParameterValues(fields);
    				for (int i=0; i<paramname.length;i++)
    				{	
    				String s=null;
    				s=fields+":" + paramname[i];
    				message.setText(s);
    				producer.send(message);
    				}
    				
    			}
    			out.println("Your request has been sent to administrator.");
                //Send a non-text control message indicating end of messages.
                producer.send(session.createMessage());
    		} catch (JMSException e) {
    			e.printStackTrace();
    		} finally {
    			if(connection != null) {
    				try {
    					connection.close();
    				} catch (JMSException e1) { }
    			}
    		}
    	}
    	
    	/* (non-Java-doc)
    	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);
    	}   	  	    
    }
    

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.





  • No labels