3 Replies Latest reply on Nov 3, 2009 9:57 AM by Roded Hess

    EJB3.0 MDB example

    Kevin Liu Newbie

      Hello all,

      I'm trying to understand the Message driven bean example here:

      http://www.jboss.org/file-access/default/members/jbossejb3/freezone/docs/tutorial/1.0.7/html/Message_Driven_Beans.html

      I have the example working correctly but I do not understand why I need the file queue-example-service.xml.

      The tutorial states " The queue-example-service.xml file defines the queues for this tutorial".

      Why aren't these queued defined when you define the Message Driven Bean?

      Is there a way to use annotations to define the queues instead of using this xml file?

      Thanks.

        • 1. Re: EJB3.0 MDB example
          jaikiran pai Master
          "kliu" wrote:

          Why aren't these queued defined when you define the Message Driven Bean?


          Queues (or topics) are part of the JMS configurations and are independent of EJB3. MDBs are just one way of listening to such destinations. As such, creation of destinations isn't part of the MDB responsibilities.

           

           

          "kliu" wrote:

          Is there a way to use annotations to define the queues instead of using this xml file?

           

           

          Not that i know of.

          • 2. Re: EJB3.0 MDB example
            Wolfgang Knauf Master

            Hi,

            the only "dynamic" way of creating the queues I know is to tell JBoss to autocreate them: https://jira.jboss.org/jira/browse/JBAS-6013

            Hope this helps

            Wolfgang

            • 3. Re: EJB3.0 MDB example
              Roded Hess Newbie

              You can use Message Driven POJOs.

              import org.jboss.annotation.ejb.DeliveryMode;
              import org.jboss.annotation.ejb.MessageProperties;
              import org.jboss.annotation.ejb.Producer;
              
              @Producer(connectionFactory="java:/JmsXA")
              @MessageProperties(delivery=DeliveryMode.NON_PERSISTENT, timeToLive=0, priority=1)
              public interface MyProcessor {
               public void processSomething(Serializable anyParameter);
              }
              


              and the implementing class:
              import java.io.BufferedInputStream;
              import java.io.File;
              import java.io.FileOutputStream;
              import java.io.InputStream;
              import java.net.URL;
              import java.net.URLConnection;
              import java.util.Collection;
              import java.util.Date;
              
              import javax.ejb.ActivationConfigProperty;
              import javax.ejb.EJB;
              import javax.ejb.TransactionAttribute;
              import javax.ejb.TransactionAttributeType;
              
              import org.jboss.annotation.ejb.Consumer;
              import org.jboss.annotation.ejb.PoolClass;
              import org.jboss.logging.Logger;
              
              import com.bulloons.server.dataCollector.omgili.entities.Document;
              
              @Consumer(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
               @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/MyTasksProcess"),
               @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "8"),
               @ActivationConfigProperty(propertyName = "minSession", propertyValue = "1"),
               @ActivationConfigProperty(propertyName = "maxMessages", propertyValue = "1") })
              @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
              @PoolClass(value = org.jboss.ejb3.StrictMaxPool.class, maxSize = 8 , timeout = (1000 * 60 * 60))
              // 10 minutes waiting for a pooled MDB. * 8 Concurrent Threads allowed,
              public class MyProcessorMDBean implements Downloader {
              
               public void processSomething(Serializable anyParameter){
               //Do the actual processing
               }
              
              }
              


              And a client:

              import java.io.BufferedReader;
              import java.io.InputStreamReader;
              import java.net.URL;
              import java.util.ArrayList;
              import java.util.Collection;
              import java.util.Date;
              import java.util.List;
              import java.util.regex.Matcher;
              import java.util.regex.Pattern;
              
              import javax.annotation.PostConstruct;
              import javax.annotation.Resource;
              import javax.ejb.EJBException;
              import javax.ejb.SessionContext;
              import javax.ejb.Stateless;
              import javax.jms.JMSException;
              import javax.naming.InitialContext;
              
              import org.jboss.ejb3.mdb.ProducerManager;
              import org.jboss.ejb3.mdb.ProducerObject;
              import org.jboss.logging.Logger;
              
              @Stateless
              public class SomeBean implements SomeLocal, SomeRemote {
              
               //Inject A session contet resource from which the JMS Producer will be created.
               @Resource
               private SessionContext sessionCtx;
              
               public void callTheAThreadedProcess() {
               ProducerManager manager = null;
               MyProcessor myProcessor = null;
               try {
               myProcessor = (MyProcessor) sessionCtx.lookup(MyProcessor.class.getName());
               // Make the connection to the JMS Queue as producer
               ProducerObject po = (ProducerObject) myProcessor;
               manager = po.getProducerManager();
               manager.connect();
               String mySeriazableParam= "hello";
               myProcessor. processSomething(mySeriazableParam);
               } catch (Exception e) {
               log.error("Some Error Occured", e);
               } finally {
               try {
               manager.close();
               } catch (JMSException e) {
               log.error(e);
               }
               }
               }
              



              Note that in Jboss 5 queues are not created automatically and need to be defined in: /deply/messaging/destinations-service.xml
              <mbean code="org.jboss.jms.server.destination.QueueService"
               name="jboss.messaging.destination:service=Queue,name=post_2_analysts_finder"
               xmbean-dd="xmdesc/Queue-xmbean.xml">
               <depends optional-attribute-name="ServerPeer">
               jboss.messaging:service=ServerPeer
               </depends>
               <depends>jboss.messaging:service=PostOffice</depends>
               <attribute name="JNDIName">queue/MyTasksProcess </attribute>
               <attribute name="RedeliveryDelay">10000</attribute>
               <attribute name="MaxDeliveryAttempts">3</attribute>
               </mbean>
              


              Find complete example at:
              http://www.commonj.com/blogs/?p=263