In this article we would show how to create a Topic in JBoss AS7 which uses HornetQ, in this example we would use CLI command to create a Topic 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.1.0.CR1 version we have a different xml file called standalone-full.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 Topic lets create an MDB and a Topic sender program which would be used to send and receive the messages.

Steps to create an MDB & Topic Sender

  1. Create a folder called MDB-3.
  2. In “MDB-3” folder create three folders called MDB3.jar, META-INF, TopicSender.war
  3. In “MDB3.jar” folder create 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.Topic"),
            @ActivationConfigProperty(propertyName="destination", propertyValue="topic/MyTopic"),
            //************************* RECONNECT PARAMERTERS  *****************************
            @ActivationConfigProperty(propertyName="reconnectAttempts", propertyValue="60"),
            @ActivationConfigProperty(propertyName="reconnectInterval", propertyValue="10")
    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) {
  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=""
  7. In “TopicSender.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>
  9. Then In “classes” folder create file and copy the below code in it and compile it as well
  10. import*;
    import java.util.*;
    import javax.jms.*;
    import javax.naming.*;
    import javax.transaction.*;
    import javax.servlet.http.*;
    import javax.rmi.PortableRemoteObject;
    import javax.servlet.ServletException;
    public class TopicSenderServlet extends HttpServlet 
    	static PrintWriter out;
    	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    			InitialContext ic = getInitialContext();
    			init(ic, TOPIC_NAME);
    		catch(Exception e)
    	//*************** Connection Factory JNDI name *************************
    	public final static String CONN_FACTORY="/ConnectionFactory";
    	//*************** Topic JNDI name *************************
    	public final static String TOPIC_NAME="topic/MyTopic";
    	protected TopicConnectionFactory tconFactory;
    	protected TopicConnection tcon;
    	protected TopicSession tsession;
    	protected static TopicPublisher tpublisher;
    	protected Topic topic;
    	protected static TextMessage msg;
    	public void init(Context ctx, String topicName) throws NamingException, JMSException
    		tconFactory = (TopicConnectionFactory) PortableRemoteObject.narrow(ctx.lookup(CONN_FACTORY),TopicConnectionFactory.class);
    		tcon = tconFactory.createTopicConnection();
    		tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    		topic = (Topic) PortableRemoteObject.narrow(ctx.lookup(topicName), Topic.class);
    		tpublisher = tsession.createPublisher(topic);
    		msg = tsession.createTextMessage();
    	private static void sendMsg(TopicSenderServlet ts) throws IOException, JMSException
    		boolean readFlag=true;
    		out.println("<H1>Topic Sender Servlet</H1>");
    		out.println("Following Messages has been sent !!!<br>");
    		for(int j=1;j<=3;j++) 
            		msg.setText(""+j); 		// Messages 
            		tpublisher.publish(msg); 	// Messages sent
       			out.println("<BR>Message Sent = "+j);
    	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 Topic in JBoss AS 7

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

    add-jms-topic --name=MyTopic --entries=topic/MyTopic
    deploy /home/urs/JMS/MDB-3/MDB-3.ear

    Run the below command

    bin] ./ --file=Topic-setup.cli


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

  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 Topic which can be seen on the browser itself
  2. http://localhost:8080/TopicSender/TopicSenderServlet

  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:32:48,295 INFO  [stdout] (Thread-1 (group:HornetQ-client-global-threads-1623557144)) ===> MyMDB Received: 1
    14:32:48,295 INFO  [stdout] (Thread-0 (group:HornetQ-client-global-threads-1623557144)) ===> MyMDB Received: 2
    14:32:48,296 INFO  [stdout] (Thread-2 (group:HornetQ-client-global-threads-1623557144)) ===> 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.