Saturday, August 30, 2008

JBoss Messaging

After a few hours of playing with JBoss messaging I have decided that it is a pretty decent product for Open Source developers and companies try to save money. JBoss provides all of the needed JMS features at no cost. Basically my test was to create a JMS server on an existing Ubuntu Linux server I have and use a couple of java clients I wrote to both write and read to the queue from my Apple MacBook Pro. I was able to designate the messages as "Persistent" and even restart the Java JMS provider without loosing the messages. I plan on using this in my next messaging project. If your interested I highly recommend checking it out at JBoss's WebSite. One important note that would have helped me to begin with, you must install a JBoss Application Server as a "Base" foundation for the messaging engine to run in. The following example code will simply take the queue name from the command line.

You will need the following JAR files to establish the connection to the queue:

jbossall-client.jar
jboss-aop-jdk50.jar
jboss-messaging-client.jar
javassist.jar
trove.jar
log4j.jar

Here is the example publish client:


/**
*
*/
package com.thewoodiefamily.jms;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import java.util.Properties;
import javax.naming.InitialContext;

import org.apache.log4j.Logger;


/**
* @author kwoodie
*
*/
public class JMSSender {

static Logger logger = Logger.getLogger(JMSSender.class);

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming rg.jnp.interfaces");
env.put(Context.PROVIDER_URL, "harry:1099");

InitialContext remoteCtx = new InitialContext(env);
String destinationName = "/queue/" + args[0];
Queue queue = (Queue)remoteCtx.lookup(destinationName);
logger.info("Queue " + destinationName + " exists");
ConnectionFactory cf = (ConnectionFactory) remoteCtx.lookup("ConnectionFactory");
Connection connection = null;

try
{
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer sender = session.createProducer(queue);
TextMessage message = session.createTextMessage("Hello!" + Math.random());
message.setJMSExpiration(0);
message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
connection.start();
sender.send(message);
System.out.println("The " + message.getText() + " message was successfully sent to the " + queue.getQueueName() + " queue");

}
finally
{
if (connection != null)
{
connection.close();
}
}

} catch (Exception e) {
e.printStackTrace();
}

}

}



And here is the consumer client:

/**
*
*/
package com.thewoodiefamily.jms;

import javax.jms.QueueConnection;
import javax.jms.QueueReceiver;
import javax.jms.QueueConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.naming.Context;
import java.util.Properties;
import javax.naming.InitialContext;

import org.apache.log4j.Logger;

/**
* @author kwoodie
*
*/
public class JMSConsumer {

static final Logger logger = Logger.getLogger(JMSConsumer.class);

public static class ExListener implements MessageListener
{
public void onMessage(Message msg)
{

TextMessage tm = (TextMessage) msg;
try {
logger.info("onMessage, recv text=" + tm.getText());
} catch(Throwable t) {
t.printStackTrace();
}
}
};


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
QueueConnection connection = null;
try {

Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming rg.jnp.interfaces");
env.put(Context.PROVIDER_URL, "harry:1099");

InitialContext remoteCtx = new InitialContext(env);

String queueName = "/queue/" + args[0];
Queue queue = (Queue)remoteCtx.lookup(queueName);

logger.info("Queue " + queueName + " exists");
QueueConnectionFactory cf = (QueueConnectionFactory) remoteCtx.lookup("ConnectionFactory");
connection = cf.createQueueConnection();
connection.start();
QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

QueueReceiver consumer = session.createReceiver(queue);
consumer.setMessageListener(new ExListener());
while(true) {
Thread.sleep(5000);
System.out.println("waiting");

}

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}

}

No comments: