0 Replies Latest reply on Sep 14, 2005 11:34 AM by Jack Jack

    JBOSS3.2.3 JMS Queue implementation

    Jack Jack Newbie

      Hi,

      The JBoss 3.2.3 I'm using.
      1. This is my MDB..
      ----------------------------------------------------------------------

      /**
      * This will be covered the @copyright details.
      */
      package test.jms.sample;

      import java.io.StringReader;

      import javax.ejb.MessageDrivenBean;
      import javax.ejb.MessageDrivenContext;
      import javax.ejb.EJBException;
      import javax.jms.JMSException;
      import javax.jms.Message;
      import javax.jms.MessageListener;
      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.InitialContext;
      import javax.naming.NamingException;
      import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;

      import org.w3c.dom.Document;
      import org.w3c.dom.NodeList;
      import org.xml.sax.InputSource;

      /**
      * The XMLMessageQueueBean class.
      * @author: JBOSS Forum
      * @date: 09-12-05
      * @purpose: This MDB transforms the Messages it receives and push it to the Queue.
      * @constraints:
      * @modification
      */
      public class XMLMessageQueueBean implements MessageDrivenBean, MessageListener

      {

      private MessageDrivenContext messageDrivenContext = null;
      private QueueConnection queueConnection;
      private QueueSession queueSession;

      /**
      * The default constructor.
      */
      public XMLMessageQueueBean() {
      }
      /**
      * The call back method.
      * @param messageDrivenContext MessageDrivenContext
      */
      public void setMessageDrivenContext(MessageDrivenContext messageDrivenContext){
      this.messageDrivenContext = messageDrivenContext;
      }
      /**
      * The call back method.
      */
      public void ejbCreate() {
      try {
      setupPTP();
      } catch (Exception exception){
      throw new EJBException("Failed to init MDB", exception);
      }
      }
      /**
      * The call back method.
      */
      public void ejbRemove() {
      this.messageDrivenContext = null;
      try {
      if (this.queueSession != null)
      this.queueSession.close();
      if (this.queueConnection != null)
      this.queueConnection.close();
      } catch (JMSException JMSException){
      JMSException.printStackTrace();
      }
      }
      /**
      * The call back method.\
      * @param message Message
      */
      public void onMessage(Message message){
      try {
      TextMessage textMessage = (TextMessage) message;
      String text = textMessage.getText();
      StringReader stringReader = new StringReader(text);
      InputSource inputSource = new InputSource(stringReader);
      Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputSource);
      //Help
      //what code I will write here to send the message to the DB
      } catch (Throwable throwable) {
      throwable.printStackTrace();
      }
      }

      /**
      * The setupPTP method.
      * @throws JMSException, NamingException.
      */
      private void setupPTP() throws JMSException, NamingException {
      InitialContext initialContext = new InitialContext();
      Object tmp = initialContext.lookup("java:comp/env/jms/XMLMessageQueue");
      QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;
      this.queueConnection = qcf.createQueueConnection();
      this.queueSession = this.queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
      this.queueConnection.start();
      }
      /**
      * The confirmation method to client. [Not used here]
      * @param text String
      * @param dest Queue
      * @throws JMSException.
      */
      private void sendReply(String text, Queue destination) throws JMSException{
      QueueSender sender = this.queueSession.createSender(destination);
      TextMessage textMessage = this.queueSession.createTextMessage(text);
      sender.send(textMessage);
      sender.close();
      }
      }


      2. This is my ejb-jar.xml
      --------------------------------------------------------------------

      <?xml version="1.0"?>

      <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

      <ejb-jar>
      <enterprise-beans>
      <message-driven>
      <ejb-name>XMLMessageQueueBean</ejb-name>
      <ejb-class>test.jms.sample.XMLMessageQueueBean</ejb-class>
      <transaction-type>Container</transaction-type>
      <acknowledge-mode>AUTO_ACKNOWLEDGE</acknowledge-mode>
      <message-driven-destination>
      <destination-type>javax.jms.Queue</destination-type>
      </message-driven-destination>
      <resource-ref>
      <res-ref-name>jms/XMLMessageQueue</res-ref-name>
      <res-type>javax.jms.QueueConnectionFactory</res-type>
      <res-auth>Container</res-auth>
      </resource-ref>
      </message-driven>
      </enterprise-beans>
      </ejb-jar>


      3. This is my jboss.xml
      --------------------------------------------------------------------

      <?xml version="1.0"?>

      <enterprise-beans>
      <message-driven>
      <ejb-name>XMLMessageQueueBean</ejb-name>
      <destination-jndi-name>queue/sendQueue</destination-jndi-name>
      <resource-ref>
      <res-ref-name>jms/XMLMessageQueue</res-ref-name>
      <jndi-name>ConnectionFactory</jndi-name>
      </resource-ref>
      </message-driven>
      </enterprise-beans>


      -------------------------------------------------------------------
      My requirement is that I've to send lots of XML based message, and it will be maintained in a queue and the message will be retrieved from queue and sent to the databse. If JBoss is taking care of the queue and retrieval : Yes then what code I will have to write in onMessage() to retrieve the message and sent it to the DB. If no then what extra configuration I will have to do to achieve this.

      If the above given files are not enough please help me out.