Results 1 to 1 of 1
  1. #1
    freakmoister is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default JMS: Can't get reply from queue

    Hi Everyone,

    I'm trying to implement a messaging service with JMS, using glassfish v2.x as my queuing server.

    I have created a requester class (sending messages and waiting for reply) and a replier class (receive messages and reply). I've also configured one connectionFactory, and 2 queues (one for request and one for reply) in glassfish.

    Currently I can send a message the replier gets it and replies however the requester is unable to get the reply. The requester uses a synchronous receiver so it just blocks at the receive() method. I'm sure that I have the relevant queues and correlation ids right, but the requester just still can't find any replies.

    Any help on this will be greatly appreciated.

    How I call the requester

    Java Code:
    public class MsgBoxProducer {
        public static void main(String[] args) throws Exception{
            String REQ_CONN = "jms/Warehouse_connection";
            String REQ_QUEUE = "jms/Warehouse_Request_Queue";
            String RPLY_QUEUE = "jms/Warehouse_Reply_Queue";
     
            Requestor requestor = new Requestor(REQ_CONN,REQ_QUEUE,RPLY_QUEUE);
            try{
                for (int i = 0; i < 1; i++) {
                    String msg = "Delivery:message" + (i + 1);
                    System.out.println("Sending message: " + msg);
                    requestor.sendMessage(msg);
     
                    String reply = requestor.receiveReply();
                    System.out.println("Received reply: " + reply);
     
                    Thread.sleep(10);
                }
            }finally{
                requestor.closeConn();
            }
            System.exit(0);
        }
    }

    How I call the Replier

    Java Code:
    public class Warehouse {   
        public static void main(String[] args) throws Exception{
            String REQ_CONN  = "jms/Warehouse_connection";
            String REQ_QUEUE = "jms/Warehouse_Request_Queue";
            
            Replier replier = new Replier(REQ_CONN,REQ_QUEUE);
            replier.startReplier();
            
            Reader inputStreamReader = new InputStreamReader(System.in);
            System.out.println("To stop Warehouse application enter: Q/q");
            char answer = 0;
            while (!((answer == 'q') || (answer == 'Q'))) {
                try {
                    answer = (char) inputStreamReader.read();
                } catch (IOException e) {
                    System.err.println("I/O exception: " + e.toString());
                }
            }
            replier.stopReplier();
            System.exit(0);      
        }    
    }
    The replier class, I've omitted open and close methods

    Java Code:
    public class Replier implements MessageListener {
        private Connection conn;
        private Session session;
        private MessageConsumer requestConsumer;
        private Destination requestQueue;
     
        public Replier (String confacName, String requestQueueName) {
            try {
                // Connector class just implements some get initial context methods
                Connector connector = new Connector();
                ConnectionFactory confac = connector.getConnectionFactory(confacName);
                conn = confac.createConnection();
                requestQueue = connector.getQueue(requestQueueName);
                session = conn.createSession(false,Session.AUTO_ACKNOWLEDGE);
                requestConsumer = session.createConsumer(requestQueue);
            }
            catch (Throwable t) {
                System.out.println("Cannot establish replier connection: " + t.getMessage());
            }
        }
     
        public void onMessage(Message message) {
            try {
                if (message instanceof TextMessage) {
                    TextMessage requestMessage = (TextMessage) message;
                    String reply = processMessage(requestMessage.getText());
                    if (message.getJMSReplyTo() != null) {
                        Destination replyDestination = message.getJMSReplyTo();
                        MessageProducer replyProducer = session.createProducer(replyDestination);
                        TextMessage replyMessage = session.createTextMessage();
                        replyMessage.setText(reply);
                        replyMessage.setJMSCorrelationID(requestMessage.getJMSMessageID());
                        replyProducer.send(replyMessage);
                    }
                    else {
                        System.out.println("Reply not required");
                    }
                }
                else {
                    System.out.println("Invalid message detected");
                }
            } catch (JMSException e) {
                    e.printStackTrace();
            }
        }
    }
    The Requester class

    Java Code:
    public class Requestor {
        private Connection conn;
        private Destination reply_dest;
        private Session sess;
        private MessageProducer prod;
        private MessageConsumer consum;
        public Requestor (String confacName, String requestQueueName, String replyQueueName) {
            try {
                Connector connector = new Connector();
     
                ConnectionFactory cxn = connector.getConnectionFactory(confacName);
                Destination dest = connector.getQueue(requestQueueName);
                reply_dest = connector.getQueue(replyQueueName);
     
                conn = cxn.createConnection();
                sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
                prod = sess.createProducer(dest);
                consum = sess.createConsumer(reply_dest);
            }
            catch (JMSException e) {
                System.out.println("cannot start connection: "+e.getMessage());
            }
            catch (Throwable t) {
                System.out.println("cannot start connection: "+t.getMessage());
            }
        }
     
        public void sendMessage(String msg) {
            try {
                TextMessage message = sess.createTextMessage();
                message.setText(msg);
                message.setJMSReplyTo(reply_dest);
                prod.send(message);
            }
            catch (JMSException e) {
                System.out.println("Failed to send message: " + e.getMessage());
            }
        }
     
        public String receiveReply() {
            String reply = null;
            try {
                Message msg = consum.receive();
                if (msg instanceof TextMessage) {
                    TextMessage replyMessage = (TextMessage) msg;
                    reply = replyMessage.getText();
                }
                else if (msg == null) {
                    System.out.println("Receive timeout");
                }
                else {
                    System.out.println("Non text message detected, discard");
                }
            }
            catch (JMSException e) {
                System.out.println("Failed to receive reply: " + e.getMessage());
            }
            finally {
                return reply;
            }
        }
    }
    Last edited by freakmoister; 05-10-2010 at 08:54 AM.

Similar Threads

  1. urgent problem..pls reply soon
    By mudit222 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-06-2010, 01:17 PM
  2. HELP PLEASE! Need reply ASAP
    By SteroidalPsycho in forum New To Java
    Replies: 3
    Last Post: 10-21-2009, 09:28 AM
  3. Please reply to help Me ......
    By mohamed sherif in forum New To Java
    Replies: 3
    Last Post: 12-02-2008, 08:05 PM
  4. Same old Question please reply
    By venat123 in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 01:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •