客户消费者未收到JMS回复

时间:2013-02-25 06:15:37

标签: java jms glassfish-3

我有一个客户端向JMS MDB发送请求。它可以很好地将消息发送到 MDB,但我不能在我的生活中找出如何让客户端获取MDB发回给它的响应。

编辑:客户端代码是使用@WebService在同一实例上的Web服务。我不知道这是否有所作为(例如交易),但除了这一个问题外,它似乎也有效。

MDB代码:

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));
    }
}

客户代码:

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());

每次运行时,行TextMessage replyMessage = (TextMessage)consumer.receive(JMS_REPLY_TIMEOUT);都返回null。每一次。我究竟做错了什么? MDB代码成功运行 - 日志消息全部输出相关数据,MDB接收的消息内容与预期一致。只是这条回复信息让我无法工作。

我使用以下代码浏览临时队列,并看到了消息!检查消息ID,我发送的回复消息肯定是在队列中,但调用consumer.receive(JMS_REPLY_TIMEOUT)仍然返回null ...我根本无法弄清楚发生了什么。

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();

我在Ubuntu 10.04上使用Java 1.7.0_04 64位运行Glassfish 3.1.2.2。

编辑:我也尝试将发送和接收分成单独的事务,如下所示:

@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();
    }
}

这并没有解决我的问题,但我承认我不确定是否需要这样做才能确保这些方法中有新的交易。

编辑:修改接收方法:

@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();
    }
}

我得到的日志输出是:

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]

因此,尽管在队列中等待了30秒,但我没有收到整个队列中已经存在的消息。所以某事是错误的。这是我在做什么吗?还是Glassfish的错误?

1 个答案:

答案 0 :(得分:1)

你必须添加

  

connection.start();

能够收到回复。