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:
Post a Comment