JMS using JBOSS

Posted On December 9, 2007

Filed under Uncategorized
Tags: , , , ,

Comments Dropped leave a response

SendRecvClient.java

package com.filler.jmstest;

import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage; 

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger; 

 /**
  * A complete JMS client example program that sends a
  * TextMessage to a Queue and asynchronously receives the
  * message from the same Queue.
  *
  * @author Scott.Stark@jboss.org
  */ 

public class SendRecvClient {
    static Logger log;

    QueueConnection conn;
    QueueSession session;
    Queue que;

    public static class ExListener
        implements MessageListener
    {
        public void onMessage(Message msg) {
            TextMessage tm = (TextMessage) msg;
            try {
                log.info("onMessage, recv text=" + tm.getText());
            } catch(Throwable t) {
                t.printStackTrace();
            }
        }
    }

    public void setupPTP()
        throws JMSException,
               NamingException
    {
        Properties props = new Properties();
        props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
        props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
        props.put("java.naming.provider.url", "localhost:1099");

        Context iniCtx = new InitialContext(props);

        Object tmp = iniCtx.lookup("ConnectionFactory");
        QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;
        conn = qcf.createQueueConnection();
        que = (Queue) iniCtx.lookup("queue/testQueue");
        session = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
        conn.start();
    } 

    public void sendRecvAsync(String text)
        throws JMSException,
               NamingException
    {
        log.info("Begin sendRecvAsync");
        // Setup the PTP connection, session 

        setupPTP();

        // Set the async listener
        QueueReceiver recv = session.createReceiver(que);

        recv.setMessageListener(new ExListener());

        // Send a text msg
        QueueSender send = session.createSender(que);

        TextMessage tm = session.createTextMessage(text);
        send.send(tm);
        log.info("sendRecvAsync, sent text=" + tm.getText());
        send.close();
        log.info("End sendRecvAsync");
    } 

    public void stop()
        throws JMSException
    {
        conn.stop();
        session.close();
        conn.close();
    } 

    public static void main(String args[])
        throws Exception
    {
        log = Logger.getLogger("SendRecvClient");
        log.info("Begin SendRecvClient, now=" + System.currentTimeMillis());
        SendRecvClient client = new SendRecvClient();
        client.sendRecvAsync("A text msg");
        client.stop();
        log.info("End SendRecvClient");
        System.exit(0);
    }
}

log4j.properties

log4j.rootCategory=DEBUG, Console

### The console appender
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Threshold=INFO
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p,%c{1}] %m%n

Addition to /projects/jboss/server/default/deploy/messaging/destinations-service.xml
Add the following mbean child to the root element (server)

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

build.xml

<?xml version="1.0" encoding="UTF-8"?>

<project name="JMS Testing" default="compile" basedir=".">
        <path id="class.path1">
                <fileset dir="/projects/jboss/server/default/lib">
                        <include name="**/*.jar" />
                </fileset>
        </path>

        <path id="class.path2">
                <fileset dir="/projects/jboss/client">
                        <include name="**/*.jar" />
                </fileset>
        </path>

        <path id="class.path3">
                <fileset dir="/projects/jboss/lib">
                        <include name="**/*.jar" />
                </fileset>
        </path>

        <target name="compile">
                <javac srcdir="." destdir="." debug="on" deprecation="on"
                 optimize="off">
                        <classpath>
                                <path refid="class.path1" />
                                <path refid="class.path2" />
                        </classpath>
                        <src path="." />
                        <include name="SendRecvClient.java" />
                </javac>
        </target>

        <target name="run">
                <java classname="com.filler.jmstest.SendRecvClient">
                        <classpath>
                                <path refid="class.path1" />
                                <path refid="class.path2" />
                                <path refid="class.path3" />
                                <pathelement location="." />
                        </classpath>
                </java>
        </target>

</project>

By the way, a sed script to replace the & < > occurrences with &amp; &lt; &gt;

sed 's/\&/\&amp;/g' < input.txt | sed 's/</\&lt;/g' | sed 's/>/\&gt;/g' > output.txt

As a sidenote, the wordpress editor is irritating to use. The visual editor is completely out of control and works as and when it fancies, the code editor has this annoying bug of restoring &amp;amp; to &amp; and likewise when editing a post.

Respond now.