June 18, 2011

EJB3 Message-Driven Bean (MDB) - Hello World Example

0) Create an EJB Project and an EAR project according to instructions specified in the following example

1) Create a new Message-Driven Bean class in the ejbModule folder of the EJB Project created in the previous example.


package info.icontraining.mdb;


import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;


@MessageDriven
(activationConfig = {
@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/MDBQueue2"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "AUTO_ACKNOWLEDGE")  })


public class MyMDB implements MessageListener {

   public void onMessage(Message message) {

      System.out.println("onMessage");
      TextMessage textMessage = (TextMessage) message;
      try {
         System.out.println("Received in an EJB3 MDB: " + 
                             textMessage.getText());
      
      } catch(Throwable t) {
         t.printStackTrace();
      }
   }
}


2) The EJB3 Message-Driven Bean (MDB) acts as a JMS consumer only. It cannot be directly accessed from a client. It is given messages delivered to a particular JMS destination such as a queue or topic.

We next create a JMS Client to write messages to a JMS Queue. The JMS provider will asynchronously deliver messages to the MDB by invoking its onMessage() method. This method like a business method of an MDB. The JMS client created below is a standalone Java program.


package info.icontraining.jms.client;


import javax.jms.*;
import javax.jms.Queue;
import javax.naming.*;
import java.util.*;


public class MyMDBSenderClient {


   public static void main(String[] args) {


      QueueConnection conn = null;
      QueueSession session = null;
      Queue que;

      try {
         Hashtable props = new Hashtable();


         props.put(Context.INITIAL_CONTEXT_FACTORY, 
                   "org.jboss.naming.NamingContextFactory");
         props.put(Context.URL_PKG_PREFIXES, 
                   "jboss.naming:org.jnp.interfaces");
         props.put(Context.PROVIDER_URL, "localhost:1099");
         props.put("java.naming.rmi.security.manager", "yes");


         InitialContext iniCtx = new InitialContext(props);


         Object tmp = iniCtx.lookup("ConnectionFactory");
         QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;

         conn = qcf.createQueueConnection();
         que = (Queue) iniCtx.lookup("queue/MDBQueue2");
         session = conn.createQueueSession(false,
                                      QueueSession.AUTO_ACKNOWLEDGE);
         conn.start();


         QueueSender queueSender = session.createSender(que);
         TextMessage message = session.createTextMessage();
         message.setText("Sent from MyMDBSenderClient");
         queueSender.send(message);


         System.out.println("Message sent.");



         queueSender.close();


      } catch (NamingException e) {
         System.out.println(e.toString());
      } catch (JMSException e) {
         System.out.println("JMS Exception");
      } finally {
         if (conn != null) {
            try {
               conn.close();
            } catch (JMSException e) {
               System.out.println("JMS Exception");
            }
         }
      }
   }
}


3) Test the MDB by executing the JMS Client code created in Step-2. Check the output in the server console printed by code in the MDB's onMessage() method.

No comments:

Post a Comment