Saturday, February 23, 2013

Client connection code for jboss JMS queue connections

If you are here which means either you are stuck with something or you're a good friend of mine and wanted to check out my blog :D :D. Anyways back to business. First of all you should have correct queue details which are configured in jboss. Otherwise this will not work.
First go to the following location: <jboss home>/server/default/deploy/messaging 
then open the connection-factories-service.xml in your favorite editor and if you did not change anything there you should probably have the following JNDI binding details

         <attribute name="JNDIBindings">
         <bindings>
            <binding>/ConnectionFactory</binding>
            <binding>/XAConnectionFactory</binding>
            <binding>java:/ConnectionFactory</binding>
            <binding>java:/XAConnectionFactory</binding>
         </bindings>
      </attribute>
If it's anything other than that note down the connection factory you have. Then go to destinations-service.xml and make sure you have configured queues correctly and note down the JNDIName of queue you want to connect (For this example we'll say it's queue/testQueue).

Thinking all these are configured correctly lets move on to the client code. First you will least need following libraries (You can find all these jar files inside jboss lib folder)

  • concurrent.jar
  • javassist.jar
  • jbossall-client5.jar
  • log4j-boot.jar
  • trove.jar
Following imports will be used in your code,
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Hashtable;

Following is the code to connect to a jboss jms queue
                    Hashtable props = new Hashtable();
        props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        props.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099"); //Add your correct ip and port if anything other than this
        props.put("java.naming.rmi.security.manager", "yes");
        props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");

        Context context = new InitialContext(props);

        QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context.lookup("ConnectionFactory");//Your connection factory if anything other than this
        Queue myOutputQueue = (Queue) context.lookup("queue/testQueue");//Your queue name goes here

        QueueConnection queueConnection = queueConnectionFactory.createQueueConnection();
        QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueSender queueSender = queueSession.createSender(myOutputQueue);
        queueConnection.start();

        TextMessage sendmsg = queueSession.createTextMessage();
        sendmsg.setJMSExpiration(600);
        sendmsg.setText("Testing message");
        queueSender.send(sendmsg);
        queueConnection.stop();

With this you should be able to connect and write a text message to jboss jms queues. 

Welcome to my little world

For a long time I wanted to start a blog of my own, a bit technical one related to software engineering world. Most of the times we come across many hurdles where we would have to google a lot :). And we would overcome those problems by finding solutions from various places. So I thought I would also post things like those which would hopefully be help to someone else.
Feel free to add corrections to any of the things I post here :)......