Pages

Simple Message Driven Bean(MDB)

Message Driven Bean(MDB) is follows the MOM architecture.There are lot of products available in market like IBM Websphere. MDB(producer) put the message(txt) in the Queue, later the consumer picks up the message when the client server up.Here will see Point to Point (P2P) example.




Step 1:Create a Bean Class. In MDB, there is No home and bean interface.
Bean
package com.bhuvan;

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

/**
 * Message-Driven Bean implementation class for: HelloWorldBean
 *
 */
@MessageDriven( activationConfig = 
            {     @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                  @ActivationConfigProperty(propertyName="destination", propertyValue="queue/DLQ"),
                  @ActivationConfigProperty (propertyName="useDLQ", propertyValue="false"),
                  @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
            }          )
           
public class HelloWorldBean implements MessageListener {

    /**
     * Default constructor. 
     */
    public HelloWorldBean() {
    }
 
 /**
     * @see MessageListener#onMessage(Message)
     */
    public void onMessage(Message message) {
     TextMessage tm=(TextMessage)message;
     System.out.println("-------------------------");
        System.out.println("Received on :");
        try {
   System.out.println(tm.getText());
  } catch (JMSException e) {
   
   e.printStackTrace();
  }
        System.out.println("-------------------------");
        
    }
}
Step 2: Check the Server Queue. Here the server is JBOSS Server. Goto the path c:/dummypath/jboss 5.1 GA/server/default/deploy/messaging and find the destinations-service.xml. Open the file and name the Queue like "DLQ"(something customized)

  <mbean code="org.jboss.jms.server.destination.QueueService"
      name="jboss.messaging.destination:service=Queue,name=DLQ"
      xmbean-dd="xmdesc/Queue-xmbean.xml">
      <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
      <depends>jboss.messaging:service=PostOffice</depends>
   </mbean>

Step 3: Remote Client Application to sends the message to queue.

import java.util.Properties;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

public class Main {
 public static void main(String[] args) {  
 QueueConnection cnn = null;
        QueueSender sender = null;
        QueueSession session = null;
        InitialContext ctx;
        try {
           Properties props = new Properties();
            props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
            props.setProperty("java.naming.factory.url.pkgs","org.jboss.naming");
            props.setProperty("java.naming.provider.url", "127.0.0.1:1099");

            ctx = new InitialContext(props);
            Queue queue = (Queue) ctx.lookup("queue/DLQ");
            QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
            cnn = factory.createQueueConnection();
            session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
            TextMessage msg = session.createTextMessage("Hello World");
            sender = session.createSender(queue);
            sender.send(msg);
            System.out.println("Message sent successfully to remote queue.");
        } catch (Exception e) {
            e.printStackTrace();
        }    
 }
}

Output:
Client Console :
Message sent successfully to remote queue.

Server Console :
-------------------------
Received on: Hello World
-------------------------