Search code examples
jakarta-eeejbejb-3.0weblogic11gmessage-driven-bean

EJB3.0 How to use MDB


Hey, I am new to coding in j2ee. My question is regarding MDB. I have already set up my weblogic 11g server. and the queue has been set up.

I have written the client code which is a java se client that sends a msg to the queue.

import java.util.Hashtable;
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.Context;
import javax.naming.InitialContext;

public class MyMDBClient {

    /**
     * @param args
     */
    public static void main(String[] args) {
         QueueConnection cnn = null;
            QueueSender sender = null;
            QueueSession session = null;
            InitialContext ctx;
            Hashtable ht = new Hashtable();

            try {
                ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
                ht.put(Context.PROVIDER_URL,"t3://localhost:7001");

                ctx = new InitialContext(ht);
                Queue queue = (Queue) ctx.lookup("jms/testQueue");
                QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("jms/connectionFactory");
                cnn = factory.createQueueConnection();
                session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
                TextMessage msg = session.createTextMessage();
                msg.setText("helloworld");
                sender = session.createSender(queue);
                sender.send(msg);

                System.out.println("Message sent successfully to remote queue.");
                session.close(); // this is important.
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}

I can see the message on the queue on my administrative console of weblogic.

however, my mdb on the server end is not invoked.

Can anyone tell me what is wrong. I will prefer the solution to be in java annotations. thank you all for reading this.

here is my server code:

/**
 * import
 *
 */

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import weblogic.ejbgen.*;


/**
 * Message-Driven Bean implementation class for: MyMDB
 *
 */
@MessageDriven(ejbName="mdb", destinationType="javax.jms.queue",destinationJndiName="jms/testQueue"  )
public class mdb implements MessageListener {

    /**
     * Default constructor. 
     */
    public mdb() {
    }

    /**
     * @see MessageListener#onMessage(Message)
     */
    public void onMessage(Message message) {
        TextMessage tmsg = null;
        tmsg = (TextMessage) message; 

        System.out.println("----------------");
        System.out.println("Received message : ");        
        try {
            System.out.println(tmsg.getText());
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

        System.out.println("----------------");        
    }
}

Solution

  • Here is an example of the annotations that I have on an MDB that is working:

    @MessageDriven(
        messageListenerInterface = javax.jms.MessageListener.class,
        name = "EventQueueListenerMDB",
        mappedName = Constants.EVENT_QUEUE_JNDI,
        activationConfig = {
                @ActivationConfigProperty(
                        propertyName = "connectionFactoryJndiName",
                        propertyValue = Constants.CONNECTION_FACTORY_JNDI),
                @ActivationConfigProperty(
                        propertyName = "destinationType",
                        propertyValue = "javax.jms.Queue")
        })
    

    Also, it seems that you have javax.jms.queue not javax.jms.Queue.