MDB 3.0 Annotations



the @MessageDriven annotation identifies this object as an MDB and specifies the MDB configuration,
the activationConfig property of the message driven annotation lets you provide messaging-system specific configuration information through an array of ActivationConfigProperty instances. 
@MessageDriven(name = "MessageMDBSample", activationConfig = {
 @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
 @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/MyQueue"),
 @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })

In our case we define the destionationType of our MDB as Queue listening on the "queue/MyQueue".
Last property is the acknowledge Mode  which is set to Auto-acknowledge.

Other EJB 3.0 Annotations



Besides MDB annotations here you can see an interesting example of Resource injection: when the
MDB is initialized a DataSource is injected  using the following setter method:

    @Resource(mappedName= "OracleDS")     public void setDataSource(DataSource dataSource) {         this.dataSource = dataSource;     }

The connection will be then used to insert data in the onMessage method (here we don't use a DAO just to keep the example very simple and short).

We need as well to inject a MessageDrivenContext context in order to rollback the transaction in case of error.


JBoss 4.X users should inject the Datasource using  @Resource(name = "OracleDS")



That's all. With this simple class you can manage asynchronously your transactions, without even
the need for a configuration file.


Packaging your MDB:



packaging your mdb is simply a matter of putting the .class file in a jar archive, that's all !


$ jar cvf mdbExample.jar com



An MDB client:


We add here a simple servlet client which you might use to test your MDB: pack your servlet in a war
file, your ejb in a jar file and put everything in an ear archive.



package com.sample.servlet;
import java.io.*;
import java.util.*;
import javax.jms.*;
import javax.jms.Queue;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class testJMS extends HttpServlet {

Context context = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender queueSender = null;
TextMessage message = null;
boolean esito = true;

public void init() {
       

      try {
           context = new InitialContext();
           getQueueSender();
           context.close();
           } catch(Exception exc) {
           System.out.println(exc.toString());
           esito = false;
      }
}


public void getQueueSender() throws JMSException,NamingException {

    QueueConnectionFactory queueFactory = (QueueConnectionFactory)context.lookup("ConnectionFactory");
        queueConnection = queueFactory.createQueueConnection();
        queueSession = queueConnection.createQueueSession(false,
             javax.jms.Session.AUTO_ACKNOWLEDGE);
        queue = (Queue)context.lookup("queue/MyQueue");
        queueSender = queueSession.createSender(queue);
        message = queueSession.createTextMessage();
}

public void sendMsg(String testo) throws JMSException {
     message.setText(testo);
     queueSender.send(queue, message);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
                               ServletException {
      String whereToForward = null;
      String testo = request.getParameter("text");
      if (text==null) {
          text="dummy message";
      }
        if(esito) {
                    try {
                          sendMsg(text);
                          whereToForward = "success";
                          } catch (JMSException jme) {
                          System.out.println(jme.toString());
                     whereToForward = "failure";
                     }
         } else {
                   whereToForward = "failure";
         }
        System.out.println("Done!");
       
}

public void destroy() {
       try {
            if(queueSender != null)queueSender.close();
            } catch (JMSException jme) {
            System.out.println(jme.toString());
            } finally {
            queueSender = null;
      }
      try {
           if(queueSession != null)queueSession.close();
           } catch (JMSException jme) {
           System.out.println(jme.toString());
           } finally {
           queueSession = null;
      }
      try {
           if(queueConnection != null)queueConnection.close();
           } catch (JMSException jme) {
           System.out.println(jme.toString());
           } finally {
           queueConnection = null;
           }
      }
}

{fcomment}


0
0
0
s2smodern

Related articles available on mastertheboss.com

EJB 3.0 tutorial : Session Beans

Enterprise JavaBeans (EJB) technology is a J2EE technology for de

JBoss EJB 3 BMT

In a Bean Managed Transaction, the code in the EJB or Message Dri

How do you keep your EJB callbacks separated from biz. methods?

JBoss daily recipe

How to set EJB timeout period ?

JBoss recipe of the day

How to add a Selector with MDB 3.0 ?

Message selectors allow an MDB to be more selective about the mes

How to create a MDB 3.0 singleton ?

  EJB3 uses the resource adapter.  When using the resource adapt