2 Replies Latest reply on Sep 12, 2006 5:45 AM by Johan Persson

    MDB multiple queues

    Johan Persson Newbie

      Hi,

      Is it possible to have an MDB listening for multiple queues?

        • 1. Re: MDB multiple queues
          Elias Ross Master

          You can reuse the same MDB class for multiple destinations. A single MDB object in memory, though, only listens to a queue at a time.

          • 2. Re: MDB multiple queues
            Johan Persson Newbie

            Thanks!

            Now my MDB is partly annotated and partly XML-descriptor driven. Works fine.

            When my MDB runs in two instances is there a concurrency issue when passing incoming messages to the 'next' queue (two mdb instances sending/routing to a single queue) ?

            THIS IS SOURCE MDB:
            package com.mdm.mdb.bean;

            import javax.annotation.Resource;
            import javax.ejb.MessageDriven;
            import javax.ejb.ActivationConfigProperty;
            import javax.jms.Message;
            import javax.jms.TextMessage;
            import javax.jms.MapMessage;
            import javax.jms.ObjectMessage;
            import javax.jms.MessageListener;
            import javax.jms.JMSException;
            import javax.jms.Queue;
            import javax.jms.QueueConnection;
            import javax.jms.QueueConnectionFactory;
            import javax.jms.QueueSender;
            import javax.jms.QueueSession;
            import javax.jms.Session;
            import com.thoughtworks.xstream.XStream;
            import static java.lang.System.out;

            /** Message Listener setup with annotations, override with XML-descriptor. */
            @MessageDriven(activationConfig ={@ActivationConfigProperty(propertyName="destinationType",
            propertyValue="javax.jms.Queue")})
            public class ServiceBrixRoutingMDB implements MessageListener{

            // Dependecy injection.
            @Resource(mappedName="java:ConnectionFactory")
            QueueConnectionFactory connectionFactory;

            // Dependecy injection.
            QueueConnection queueConnection;

            // Dependecy injection.
            @Resource(mappedName="queue/mdm-input")
            Queue mDMInputQueue;

            // Parser for Object to XML marshaller.
            private XStream xstream = null;

            /** This is the entry for the message listener. */
            public void onMessage(Message messageFromServicemix){
            try{
            if(messageFromServicemix instanceof TextMessage){
            sendMesageToMDM();
            }
            }catch(Exception e){
            out.println(e);
            }
            }

            /** Send MapMessage to MDM node. */
            private void sendMesageToMDM() throws JMSException{
            QueueSender qsender = null;
            queueConnection = getConnection();
            QueueSession qsession = queueConnection.createQueueSession(
            false, Session.AUTO_ACKNOWLEDGE);
            TextMessage mdmMessagge = null;
            qsender = qsession.createSender(mDMInputQueue);
            mdmMessagge = qsession.createTextMessage("MESSAGE_FROM_SERVICEMIX");
            qsender.send(mdmMessagge);
            qsession.close();
            queueConnection.close();
            queueConnection = null;
            }

            /** Get QueConnection from JBoss Factory. */
            private QueueConnection getConnection() throws JMSException{
            if (queueConnection == null){
            synchronized(connectionFactory){
            if(queueConnection == null){
            queueConnection = connectionFactory.createQueueConnection();
            }
            }
            }

            return queueConnection;
            }

            /** Get thread safe XStream object for marshall/unmarshall Object/XML. */
            private XStream getXStream(){
            if(xstream == null){xstream = new XStream();}return xstream;
            }
            }

            THIS IS SOURCE EJBJAR-XML:
            <?xml version="1.0" encoding="UTF-8"?>
            <ejb-jar
            xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
            version="3.0">
            Message Driven Bean for Routing from Servicemix to Flowbrix
            <display-name>MDB for routing from Servicemix to Flowbrix</display-name>
            <enterprise-beans>

            <message-driven>
            <ejb-name>CPAssistedRequestMDB</ejb-name>
            <ejb-class>com.smarttrust.mdm.mdb.bean.ServiceBrixRoutingMDB</ejb-class>
            <transaction-type>Bean</transaction-type>
            <message-destination-type>javax.jms.Queue</message-destination-type>
            <activation-config>
            <activation-config-property>
            <activation-config-property-name>acknowledgeMode</activation-config-property-name>
            <activation-config-property-value>AUTO_ACKNOWLEDGE</activation-config-property-value>
            </activation-config-property>
            </activation-config>
            </message-driven>

            <message-driven>
            <ejb-name>CPAssistedReplyMDB</ejb-name>
            <ejb-class>com.smarttrust.mdm.mdb.bean.ServiceBrixRoutingMDB</ejb-class>
            <transaction-type>Bean</transaction-type>
            <message-destination-type>javax.jms.Queue</message-destination-type>
            <activation-config>
            <activation-config-property>
            <activation-config-property-name>acknowledgeMode</activation-config-property-name>
            <activation-config-property-value>AUTO_ACKNOWLEDGE</activation-config-property-value>
            </activation-config-property>
            </activation-config>
            </message-driven>

            </enterprise-beans>
            </ejb-jar>