Home JBoss Server JBoss JMS Topic example
30 | 07 | 2010
JBoss 5 AS Book
"JBoss AS 5 development" reviews
Please share your feedback/review with other readers!
Banner
Dashboard
Advertise with Us
Banner
RSS Feed
Java EE 1.6 resources
Login
Sign here for the NewsLetter.



JBoss admin resources
Banner
Java EE 1.6 resources
JBoss howto

How to avoid the 50 seconds start up limit in Eclipse ?

JBoss recipe of the day ...
Read More

How can you solve deployment errors caused by large war/jar/ear files ?

jboss recipe of the day ...
Read More

How do you configure your .war to be deployed after your EJB ?

jboss recipe of the day ...
Read More
JBoss JMS Topic example PDF Print E-mail
Written by Mark S.   
Monday, 11 May 2009 16:11

The following article shows how to create a simple JMS Topic Publisher and Subscriber. This example uses JBoss Initial Context to run, however it can be easily adapted to any JMS Provider.  

At first you need to deploy a JMS Topic to JBoss. Create a file ending with -service.xml in the deploy folder of JBoss.

Case 1: JBoss Messaging
If you are running JBoss 5, which ships with JBoss messaging you can use the following descriptor:
 

<mbean code="org.jboss.jms.server.destination.TopicService"
      name="jboss.messaging.destination:service=Topic,name=topicA"
      xmbean-dd="xmdesc/Topic-xmbean.xml">
   <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
   <depends>jboss.messaging:service=PostOffice</depends>
</mbean>   

Case 2: JBoss MQ
If you using JBoss older JMS provider, the configuration file is the following:

<mbean code="org.jboss.mq.server.jmx.Topic"
     name="jboss.mq.destination:service=Queue,name=topicA">
    <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>

Example Code:

package com.sample;


import java.util.Properties;
import java.util.Scanner;

import javax.jms.*;
import javax.naming.Context;
 

public class TopicExample implements MessageListener  
{
   
   public void example() throws Exception
   {
      String destinationName = "topic/topicA";
      
      Context ic = null;
      ConnectionFactory cf = null;
      Connection connection =  null;

      try
      {         
         ic = getInitialContext();
         
         cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
         Topic topic = (Topic)ic.lookup(destinationName);
         
         connection = cf.createConnection();
         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
         MessageProducer publisher = session.createProducer(topic);
         MessageConsumer subscriber = session.createConsumer(topic);
         
         subscriber.setMessageListener(this);
         connection.start();
         
         TextMessage message = session.createTextMessage("Hello!");
         publisher.send(message);
               
 		 Scanner keyIn = new Scanner(System.in); 
 		 
 		 System.out.print("JMS Server listening. Type a Key + CR to exit\n");
	     keyIn.next();
               
      }
      finally
      {         
         if(ic != null)
         {
            try
            {
               ic.close();
            }
            catch(Exception e)
            {
               throw e;
            }
         }
         
         // ALWAYS close your connection in a finally block to avoid leaks.
         // Closing connection also takes care of closing its related objects e.g. sessions.
         closeConnection(connection);
      }
   }
   public synchronized void onMessage(Message message)
   {
      TextMessage text = (TextMessage)message;
      String strMessage = null;
	try {
		strMessage = text.getText();
	} catch (JMSException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
      System.out.println("Message received: "+strMessage);
   }

   private void closeConnection(Connection con)
   {      
      try
      {
         if (con != null)
         {
            con.close();
         }         
      }
      catch(JMSException jmse)
      {
    	  System.out.println("Could not close connection " + con +" exception was " + jmse);
      }
   }
      
   protected boolean isQueueExample()
   {
      return true;
   }
   
   public static void main(String[] args) throws Exception
   {
      new TopicExample().example();
   }
   public static Context getInitialContext( )
	throws javax.naming.NamingException {

		Properties p = new Properties( );
		p.put(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		p.put(Context.URL_PKG_PREFIXES,
		" org.jboss.naming:org.jnp.interfaces");
		p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
		return new javax.naming.InitialContext(p);
	}  
}


JBoss.org Search
Custom Search
Comments
Search
Only registered users can write comments!

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated on Tuesday, 06 July 2010 09:48