gpt4 book ai didi

java - 客户端消费者未收到 JMS 回复

转载 作者:太空宇宙 更新时间:2023-11-04 07:47:26 32 4
gpt4 key购买 nike

我有一个向 JMS MDB 发送请求的客户端。它可以很好地向 MDB 发送消息,但我一辈子都无法弄清楚如何让客户端接收 MDB 发回给它的响应。

编辑:客户端代码是同一实例上的 Web 服务,使用 @WebService。我不知道这是否会产生影响(例如对于交易),但除了这个问题之外,它似乎有效。

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

每次运行此命令时,TextMessagereplyMessage = (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 的错误?

最佳答案

您必须添加

connection.start();

能够接收响应。

关于java - 客户端消费者未收到 JMS 回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15061149/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com