JBoss JMS Queue standalone application

JBoss JMS Queue standalone application

In JBoss application server JMS is implemented in all version. Configuration is little different in different version of JBoss. In this tutorial we will configure and create JBoss JMS Queue standalone application where we will be sending message on the queue and will print message once its received.

Note: We will be using jboss-6.1.0.Final version for this demo. If you are using any other version please add JMS detonation using this tutorial

  • Configure JMS destination on JBoss.
  • Go to jboss-6.1.0.Final\server\default\deploy\hornetq\hornetq-jms.xml and below queue:
<queue name="testQueue">
    <entry name="/javahonk/queue"/>
</queue>

JBoss JMS Queue standalone application

  • Create maven project JBossJMS:

JBoss JMS Queue standalone application

 

  • pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.javahonk</groupId>
  <artifactId>JBossJMSQueue</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>JBossJMSQueue</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
  • JBossJMSQueue.java:
package com.javahonk;

import java.util.Properties;
import java.util.Scanner;
 
import javax.jms.*;
 
import javax.naming.Context;

public class JBossJMSQueue implements MessageListener {

	public static void main(String[] args) throws Exception {
		new JBossJMSQueue().example();
	}

	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://localhost:1099");
		return new javax.naming.InitialContext(p);
	}
	
	public void example() throws Exception {
		
		String destinationName = "javahonk/queue";

		Context context = null;
		ConnectionFactory connectionFactory = null;
		Connection connection = null;

		try {
			context = getInitialContext();

			connectionFactory = (ConnectionFactory) context.lookup("/ConnectionFactory");
			Queue queue = (Queue) context.lookup(destinationName);

			connection = connectionFactory.createConnection();
			Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageProducer publisher = session.createProducer(queue);
			MessageConsumer subscriber = session.createConsumer(queue);

			subscriber.setMessageListener(this);
			connection.start();

			TextMessage message = session.createTextMessage("Hello Java Honk!");
			publisher.send(message);

			Scanner keyIn = new Scanner(System.in);

			System.out.print("JBoss JMS Server listening. Type any Key and enter to exit");
			keyIn.next();

		} finally {
			if (context != null) {

				try {
					context.close();
				} catch (Exception e) {
					throw e;
				}
			}

			try {
				if (connection != null) {
					connection.close();
				}
			} catch (JMSException jmse) {
				System.out.println("Could not close connection " + connection
						+ " exception was " + jmse);
			}
		}
	}

	public synchronized void onMessage(Message message) {
		TextMessage text = (TextMessage) message;
		String strMessage = null;
		try {
			strMessage = text.getText();
		} catch (JMSException e) {

			e.printStackTrace();
		}
		System.out.println("Message received: " + strMessage);
	}

	protected boolean isQueueExample() {
		return true;
	}
	
}

Important: If you are running as standalone application you need below Jars in your class path which you will find in “jboss-6.1.0.Final\common\lib” folder:

JBoss JMS Queue standalone application

  • To keep process simple add all JBoss Jars in your class path. Right click project –> Properties –> Add Library

JBoss JMS Queue standalone application

JBoss JMS Queue standalone application

  • Now start the JBoss server. Go to jboss-6.1.0.Final\bin –> Double click run.bat to start the server. Once server is started run the queue. Right click JBossJMSQueue.java –> Run As –> Java Application you will see below output on console:

JBoss JMS Queue standalone application

  • For more information please visit JBoss documentation here

Leave a Reply

Your email address will not be published. Required fields are marked *