Monday, March 12, 2007

Message Driven Bean in Java EE 5 : Part 2

In a previous post, I described how to implement Messaging in Java EE 5 using annotation. This post is an extension of that post to describe how to implement Messaging in Java EE 5 using a deployment descriptor. To implement this, you can use the same client that was described in the other post. The only change needed is in the MDB part. Here's how to implement a Message Driven bean using the deployment descriptor.
  1. Create the Message driven bean: The message driven bean here is a simple Java class that implements the MessageListener interface. All the configuration will be done in the deployment descriptors.
    package jms;

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

    import org.apache.log4j.Level;
    import org.apache.log4j.Logger;

    public class Messaging3Mdb implements MessageListener {

    static final Logger logger = Logger.getLogger("MDB");

    public Messaging3Mdb() {

    public void onMessage(Message inMessage) {
    TextMessage msg = null;

    try {
    if (inMessage instanceof TextMessage) {
    msg = (TextMessage) inMessage;"MESSAGE BEAN: Message received: " + msg.getText());
    } else {
    logger.warn("Message of wrong type: " + inMessage.getClass().getName());
    } catch (JMSException e) {
    logger.error("MessageBean.onMessage: JMSException: " + e.toString());
    } catch (Throwable te) {
    logger.error("MessageBean.onMessage: Exception: " + te.toString());
  2. The sun-ejb-jar.xml file: This file contains the mapping of the JMS resources.
    <?xml version="1.0" encoding="UTF-8"?>
  • The EJB deployment descriptor:
    <?xml version="1.0" encoding="UTF-8" ?>


  • Environment: This example was implemented on Glassfish v1, Milestone 7

    1. hi Abhi
      i am using servlet to post a message Using JMS i am successfully posting but not able to recieve response from queue
      if i dont want to use message driven beans can i recive response from queue?
      i trying to do this but not recieving response can i have solution for this

    2. This solved my problem. I don't know why but annotations are not working on my netbeans. I'm using netbeans 6.0 with jdk 1.6. I think descriptors are better way to do this. Netbeans has made things very complicated. If there's any error, it's very difficult to find out.

    3. Hii I cant understand why dependencies are not getting injected in Servlets , while JNDI works fine.
      I Even migrated to Glassfish v3 from v2.1 .Can anybody suggest whats the problem


    Popular Posts