Versions Compared

Key

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

...

  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
    Code Block
    titleUserServlet.java
    borderStylesolid
    package webmdb;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Enumeration;
    import java.util.Properties;
    
    import javax.annotation.Resource;
    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.JMSException;
    import javax.jms.MessageListener;
    import javax.jms.MessageProducer;
    import javax.jms.Queue;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import mdb.AdminMDB;
    
     public class UserServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    	 @Resource(name="DefaultActiveMQConnectionFactory")
    	 private ConnectionFactory connectionFactory;
    	 @Resource(name="SendReceiveQueue")
    	 private Queue queue;
    	 
    	 static final long serialVersionUID = 1L;
       
         public UserServlet() {
    		super();
    	}   	
    	
    	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;
    		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);
    			String s=null;
    			int i;
    			for (i=0; i<paramname.length;i++)
    			{	
    			s=fields+":" + paramname[i];
    			message.setText(s);
    			producer.send(message);
    			}
    			}
    			}
    		catch(Exception e)
    			{
    				e.printStackTrace();
    			}
    	}
    	
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);
    	}   	  	    
    }
    

  10. Let us walkthrough the code
    • @Resource(name="DefaultActiveMQConnectionFactory")- This is a resource injection wherein connection factory has been injected.
    • @Resource(name="SendReceiveQueue")- a queue 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 DefaultActiveMQConnectionFactory
    • 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.

...

  1. In WebMDB project select WebContent/WEB-INF/geronimo-web.xml and modify the content with the one shown below.
    Code Block
    titlegeronimo-web.xml
    borderStylesolid
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ns8:web-app xmlns="http://geronimo.apache.org/xml/ns/deployment-1.2" 
    xmlns:ns2="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2" 
    xmlns:ns3="http://geronimo.apache.org/xml/ns/naming-1.2" 
    xmlns:ns4="http://geronimo.apache.org/xml/ns/j2ee/ejb/openejb-2.0" 
    xmlns:ns5="http://geronimo.apache.org/xml/ns/j2ee/application-2.0" 
    xmlns:ns6="http://geronimo.apache.org/xml/ns/security-2.0" 
    xmlns:ns7="http://java.sun.com/xml/ns/persistence" 
    xmlns:ns8="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1" 
    xmlns:ns9="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0">
        <environment>
            <moduleId>
                <groupId>default</groupId>
                <artifactId>WebMDB</artifactId>
                <version>1.0</version>
                <type>car</type>
            </moduleId>
            <dependencies>
                <dependency>
                    <groupId>default</groupId>
                    <artifactId>MessageDrivenBean</artifactId>
                    <version>1.0</version>
                    <type>car</type>
                </dependency>
            <dependency>
                    <groupId>org.apache.geronimo.configs</groupId>
                    <artifactId>activemq-ra</artifactId>
                    <version>2.1.1</version>
                    <type>car</type>
                </dependency> 
            </dependencies>
        </environment>
        <ns8:context-root>/WebMDB</ns8:context-root>
        <ns3:resource-ref>
            <ns3:ref-name>testfactory</ns3:ref-name>
            <ns3:resource-link>DefaultActiveMQConnectionFactory</ns3:resource-link>
       		</ns3:resource-ref>
    </ns8:web-app>
    
    • <dependency>- Defines the dependency of the application on ActiveMQ and MessageDrivenBean
    • <ns3: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.
  2. In MessageDrivenBean project select META-INF/openejb-jar.xml and modify it as follows
    Code Block
    titleopenejb-jar.xml
    borderStylesolid
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <openejb-jar xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.2" 
    xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.2" 
    xmlns:pkgen="http://www.openejb.org/xml/ns/pkgen-2.0" 
    xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.2" 
    xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.2">
        <sys:environment>
            <sys:moduleId>
                <sys:groupId>default</sys:groupId>
                <sys:artifactId>MessageDrivenBean</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.1</sys:version>
            <sys:type>car</sys:type>
            </sys:dependency>
            </sys:dependencies>
            </sys:environment>
            <enterprise-beans>
              <message-driven>
                <ejb-name>AdminMDB</ejb-name>
        		<nam:resource-adapter>
         		<nam:resource-link>ActiveMQ RA</nam:resource-link>
        		</nam:resource-adapter>
        		
        		<activation-config>
    
         <activation-config-property>
          <activation-config-property-name>
           destination
          </activation-config-property-name>
          <activation-config-property-value>
          SendReceiveQueue
          </activation-config-property-value>
         </activation-config-property>
          <activation-config-property>
           <activation-config-property-name>
            destinationType
           </activation-config-property-name>
           <activation-config-property-value>
            javax.jms.Queue</activation-config-property-value>
           </activation-config-property>
    
          </activation-config>
       			</message-driven>
                </enterprise-beans>
    </openejb-jar>
    



    • <enterprise-beans>- This tag suggests that the usage of EJB's in our application.
    • <message-driven>- This tag suggest the usage of MDB's in our application.
    • <ejb-name>- Name of the ejb.
    • <nam:resource-link>- Link for the resource adapter.
      To learn more on MDB deployment plans refer Deployment + planssection.

      Deploy and Run

  3. Export MessageDrivenBean.jar and WebMDB.war from eclipse as shown in the figure below








  4. Launch Administrative console and deploy the EJB and Web Application using Deploy New.
  5. Once done launch the web application with http://localhost:8080/WebMDB/.\\

  6. Fill the form as suggested in the figure and Select Submit.





  7. Once done check out the command prompt. Message Driven Bean has triggered and it displays the message currently on the queue.