In this article we would show how to create a queue in JBoss AS7 which uses HornetQ, in this example we would use CLI command to create a queue as well to deploy an EAR file which would consists of both a message sender as well as an MDB which would work as the consumer to consume the messages by the sender code.

We would be using the standalone mode for this article and in JBoss AS 7.0.1 Final version we have a different xml file called standalone-preview.xml which has the all the HornetQ related configuration, but it has been said that the same would be integrated in the standalone.xml file in coming JBoss AS 7.1 version.

Before creating a queue lets create an MDB and a Queue sender program which would be used to send and receive the messages.

Steps to create an MDB & Queue Sender

  1. Create a folder called MDB-3.
  2. In “MDB-3” folder create three folders called MDB3.jar, META-INF, QueueSender.war
  3. In “MDB3.jar” folder create MyMDB.java file and copy the below code in it and compile it as well.
  4. import javax.jms.Message;
    import javax.jms.TextMessage;
    import javax.jms.JMSException;
    import javax.jms.MessageListener;
    import javax.ejb.MessageDriven;
    import javax.ejb.ActivationConfigProperty;
    
    @MessageDriven(activationConfig =
            {
            @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
            @ActivationConfigProperty(propertyName="destination", propertyValue="queue/TestQ")
            })
    
    public class MyMDB implements MessageListener
    {
        public void onMessage(Message message)
        {
            TextMessage textMessage = (TextMessage) message;
            try
           {
                System.out.println("===> MyMDB Received: "+ textMessage.getText());
            }
            catch (JMSException e)
            {
                e.printStackTrace();
            }
        }
    }
    
  5. In “META-INF” folder create application.xml file and copy the below code in it.
  6. <?xml version="1.0" encoding="UTF-8"?>
    <application version="5" xmlns="http://java.sun.com/xml/ns/javaee"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                  http://java.sun.com/xml/ns/javaee/application_5.xsd">
       <module>
           <ejb>MDB3.jar</ejb>
       </module>
       <module>
    	<web>
    	   <web-uri>QueueSender.war</web-uri>
    	   <context-root>QueueSender</context-root>
    	</web>
       </module>
    </application>
    
  7. In “QueueSender.war” folder create WEB-INF folder which has classes folder and web.xml inside it and copy the below code in “web.xml”.
  8. <web-app>
    <servlet>
    	<servlet-name>QueueSenderServlet</servlet-name>
    	<servlet-class>QueueSenderServlet</servlet-class>
    	<load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    	<servlet-name>QueueSenderServlet</servlet-name>
    	<url-pattern>/QueueSenderServlet</url-pattern>
    </servlet-mapping>
    </web-app>
    
  9. Then In “classes” folder create QueueSenderServlet.java file and copy the below code in it and compile it as well
  10. import java.io.*;
    import javax.jms.*;
    import javax.naming.*;
    import javax.servlet.http.*;
    import javax.servlet.ServletException;
    
    public class QueueSenderServlet extends HttpServlet
    {
    	static PrintWriter out;
    	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
     	 {
    		try
    		{
    			out=response.getWriter();
    			InitialContext ic = getInitialContext();
    			init(ic, QUEUE_NAME);
    			sendMsg(this);
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	}
    
    	//*************** Connection Factory JNDI name *************************
    	public final static String CNN_FACTORY="/ConnectionFactory";
    
    	//*************** Connection Factory JNDI name *************************
    	public final static String QUEUE_NAME="queue/TestQ";
    
    	private QueueConnectionFactory qconFactory;
    	private QueueConnection qcon;
    	private QueueSession qsession;
    	private static QueueSender qsender;
    	private Queue queue;
    	private static TextMessage msg;
    
    	public void init(Context ctx, String queueName)throws NamingException, JMSException
    	{
    		qconFactory = (QueueConnectionFactory) ctx.lookup(CNN_FACTORY);
    		qcon = qconFactory.createQueueConnection();
    		qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    		queue = (Queue) ctx.lookup(queueName);
    		qsender = qsession.createSender(queue);
    		msg = qsession.createTextMessage();
    		qcon.start();
    	}
    
    	private static void sendMsg(QueueSenderServlet qs) throws IOException, JMSException
    	{
    		boolean readFlag=true;
    		out.println("");
    		out.println("");
    		out.println("");
    		out.println("<h1>Queue Sender Servlet</h1>");
    		out.println("Following Messages has been sent !!!");
    		out.println("====================================");
    		for(int j=1;j<=3;j++)
    		{
            		msg.setText(""+j); 	// Messages
            		qsender.send(msg); 	// Messages sent
       			out.println("Message Sent = "+j);
    		}
    		out.println("====================================");
    		out.println("");
    		out.println("");
    		out.println("");
    	}
    
    	private static InitialContext getInitialContext() throws NamingException
    	{
    		return new InitialContext();
    	}
    }
    
  11. Once you have done with all this you now just have to create an EAR file out of this, for that you need to be in the MDB-3 folder and run the below command (make sure you have setted the java/bin in your PATH). Note: you would have to give the (.) dot at last as well
  12. MDB-3] jar -cvf MDB-3.ear .
    

Steps for creating a Queue in JBoss AS 7

  1. Now we would have to start our JBoss AS 7 with standalone-preview.xml using –server-config parameter during the start-up of JBoss
  2. bin] ./standalone.sh --server-config=standalone-preview.xml
    
  3. Once the JBoss server is up and running properly then you can run the below CLI command with using the a queue-setup.cli file which would make sure that a new queue TestQ is been created and it would also deploy the MDB-3.ear to the server.
  4. Create a file called queue-setup.cli in the bin folder of JBoss and copy the below content

    connect
    add-jms-queue --name=TestQ --entries=queue/TestQ
    deploy /home/urs/JMS/MDB-3/MDB-3.ear
    

    Run the below command

    bin] ./jboss-admin.sh --file=queue-setup.cli
    

Testing


Once you can successfully created a queue and deployed the application using the above CLI command now its time for testing our newly created queue

  1. Open a browser and hit the below URL (I am supposing that you are running JBoss on default port and IP) and that would send 3 messages to the queue which can be seen on the browser itself
  2. http://localhost:8080/QueueSender/QueueSenderServlet
    

  3. After hitting the above URL you would see the below messages been received by the MDB on the running JBoss server prompt
  4. 14:42:06,398 INFO  [stdout] (Thread-0 (group:HornetQ-client-global-threads-980087547)) ===> MyMDB Received: 1
    14:42:06,403 INFO  [stdout] (Thread-3 (group:HornetQ-client-global-threads-980087547)) ===> MyMDB Received: 2
    14:42:06,403 INFO  [stdout] (Thread-2 (group:HornetQ-client-global-threads-980087547)) ===> MyMDB Received: 3
    
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.