0 Replies Latest reply: Mar 30, 2012 6:55 AM by chithu21 RSS

JMS Client program is not able to receive messages from the queue

chithu21 Newbie

I have a jms client program and an MDB. The client program is not able to receive message from the queue. But I am able to see the messages in the MDB's system.out log.

Can anyone help me please? Thanks in advance.

 

Client Program:

----------------------

 

public class MessageReceiver {

 

    static final int N = 1;

    static CountDown done = new CountDown(N);

 

    ReadValues readValues = new ReadValues();

    Config config = readValues.readPropertiesFile();

 

    public static class ExListener implements MessageListener {

        public void onMessage(Message msg) {

            System.out.println("Inside OnMessage");

            done.release();

            TextMessage tm = (TextMessage) msg;

 

            try {

                String receivedMsg = tm.getText();

                System.out.println("onMessage, recv text=" + receivedMsg);

                // System.out.println("Added Some text with " + receivedMsg);

 

            } catch (Throwable t) {

                t.printStackTrace();

            }

        }

       

             }

  

 

 

    public void receiveMessage() throws JMSException, NamingException,

            InterruptedException, ClassNotFoundException, SQLException {

 

        System.out.println("Begin receiveMessage method");

 

        JMSConnector jmsConnector = new JMSConnector();

 

        QueueObject queueObj = jmsConnector.setupPTP(config.getProviderUrl(),

                "XibToCatQueue");

        QueueSession session = queueObj.getQueueSession();

        Queue xibToCatQueue = queueObj.getQueue();

 

        System.out.println("Queue Name : " + xibToCatQueue.getQueueName());

 

        // Set the async listener for xibToCatQueue

                QueueReceiver recv = session.createReceiver(xibToCatQueue);

            //    System.out.println("........." + recv.getQueue());

                recv.setMessageListener(new ExListener());   

       

 

        Queue catToXibQueue = queueObj.getCatToXibQueue();

        QueueSender send = session.createSender(catToXibQueue);

 

    

        String text = "Test Message...";

     

        TextMessage tm = session.createTextMessage(text);

        tm.setJMSReplyTo(xibToCatQueue);

        send.send(tm);

        System.out.println("Sent text=" + tm.getText());

        send.close();

       

 

        MessageReceiver.done.acquire();

        jmsConnector.closePTP();

       

        System.out.println("End receiveMessage method");

        System.exit(0);

    }

 

 

     public static void main(String[] args) throws JMSException,

            NamingException, InterruptedException, ClassNotFoundException,

            SQLException {

        MessageReceiver msgReceiver = new MessageReceiver();

        msgReceiver.receiveMessage();

 

    }

}

 

 

Connector Program:

---------------------------------

 

 

public QueueObject setupPTP(String contextUrl,String queueName) throws JMSException, NamingException {

       

        ReadValues readValues = new ReadValues();

        Config config = readValues.readPropertiesFile();

        Properties env = new Properties();

        env.put(Context.INITIAL_CONTEXT_FACTORY,config.getContextFactory());

        env.put(Context.PROVIDER_URL, contextUrl);

        env.put(Context.URL_PKG_PREFIXES,config.getPkgPrefixes());

        InitialContext iniCtx = new InitialContext(env);

       

        Object tmp = iniCtx.lookup("ConnectionFactory");

        QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;

       

        connection = qcf.createQueueConnection();

        String lookUpObj = "queue/" + queueName;

        Queue xibToCatQueue =  (Queue) iniCtx.lookup(lookUpObj);

        Queue catToXIbQueue = (Queue) iniCtx.lookup("queue/CatToXibQueue");

       

        session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

        connection.start();

       

       

     

        return new QueueObject(session,xibToCatQueue,catToXIbQueue);

    }

   

    public void closePTP() throws JMSException {

        System.out.println("Inside closePTP.....");

        connection.stop();

        if(session != null) {

            session.close();

        }

        if(connection != null) {

            connection.close();

        }

    }