Search code examples
javajmsglassfish-3

JMS reply not received by client consumer


I've got a client that sends a request to a JMS MDB. It works fine for sending the messages to the MDB, but I cannot for the life of me figure out how to get the client to pick up the response the MDB sends back to it.

EDIT: The client code is a web service on the same instance, using @WebService. I don't know if that makes a difference (e.g. for transactions), but it seems to work, apart from this one issue.

MDB Code:

private void sendReply(String replyData, javax.jms.Message requestMessage)
{
    try
    {
        logger.info("sendReply message received had ID: " + requestMessage.getJMSMessageID());
        logger.info("sendReply message getJMSReplyTo: " + requestMessage.getJMSReplyTo());

        Session session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);

        final Queue replyQueue = (Queue) requestMessage.getJMSReplyTo();
        MessageProducer producer = session.createProducer(replyQueue);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        TextMessage message = session.createTextMessage();
        message.setText(replyData);
        message.setJMSCorrelationID(requestMessage.getJMSMessageID());

        producer.send(message, DeliveryMode.PERSISTENT, 9, 60000);
        producer.close();

        logger.info("sendReply message returned had ID: " + message.getJMSMessageID());

        session.close();

        logger.debug("sendReply sent reply containing:\n" + replyData);
    }
    catch (Throwable e)
    {
        logger.warn("Exception in sendReply(): " + stackToString(e));
    }
}

Client Code:

connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(requestQueue);

Message message = null;

// Snip creation of message

producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
final Queue replyQueue = session.createTemporaryQueue();
message.setJMSReplyTo(replyQueue);
consumer = session.createConsumer(replyQueue);  
producer.send(message);
producer.close();

log.info("call message transmitted had  Message ID: " + message.getJMSMessageID());

TextMessage replyMessage = (TextMessage)consumer.receive(JMS_REPLY_TIMEOUT); // 30 seconds

log.info("call message returned had Correlation ID: " + replyMessage.getJMSCorrelationID());
log.info("call message returned had     Message ID: " + replyMessage.getJMSMessageID());

Every single time I run this, the line TextMessage replyMessage = (TextMessage)consumer.receive(JMS_REPLY_TIMEOUT); returns null. Every single time. What am I doing wrong? The MDB code runs succesfully - the log messages all output relevant data, and the content of the message received by the MDB is as expected. It's just this reply message that I cannot get to work.

I have used the following code to browse the temporary Queue, and that sees the message! Checking the message IDs, the reply message I sent is definitely in the queue, but calling consumer.receive(JMS_REPLY_TIMEOUT) still returns null... I simply cannot figure out what's going on.

log.info("Browsing Queue");
QueueBrowser browser = session.createBrowser(replyQueue);
final Enumeration<?> en = browser.getEnumeration();
while (en.hasMoreElements()) {
    Message m = (Message) en.nextElement();
    log.info("Found message in Queue with Browser with ID [{}] and CorrelID [{}]", m.getJMSMessageID(), m.getJMSCorrelationID());
}
browser.close();

I'm running Glassfish 3.1.2.2 on Ubuntu 10.04 with Java 1.7.0_04 64bit.

EDIT: I have also tried splitting out the send and receive into separate transactions, like so:

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
private void sendMessage(final Session session, final Message message) throws JMSException {
    // Create a MessageProducer from the Session to the Topic or Queue
    final MessageProducer producer = session.createProducer(requestQueue);

    try {
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        producer.send(message);
    }
    finally {
        producer.close();
    }
}

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
private TextMessage receiveMessage(Session session, Destination replyQueue) throws JMSException {
    final MessageConsumer consumer = session.createConsumer(replyQueue);
    try {
        final TextMessage m = (TextMessage)consumer.receive(JMS_REPLY_TIMEOUT);
        return m;
    }
    finally {
        consumer.close();
    }
}

That did not solve my problem, though I will admit I'm unsure if that's all I need to do to ensure there is a new transaction in those methods.

EDIT: Modified receive method to:

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
private TextMessage receiveMessage(Session session, Destination replyQueue) throws JMSException {
    final MessageConsumer consumer = session.createConsumer(replyQueue);
    try {
        log.info("Receiving message on Queue [{}]", replyQueue);
        final TextMessage m = (TextMessage)consumer.receive(JMS_REPLY_TIMEOUT);
        log.info("Received message [{}]", m);
        log.info("Browsing Queue");
        QueueBrowser browser = session.createBrowser((Queue) replyQueue);
        final Enumeration<?> en = browser.getEnumeration();
        while (en.hasMoreElements()) {
            Message mess = (Message) en.nextElement();
            log.info("Found message in Queue with Browser with ID [{}] and CorrelID [{}]", mess.getJMSMessageID(), mess.getJMSCorrelationID());
        }
        browser.close();
        return m;
    }
    finally {
        consumer.close();
    }
}

And the log output I get is:

20130225200939808 CLIENT heartBeat Called.
20130225200939808 CLIENT heartBeat Sending Message...
20130225200940020 CLIENT callMAPSWebServiceMDB building TextMessage
20130225200940057 CLIENT Sending message
20130225200940064 CLIENT Sent message
20130225200940067 CLIENT message transmitted had  Message ID: ID:1-192.168.2.105(b5:49:dd:bf:5b:a6)-1-1361776180058
20130225200940077 CLIENT Receiving message on Queue [Oracle GlassFish(tm) Server MQ Destination
getName():      temporary_destination://queue/192.168.2.105/3170601277394174976/1
Class:          com.sun.messaging.jms.ra.TemporaryQueue
getVERSION():       3.0
isReadonly():       false
getProperties():    {imqDestinationName=temporary_destination://queue/192.168.2.105/3170601277394174976/1, imqDestinationDescription=A Description for the Destination Object}]
20130225200940183 SERVER Message Received
20130225200940757 SERVER Function called is: [heartBeat]
20130225200950759 SERVER sendReply message received had ID: ID:1-192.168.2.105(b5:49:dd:bf:5b:a6)-1-1361776180058
20130225200950760 SERVER sendReply message getJMSReplyTo: Oracle GlassFish(tm) Server MQ Destination
getName():      temporary_destination://queue/192.168.2.105/3170601277394174976/1
Class:          com.sun.messaging.jms.ra.TemporaryQueue
getVERSION():       3.0
isReadonly():       false
getProperties():    {imqDestinationName=temporary_destination://queue/192.168.2.105/3170601277394174976/1, imqDestinationDescription=A Description for the Destination Object}
20130225200950777 SERVER Sending reply message
20130225200950779 SERVER Sent reply message
20130225200950780 SERVER sendReply message returned had ID: ID:2-192.168.2.105(b5:49:dd:bf:5b:a6)-1-1361776190777
20130225200950781 SERVER MAPSWebServiceMDB Message Done
20130225201010078 CLIENT Received message [null]
20130225201010080 CLIENT Browsing Queue
20130225201010092 CLIENT Found message in Queue with Browser with ID [ID:2-192.168.2.105(b5:49:dd:bf:5b:a6)-1-1361776190777] and CorrelID [ID:1-192.168.2.105(b5:49:dd:bf:5b:a6)-1-1361776180058]

So despite waiting on the queue for 30 seconds, I didn't receive the message that had been on the queue the entire time. So something is wrong. Is it something I am doing? Or a bug with Glassfish?


Solution

  • You have to add

    connection.start();

    to be able to receive the response.