Java Quickly

Just another WordPress.com weblog

JBoss and JMS

Introduction

The JMS specification describes a set of programming interfaces that support distributed, enterprise messaging. An enterprise messaging systems enables independent distributed components or applications to interact through messages.

In the messaging world, messages are not sent directly to other applications. Instead, messages are sent to destinations, known as queues or topics.

JMS specification defines two Messaging paradigms:

  • Point To Point (PTP)
    • It uses the message Queue concept.
    • Messages are stored in a Queue destination.
    • The message has only one consumer.
  • Publish Subscribe:
    • It uses a Topic to send and receive messages.
    • Each message can have multiple consumers.

Administrator is responsible for defining the destination Queue/Topic with their JNDI names.

JMS Application Components

JMS Clients

Java program that send / receive Messages. Clients can be standalone java applications or Enterprise Java Beans capable of receiving messages asynchronously (Message Driven beans)

Messages

Administered Objects

These are objects created on the Application server. They include the Connection Factory, the topic or queue destinations.

JMS Provider

Known as a Message Oriented Middleware (MOM) system.

JBoss comes with a JMS 1.1 compliant JMS provider called JBoss Messaging or JBossMQ

Setup JMS Connections in JBoss

A connection is needed to the destination Queue/Topic, this can be done using JNDI lookup, but we need first to get the initial context which is the entry point in any JNDI lookup operation.

Hashtable environment = new Hashtable();

environment.put(Context.INITIAL_CONTEXT_FACTORY,

“org.jnp.interfaces.NamingContextFactory”);

environment.put(Context.URL_PKG_PREFIXES,

“org.jboss.naming:org.jnp.interfaces”);

environment.put(Context.PROVIDER_URL, “jnp://localhost:1099″);

InitialContext iniCtx = new InitialContext(environment);

The next step is to create a connection to the destination Queue/Topic

Object tmp = iniCtx.lookup(“ConnectionFactory”);

Then depending on whether we want a Queue or Topic destination we type cast the connection factory object was lookup to either QueueConnectionFactory or TopicConnectionFactory object.

QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;

javax.jms.QueueConnection = qcf.createQueueConnection();

javax.jms.Queue = (Queue) iniCtx.lookup(“queue/testQueue”);

javax.jms.QueueSession = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

Note that Queues/Topics should be prefixed with queue or topic words.

Create a JMS sender

The Session interface defines different types of messages that can be sent.

PTP Sender

Javax.jms.QueueSender = session.createSender(javax.jms.Queue);

October 17, 2008 - Posted by wzedan | JBoss | | No Comments Yet

No comments yet.

Leave a comment