Setting Jboss 5.0 for standalone JMS consumers/publishers

Most of programmers get chance to work on MOM (Messaging oriented Middleware). Generally all queues,topics etc are maintained by Middleware team or some other team and developers don’t get much chance to play with topics/queues and setup same on their local machines.
In this article I shall explain how to setup JBoss 5.0 on local machine to work as destination for topics or queues.
To setup you need to have Jboss 5.0 , JDK 1.6 onyour machine. You will also need and IDE like Eclipse to write sample programs for consuming and publishing messages.

The Java Messaging Service (JMS) has been implemented by means of different providers in the releases 4.x, 5.x and 6.x of JBoss application server. Setup includes two basic steps

  1. Setup of destinations on JBoss AS server
  2. Setup of JBoss libraries on standalone client

1. Setup of destinations on JBoss AS server

In JBoss 5.0 we need to edit destinations-services.xml in the messaging folder the deploy folder. It may be present in deploy folder in some cases depending on your configuration. Add the following code respectively

For Queue –


<mbean xmbean-dd="xmdesc/Queue-xmbean.xml" name="jboss.messaging.destination:service=Queue,name=SourabhQueue1" code="org.jboss.jms.server.destination.QueueService">
  <attribute name="JNDIName">queue/SourabhQueue1</attribute>
  <depends optional-attribute- name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
  <depends>jboss.messaging:service=PostOffice</depends>
</mbean>

For Topic –

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

2.Setup of JBoss libraries on standalone client

Make sure you add JBoss libraries to your classpath.
How to get the initial context :

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);
  }

Get the Connection factory/Connection and Session

    String queueName = "queue/SourabhQueue1";
    String topicName = "topic/SourabhTopic1";
    Context ic = null;
    ConnectionFactory cf = null;
    Connection connection =  null;
    private Topic topic;
    private TopicSession topicSession;
    private QueueSession queueSession;
................................................................
    connectionFactory = (ConnectionFactory) ctx.lookup("/ConnectionFactory");
    conn = connectionFactory.createQueueConnection();
...............................................................
    queueSession = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    topic = (Topic) ctx.lookup(topicName);
    MessageProducer topicPublisher = topicSession.createPublisher(topic);
..............................................................
    queueSession = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    queue = (Queue) ctx.lookup(queueName);
    MessageProducer queuePublisher = queueSession.createProducer(queue);

You can change the JNDI name of connection factory or you can create your own by editing connection-factories-service.xml in the same directory where you found destinations-services.xml .

You can get subscriber and listen in the same way once you get the session.

    MessageConsumer consumer = queueSession.createConsumer(queue);
    conn.start();
    consumer.setMessageListener(this);
............................................................
    MessageConsumer consumer = topicSession.createConsumer(topic);
    conn.start();
    consumer.setMessageListener(this);

As always a best practice, make sure you close all resources in finally clause. Here you need to close InitialContext and Connection.

You can send and receive simple text messages like

    TextMessage message = queueSession.createTextMessage("Hello Sourabh Girdhar");
    publisher.send(message);

To receive messages, you need to implement MessageListener interface and override the onMessage() method as per JMS specification.

@Override
    public void onMessage(Message msg) {
	TextMessage message = (TextMessage) msg;
	String text = null;
	try{
		text = message.getText();
		System.out.println("Message received - " + text);
	}catch (JMSException jme){
		jme.printStackTrace();
	}
}

In my next posts I shall be explaining about different patterns in Messaging. Keep looking :)

About these ads
This entry was posted in JMS, Middleware and tagged , , . Bookmark the permalink.

5 Responses to Setting Jboss 5.0 for standalone JMS consumers/publishers

  1. sridhar says:

    Amazing Post. Exactly what i needed.

  2. khan says:

    I followed the above instructions and modified the destinations file but when i executed the program its giving the error: JNDI API lookup failed: javax.naming.NameNotFoundException: SourabhQueue1 not bound.

    Can anyone please let me know where to bound SourabhQueue1.

  3. yash says:

    awesome post…:)

  4. Rohit says:

    Great — But there is a typo an extra space while configuring queue .Great post though .
    <depends optional-attribute- name="ServerPeer"

  5. Wow.. great post… just the kind of hands on i was looking for .
    Tried it and everything went smoothly.. Except for the fact that the server should be started before trying this out , otherwise u would get a “Connection Refused” .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s