Saturday, February 26, 2011

How to Access JBoss Queue with a Standalone Client

In this post I'm going to talk about how to access a JBoss queue using a standalone client. I'm not going to talk about the basic details of creating a queue that you can find in JBoss documents.

There is two steps to this
  1. Setup the JBoss queue
  2. Setup the standalone client
Lets start with the JBoss queue, lets create a normal queue

<mbean <name="jboss.messaging.destination:service=Queue, name=Queue1" code="org.jboss.jms.server.destination.QueueService">
  <attribute name="JNDIName">queue/Queue1</attribute>
  <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>

Now lets setup the remote client. Make sure the JBoss necessary libraries are in the classpath.
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://");
   return new InitialContext(p);

 now lets setup the queue

    String queueName = "queue/Queue1";
    Context ic = null;
    ConnectionFactory cf = null;
    Connection connection =  null;
    QueueSession queueSession;

    connectionFactory = (ConnectionFactory) ctx.lookup("/ConnectionFactory");
    conn = connectionFactory.createQueueConnection();

    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.

Now lets setup a listener.
    MessageConsumer consumer = queueSession.createConsumer(queue);

You can test the connection by sending simple message
TextMessage message = queueSession.createTextMessage("Hello Sourabh Girdhar");

To receive messages you will need to implement MessageListener interface and override the onMessage()  
public void onMessage(Message msg) {
 TextMessage message = (TextMessage) msg;
 String text = null;
  text = message.getText();
  System.out.println("Message received - " + text);
 }catch (JMSException jme){


No comments:

Post a Comment