Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  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.





    Add the following code to UserServlet.java
    Code Block
    titleUserServlet.java
    borderStylesolid
    package webjms;
    
    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);
    	}   	  	    
    }
    
  4. Similarly create a second servlet AdminServlet. This is the consumer in the application.








    Add the following code to AdminServlet.java
    Code Block
    titleAdminServlet.java
    borderStylesolid
    package webjms;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.annotation.Resource;
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageConsumer;
    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: AdminServlet
     *
     */
     public class AdminServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    	 @Resource(name="jms/TestConnectionFactory")
    		private ConnectionFactory connectionFactory; 
    		
    		@Resource(name="jms/TestQueue")
    		private Queue queue;
    		
    		/* (non-Java-doc)
    		 * @see javax.servlet.http.HttpServlet#HttpServlet()
    		 */
    		public AdminServlet() {
    			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();
    				Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    				MessageConsumer consumer = session.createConsumer(queue);
    				connection.start();
    				out.println("The following information has been received for updation\n");
    				while(true) {
    					Message m = consumer.receive(); 
    					if (m instanceof TextMessage) {
    						TextMessage message = (TextMessage) m;
    						out.println(message.getText());
    					} else {
    						break;
    					} 
    				}
    			} 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);
    		}   	  	    
    	}
    
  5. Right Click on WebContent and create a jsp.





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





  7. Select Finish.





    Add the following code to index.jsp
    Code Block
    titleindex.jsp
    borderStylesolid
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Sample Web Application accessing JMS Resources</title>
    </head>
    <body>
    <form action="/WebJMS/UserServlet">
    <h1>Please enter the updated information</h1>
    <table>
    <tr>
    <td>UserName:</td>
    <td><input type="text" name="username" size="30"></td>
    </tr>
    <tr>
    <td>UserID:</td>
    <td><input type="text" name="userid" size="30"></td>
    </tr>
    <tr>
    <td>Old Address:</td>
    <td><input type="text" name="oldaddress" size="30"></td>
    </tr>
    <tr>
    <td>New Address:</td>
    <td><input type="text" name="newaddress" size="30"></td>
    <tr>
    </table>
    <input type="submit" value="Submit">
    </form>
    </body>
    </html>
    
    This will create the producer, consumer and user interface required by the application.

...

Code Block
titlegeronimo-web.xml
borderStylesolid
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.2" xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1" xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
  <sys:environment>
    <sys:moduleId>
      <sys:groupId>default</sys:groupId>
      <sys:artifactId>WebJMS</sys:artifactId>
      <sys:version>1.0</sys:version>
      <sys:type>car</sys:type>
    </sys:moduleId>
    <sys:dependencies>
            <sys:dependency>
                <sys:groupId>org.apache.geronimo.configs</sys:groupId>
                <sys:artifactId>activemq-ra</sys:artifactId>
                <sys:version>2.1</sys:version>
                <sys:type>car</sys:type>
            </sys:dependency>
        </sys:dependencies>
  </sys:environment>
  <context-root>/WebJMS</context-root>
  <nam:resource-ref>
        <nam:ref-name>jms/TestConnectionFactory</nam:ref-name>
        <nam:pattern>
            <nam:groupId>org.apache.geronimo.configs</nam:groupId>
            <nam:artifactId>activemq-ra</nam:artifactId>
            <nam:version>2.1</nam:version>
            <nam:name>DefaultActiveMQConnectionFactory</nam:name>
        </nam:pattern>
    </nam:resource-ref>
    <nam:resource-env-ref>
        <nam:ref-name>jms/TestQueue</nam:ref-name>
        <nam:pattern>
            <nam:groupId>org.apache.geronimo.configs</nam:groupId>
            <nam:artifactId>activemq-ra</nam:artifactId>
            <nam:version>2.1</nam:version>
            <nam:name>SendReceiveQueue</nam:name>
        </nam:pattern>
    </nam:resource-env-ref>
</web-app>

Deploy and Run

Warning
titleWarning

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.
    Image Added
  2. Browse to a convenient location to save the exported project.
    Image Added
  3. Once done select save. This will save the file as WebJMS.war.
    Image Added
  4. Select Finish.
    Image Added
  5. Start the server and Launch Administrative console.
  6. Under Applications. Select Deploy New.
    Image Added
  7. In the Install New Applications window. Select Browse.
    Image Added
  8. Browse to the directory where the application was exported. Select Open.
    Image Added
  9. Once done. Select Install.
    Image Added
  10. You will get the following message once the application is successfully deployed.
    Image Added
  11. Launch the application using http://localhost:8080/WebJMS. Fill in the required information and Select Submit.
    Image Added
  12. Once your request is successfully sent to administrator you will get the following message.
    Image Added
  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.
    Image Added