8 Replies Latest reply on Sep 24, 2001 3:16 AM by Peter Antman

    QueueReceiver class doesn't work when JBoss manage JMS resou

    eddy Newbie

      Am I able to use the QueueReceiver.receive() method inside a statless bean for which JBoss managed the JMS resource? I knew I can use MDB instead; however, MDB is not allow to set message selector to receive a specific message. I am using JBoss-2.4.1. The following is my code. When I call the QueueReceiver.receive() method inside my remove() method, it simply hung.

      Thanks in advance!

      import java.rmi.*;
      import javax.ejb.*;
      import javax.jms.Queue;
      import javax.jms.QueueConnection;
      import javax.jms.QueueSession;
      import javax.jms.QueueConnectionFactory;
      import javax.jms.QueueSender;
      import javax.jms.QueueReceiver;
      import javax.jms.QueueBrowser;
      import javax.jms.JMSException;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.jms.Message;
      import javax.jms.TextMessage;
      import javax.jms.ObjectMessage;
      import javax.jms.Session;
      import java.util.Enumeration;
      import java.io.Serializable;
      import java.util.Vector;
      import java.util.HashMap;
      import com.seitel.gos.util.GOSConstant;
      import javax.jms.MessageListener;
      
      public class QueueManagerSLB implements SessionBean {
       private SessionContext sessionContext;
       private static final String QUEUE_CONNECTION_FACTORY_JNDI =
       "java:comp/env/jms/QueueConnectionFactory";
       private static final String QUEUE_JNDI = "java:comp/env/jms/Queue";
       private long m_timeToLive = 400000;
       private QueueConnection m_qeueConn = null;
       private Queue m_queue = null;
      
       private boolean DEBUG_ON = true;
       private void debug(Object mesg){
       if (DEBUG_ON)
       System.out.println("DEBUG: QueueManagerSLB: " + mesg);
       }
      
      
       public void ejbCreate() throws CreateException{
       try{
       Context ctx = new InitialContext();
       this.m_queue = (Queue)ctx.lookup(QUEUE_JNDI);
       QueueConnectionFactory qFactory = (QueueConnectionFactory)ctx.lookup(
       QUEUE_CONNECTION_FACTORY_JNDI);
       this.m_qeueConn = qFactory.createQueueConnection();
       }catch(NamingException namex){
       throw new CreateException(namex.getMessage());
       }catch(JMSException jmsex){
       throw new CreateException("JMS Error Code: " +jmsex.getErrorCode()+
       jmsex.getMessage());
       }
       }
       public void ejbRemove() throws RemoteException {
       if (this.m_qeueConn != null){
       try{
       this.m_qeueConn.close();
       }catch(JMSException jmsex){
       jmsex.printStackTrace();
       }
       }
       }
       public void ejbActivate() throws RemoteException {
       }
       public void ejbPassivate() throws RemoteException {
       }
       public void setSessionContext(SessionContext sessionContext) throws RemoteException {
       this.sessionContext = sessionContext;
       }
      
      
       public void add(HashMap info,int deliveryMode,
       int priority){
       QueueSession qSession = null;
       try{
       qSession = this.m_qeueConn.createQueueSession(true,
       Session.AUTO_ACKNOWLEDGE);
       QueueSender qSender = qSession.createSender(this.m_queue);
       //this.debug("Default delivery Mode: " + qSender.getDeliveryMode());
       //this.debug("Default Priority : " + qSender.getPriority());
       //this.debug("Default Time to Live : " + qSender.getTimeToLive());
       ObjectMessage objMessage = qSession.createObjectMessage();
       objMessage.setJMSCorrelationID((String)info.get(
       GOSConstant.TRANSACTION_ID));
       objMessage.setObject(info);
       qSender.send(objMessage,deliveryMode,priority,this.m_timeToLive);
       }catch(JMSException jmsex){
       this.sessionContext.setRollbackOnly();
       throw new EJBException(jmsex);
       }finally{
       if (qSession != null){
       try{
       qSession.close();
       }catch(JMSException jmsex){
       jmsex.printStackTrace();
       }
       }
       }
       }
      
       public Vector list(){
       QueueSession qSession = null;
       try{
       qSession = this.m_qeueConn.createQueueSession(true,
       Session.AUTO_ACKNOWLEDGE);
       QueueBrowser qBrowser = qSession.createBrowser(this.m_queue);
       Enumeration enu = qBrowser.getEnumeration();
      
       Vector tmp = new Vector();
       Message m = null;
       while(enu.hasMoreElements()){
       m = (Message)enu.nextElement();
       tmp.add(m);
       }
      
       return tmp;
       }catch(JMSException jmsex){
       throw new EJBException(jmsex);
       }finally{
       if (qSession != null){
       try{
       qSession.close();
       }catch(JMSException jmsex){
       jmsex.printStackTrace();
       }
       }
       }
       }
      
      
       public Message remove(String transactionId){
       QueueSession qSession = null;
       QueueReceiver qReceiver = null;
       Message obj = null;
       String messageFilter = "JMSCorrelationID='" + transactionId + "'";
       try{
       qSession = this.m_qeueConn.createQueueSession(true,
       Session.AUTO_ACKNOWLEDGE);
       qReceiver = qSession.createReceiver(this.m_queue,messageFilter);
       obj = (Message)qReceiver.receive();
      
       }catch(JMSException jmsex){
       jmsex.printStackTrace();
       throw new EJBException(jmsex);
       }finally{
       if (qReceiver != null){
       try{ qReceiver.close();
       }catch(JMSException jmsex){
       jmsex.printStackTrace();
       }
       }
       if (qSession != null){
       try{ qSession.close();
       }catch(JMSException jmsex){
       jmsex.printStackTrace();
       }
       }
       }
       return obj;
       }
      
       private class DumpMessageListener implements MessageListener{
       public void onMessage(Message m){
       System.out.println("Message Received: " + m);
       }
      
       }
      }