jms

Simple JMS example on JBoss 5.1

In this example we shall show you how to create a simple hello World example in JMS, using JBoss 5.1. JMS is a very popular API standard for messaging, and most messaging systems provide a JMS API. To create a simple JMS example that produces and consumes a simple hello World message one should perform the following steps:

  • Create a JNDI InitialContext from which to lookup our JMS objects. We are using the InitialContext(Hashtable<?,?> environment) constructor with a Hashtable where we have set the properties needed for connecting to JBoss, such as the initial context factory to use, the service provider that is the localhost, on port 1099 and the package prefix to use when loading in the context factory.
  • Lookup the JMS connection factory from the JBoss 5.1 object store, using lookup(String name) API method of Context.
  • Lookup a queue from the JBoss 5.1 object store, using lookup(String name) API method of Context.
  • Create a connection to the JBoss 5.1 Message Service, using createConnection() API method of javax.jms.ConnectionFactory.
  • Create a non transacted JMS Session, with AUTO_ACKNOWLEDGE acknowledge mode within the connection, using createSession(boolean arg0, int arg1) API method of javax.jms.Connection.
  • Create a message producer to put messages on the queue, with createProducer(Destination arg0) API method of javax.jms.Session.
  • Create a message, with createTextMessage() API method of javax.jms.Session and setText(String arg0) API method of javax.jms.TextMessage and send it to the queue, with send(Message arg) method of javax.jms.MessageProducer.
  • Create a message consumer that will consume orders from the queue, with createConsumer(Destination arg0) API method of javax.jms.Session.
  • Make sure to start the connection, or delivery won’t occur on it. Use start() method of javax.jms.Connection.
  • Receive the message from the queue, using receive() API method of javax.jms.MessageConsumer and get the contents of the message, with getText() API method of javax.jms.TextMessage.
  • Close the session and connection resources, using close() API methods of javax.jms.Session and javax.jms.Connection.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.enterprise;

import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
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 javax.naming.InitialContext;

public class HelloWorldJMS {

    public static void main(String[] args) {

  try {



/*


 * Connecting to JBoss naming service running on local host and on


 * default port 1099 the environment that should be created is like the


 * one shown below :


 */


Hashtable env = new Hashtable();


env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");


env.put(Context.PROVIDER_URL, "jnp://localhost:1099");


env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");





// Create the initial context


Context ctx = new InitialContext(env);





// Lookup the JMS connection factory from the JBoss 5.1 object store


ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
  


// Lookup a queue from the JBoss 5.1 object store


Queue queue = (javax.jms.Queue)ctx.lookup("/queue/DLQ");



// Create a connection to the JBoss 5.1 Message Service


Connection connection = connectionFactory.createConnection();



// Create a session within the connection


Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);



// Create a message producer to put messages on the queue


MessageProducer messageProducer = session.createProducer(queue);



//Create and send a message to the queue


TextMessage textMessage = session.createTextMessage();


textMessage.setText("Hello World");


System.out.println("Sending Message: " + textMessage.getText());


messageProducer.send(textMessage);



// Create a message consumer


MessageConsumer messageConsumer = session.createConsumer(queue);



// Start the Connection created


connection.start();



// Receive a message from the queue.


Message msg = messageConsumer.receive();



// Retrieve the contents of the message.


if (msg instanceof TextMessage) {


    TextMessage txtMsg = (TextMessage) msg;


    System.out.println("Read Message: " + txtMsg.getText());


}



//Close the session and connection resources.


session.close();


connection.close();


  } catch (Exception ex) {


ex.printStackTrace();

  }
    }

}

Output:

Sending Message: Hello World
Read Message: Hello World

 
This was an example of how to create a simple JMS example using JBoss 5.1.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button