JMS in JBoss AS 6

On March 1, 2011, in JBoss, JBoss 6, JMS, by lucasterdev

JBoss AS 6 is bundled with HornetQ, a Java Messaging service provider.

It already has some ready-to-use queues and connection factories. You can view their list in $JBOSS_HOME/server/default/deploy/hornetq/hornetq-jms.xml.

Here is a basic example of message sender and message receiver. It’s a corrected version of the example you can find in the JMS Tutorial 1.3.

I had the whole JBoss Runtime Library in my classpath.

To run the example, right-click on the .java files and select “Run As” –> “Java Application”.

If you go to http://localhost:8080/admin-console and browse the tree on the left, you’ll find jms.queue.ExpiryQueue. This will open the page relative to the queue used in this example. From there, you can observe the number of messages currently held in this queue awaiting delivery.

Also, keep an eye on Eclipse’s Console. Use the second button from the right (Display Selected Console) To cycle through the outputs of the two programs.

package com.jms.myexample;

import java.util.Properties;

import javax.jms.*;
import javax.naming.*;

public class SimpleQueueReceiver {
	/**
	 * Main method.
	 *
	 * @param args
	 *            the queue used by the example
	 */
	public static void main(String[] args) {

		String queueName = "/queue/ExpiryQueue";
		String connectionFactoryName = "/ConnectionFactory";
		Context jndiContext = null;
		QueueConnectionFactory queueConnectionFactory = null;
		QueueConnection queueConnection = null;
		QueueSession queueSession = null;
		Queue queue = null;
		QueueReceiver queueReceiver = null;
		TextMessage message = null;

		Properties props = new Properties();
		props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming.client");
		props.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
		props.setProperty("j2ee.clientName", "SimpleQueueReceiver"); 

		/**
		 * Display the queue name.
		 */
		System.out.println("Queue name is " + queueName);

		/**
		 * Create a JNDI API InitialContext object if none exists yet.
		 */
		try {
			jndiContext = new InitialContext(props);
		} catch (NamingException e) {
			System.out.println("Could not create JNDI API " + "context: " + e.toString());
			System.exit(1);
		}

		/**
		 * Look up connection factory and queue. If either does not exist, exit.
		 */
		try {
			queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup(connectionFactoryName);
			queue = (Queue) jndiContext.lookup(queueName);
		} catch (NamingException e) {
			System.out.println("JNDI API lookup failed: " + e.toString());
			System.exit(1);
		}

		/**
		 * Create connection.
		 * Create session from connection; false means session is not transacted.
		 * Create receiver, then start message delivery.
		 * Receive all text messages from queue until a non-text message is received indicating end of message stream.
		 * Close connection.
		 */
		try {
			queueConnection = queueConnectionFactory.createQueueConnection();
			queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			queueReceiver = queueSession.createReceiver(queue);
			queueConnection.start();
			while (true) {
				Message m = queueReceiver.receive(1);
				if (m != null) {
					if (m instanceof TextMessage) {
						message = (TextMessage) m;
						System.out.println("Reading message: " + message.getText());
					} else {
						System.out.println("Received end-of-messages message.");
						break;
					}
				}
			}
		} catch (JMSException e) {
			System.out.println("Exception occurred: " + e.toString());
		} finally {
			if (queueConnection != null) {
				try {
					queueConnection.close();
				} catch (JMSException e) {
				}
			}

		}
		System.out.println("Receiver: exiting.");
	}
}
package com.jms.myexample;

/**
 * The SimpleQueueSender class consists only of a main method,
 * which sends several messages to a queue.
 *
 * Run this program in conjunction with SimpleQueueReceiver.
 * Specify a queue name on the command line when you run the
 * program. By default, the program sends one message. Specify
 * a number after the queue name to send that number of messages.
 */
import java.util.Properties;

import javax.jms.*;
import javax.naming.*;

public class SimpleQueueSender {
	/**
	 * Main method.
	 *
	 * @param args
	 *            the queue used by the example and, optionally, the number of
	 *            messages to send
	 */
	public static void main(String[] args) {

		String queueName = "/queue/ExpiryQueue";
		String connectionFactoryName = "/ConnectionFactory";
		final int NUM_MSGS = 5;
		Context jndiContext = null;
		QueueConnectionFactory queueConnectionFactory = null;
		QueueConnection queueConnection = null;
		QueueSession queueSession = null;
		Queue queue = null;
		QueueSender queueSender = null;
		TextMessage message = null;

		Properties props = new Properties();
		props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
		props.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming.client");
		props.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
		props.setProperty("j2ee.clientName", "SimpleQueueSender"); 

		/**
		 * Display the queue name.
		 */
		System.out.println("Queue name is " + queueName);

		/**
		 * Create a JNDI API InitialContext object if none exists yet.
		 */
		try {
			jndiContext = new InitialContext(props);
		} catch (NamingException e) {
			System.out.println("Could not create JNDI API " + "context: " + e.toString());
			System.exit(1);
		}

		/**
		 * Look up connection factory and queue. If either does not exist, exit.
		 */
		try {
			queueConnectionFactory = (QueueConnectionFactory) jndiContext.lookup(connectionFactoryName);
			queue = (Queue) jndiContext.lookup(queueName);
		} catch (NamingException e) {
			System.out.println("JNDI API lookup failed: " + e.toString());
			System.exit(1);
		}

		/**
		 * Create connection.
		 * Create session from connection; false means session is not transacted.
		 * Create sender and text message. Send messages, varying text slightly.
		 * Send end-of-messages message.
		 * Finally, close connection.
		 */
		try {
			queueConnection = queueConnectionFactory.createQueueConnection();
			queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
			queueSender = queueSession.createSender(queue);
			message = queueSession.createTextMessage();
			for (int i = 0; i < NUM_MSGS; i++) {
				message.setText("This is message " + (i + 1));
				System.out.println("Sending message: " + message.getText());
				queueSender.send(message);
			}
			/**
			 * Send a non-text control message indicating end of messages.
			 */
			System.out.println("Sending end-of-messages message");
			queueSender.send(queueSession.createMessage());
		} catch (JMSException e) {
			System.out.println("Exception occurred: " + e.toString());
		} finally {
			if (queueConnection != null) {
				try {
					queueConnection.close();
				} catch (JMSException e) {
				}
			}
		}
		System.out.println("Sender: exiting.");
	}
}
 

Leave a Reply