October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Building Messaging Systems with JMS

  • June 17, 2004
  • By Olexiy & Alexander Prokhorenko
  • Send Email »
  • More Articles »

When working with EJB 2.0

It's necessary to note the following:

In the new EJB 2.0 specification, integration with JMS was done with the help of creating a new EJB type: the Message Driven Bean (MDB). The feature of MDB is that the client does not communicate with it with a remote interface. The only way of interaction is through message sending. MDB is just MessageListener, and nothing else: a class implements javax.ejb.MessageDrivenBean and javax.jms.MessageListener. The first of these interfaces has only two methods: setMessageDrivenContext() and ejbRemove(). The second interface has only one method: onMessage(). The specification also requires the creation of a ejbCreate() method without parameters. The client does not communicate directly with MDB; it does not create it. The container itself will decide when and how many MDBs are required to process messages from the given destination. The main shortcoming of MDB is that it can accept messages only from one destination.

Code Example

We suppose that you already have installed J2SE (which can be found at http://java.sun.com/j2se/), and have installed and are running JBoss Application Server (which can be found at http://www.jboss.org/).

To compile the following example, you need to type:

javac -classpath .;C:\jboss-3.2.3\client\jbossall-client.jar
      SimpleSender.java

To run it, you need to type:

java -classpath .;C:\jboss-3.2.3\client\jbossall-client.jar
     SimpleSender

(Please type, instead of C:\jboss-3.2.3, the path to your JBoss directory. Also, remember that JBoss server needs to be running before you can run these examples.)

So, let's create a simple sender and receiver.

// SimpleSender.java
import java.util.Properties;
import javax.jms.*;
import javax.naming.*;

public class SimpleSender {

  public static void main(String argv[]) {
    new SimpleSender();
  }

  public SimpleSender() {
    try {
      QueueConnectionFactory myQConnFactory;
      Queue myQueue;

      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY,
                     "org.jnp.interfaces.NamingContextFactory");
      properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
      properties.put(Context.PROVIDER_URL, "localhost");

      Context ctx = new InitialContext(properties);
 
      myQConnFactory = (QueueConnectionFactory)ctx.lookup
                       ("UIL2ConnectionFactory");
      myQueue = (Queue) ctx.lookup("queue/testQueue");

      ctx.bind ("SimpleSender", myQueue);

      QueueConnection con = myQConnFactory.createQueueConnection();
      QueueSession session = con.createQueueSession(false,
                             Session.AUTO_ACKNOWLEDGE);
      TextMessage textMessage = session.createTextMessage();
      QueueSender sender = session.createSender(myQueue);
      con.start();

      for (int i=0; i<10; i++) {
        textMessage.setText("Hello World #" + i);
        sender.send(textMessage);
      }

      con.close();
      ctx.close();

    } catch(Exception e) {
      e.printStackTrace();
    }

  }
}

There are two ways to receive messages. The first one is a synchronous request of messages from a queue, using a receive() method of javax.jms.QueueReceiver. This can block Reciever until it will not receive the message, or can return management on a time-out if the message does not arrive during the specified time interval. The second one is an asynchronous reception of messages as soon as they become accessible, with javax.jms.MessageListener calling the onMessage() method, which processes the message contents.

Many steps in creating receiver are similar to those for sender.

// SyncReceiver.java
import java.util.Properties;
import javax.jms.*;
import javax.naming.*;

public class SyncReceiver {

  public static void main(String argv[]) {
    new SyncReceiver();
  }

  public SyncReceiver() {
    try {
      QueueConnectionFactory myQConnFactory;
      Queue myQueue;

      Properties properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY,
                     "org.jnp.interfaces.NamingContextFactory");
      properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
      properties.put(Context.PROVIDER_URL, "localhost");

      Context ctx = new InitialContext(properties);
      myQConnFactory = (QueueConnectionFactory)
                       ctx.lookup("UIL2ConnectionFactory");
      myQueue = (Queue) ctx.lookup("queue/testQueue");

      ctx.bind("SyncReceiver", myQueue);

      QueueConnection con = myQConnFactory.createQueueConnection();
      QueueSession session = con.createQueueSession(false,
                             Session.AUTO_ACKNOWLEDGE);
      QueueReceiver receiver = session.createReceiver(myQueue);

      con.start();

      for (int i=0; i<10; i++) {
        TextMessage textMessage = (TextMessage) receiver.receive();
        System.out.println("Got: " + textMessage.getText());
      }

      con.close();
      ctx.close();

    } catch(Exception e) {
      e.printStackTrace();
    }

  }
}




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel