JMS/Glassfish - MDB 不使用队列消息
我是第一次使用 JMS,并且使用 Glassfish 3.1.1。我已经设置了一个 JMS 连接工厂:
Pool Name: jms/QueueConnectionFactory
JNDI Name: jms/QueueConnectionFactory
Resource Type: javax.jms.QueueConnectionFactory
和一个目标资源:
JNDI Name: jms/ProcessBatchQueue
Physical Destination: ProcessBatchQueue
Resource Type: javax.jms.Queue
我已经部署了一个带有 servlet 的战争,该 servlet 接受文件,解析它并将内容保存到数据库。如果这一切都成功,它会向队列发送一条消息:
@Resource(lookup = "jms/ProcessBatchQueue")
private Queue processBatchQueue;
private void sendProcessBatchMessage(String batchID) throws JMSException
{
log.info("Attempting to send process batch message for batch ID: "
+ batchID);
Connection jmsConnection = connectionFactory.createConnection();
Session jmsSession = jmsConnection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
TextMessage message = jmsSession.createTextMessage();
message.setText(batchID);
MessageProducer msgProducer = jmsSession.createProducer(processBatchQueue);
msgProducer.send(message);
jmsConnection.close();
}
我部署了一个带有 MDB 的耳朵,它应该侦听队列并执行该消息:
@MessageDriven(mappedName = "jms/ProcessBatchQueue")
public class BatchReceiver
{
private final Logger log = LoggerFactory.getLogger(BatchReceiver.class);
public void onMessage(Message message)
{
log.info("Received message from jms/ProcessBatchQueue: " + message);
try
{
if (message instanceof TextMessage)
{
String batchId = ((TextMessage) message).getText();
// do processing
}
else
{
log.error("Received invalid message type from jms/ProcessBatchQueue");
}
}
catch (Exception ex)
{
String error = "Received error '" + ex.toString()
+ "' retrieving message from jms/BatchProcessingTopic.";
Throwable linkedEx = ex.getCause();
if (linkedEx != null)
{
log.error(error += "Linked exception: " + linkedEx.getMessage(),
linkedEx);
}
else
{
log.error(error + ", " + ex.getMessage(), ex);
}
}
}
}
在我的战争日志中,我得到了该
log.info("Attempting to send process batch message for batch ID: " + batchID);
语句,但在耳朵日志中,我没有得到任何表明 MDB 正在接收消息的信息。
我的理解是,我应该能够“仅”使用 MDB 部署耳朵,并且它应该开始接收消息。我是否错过了某个配置步骤?
有没有某种方法可以确认 servlet 中生成的消息首先进入队列?包括 server.log 在内的任何日志中都没有错误。
I am using JMS for the first time and am using Glassfish 3.1.1. I have set up a JMS Connection Factory:
Pool Name: jms/QueueConnectionFactory
JNDI Name: jms/QueueConnectionFactory
Resource Type: javax.jms.QueueConnectionFactory
and a Destination Resource:
JNDI Name: jms/ProcessBatchQueue
Physical Destination: ProcessBatchQueue
Resource Type: javax.jms.Queue
I have deployed a war with a servlet that accepts a file, parses it and saves the contents to a database. If this is all successful it sends a message to the queue:
@Resource(lookup = "jms/ProcessBatchQueue")
private Queue processBatchQueue;
private void sendProcessBatchMessage(String batchID) throws JMSException
{
log.info("Attempting to send process batch message for batch ID: "
+ batchID);
Connection jmsConnection = connectionFactory.createConnection();
Session jmsSession = jmsConnection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
TextMessage message = jmsSession.createTextMessage();
message.setText(batchID);
MessageProducer msgProducer = jmsSession.createProducer(processBatchQueue);
msgProducer.send(message);
jmsConnection.close();
}
I have a deployed an ear with an MDB that should be listening to the queue and actioning the message:
@MessageDriven(mappedName = "jms/ProcessBatchQueue")
public class BatchReceiver
{
private final Logger log = LoggerFactory.getLogger(BatchReceiver.class);
public void onMessage(Message message)
{
log.info("Received message from jms/ProcessBatchQueue: " + message);
try
{
if (message instanceof TextMessage)
{
String batchId = ((TextMessage) message).getText();
// do processing
}
else
{
log.error("Received invalid message type from jms/ProcessBatchQueue");
}
}
catch (Exception ex)
{
String error = "Received error '" + ex.toString()
+ "' retrieving message from jms/BatchProcessingTopic.";
Throwable linkedEx = ex.getCause();
if (linkedEx != null)
{
log.error(error += "Linked exception: " + linkedEx.getMessage(),
linkedEx);
}
else
{
log.error(error + ", " + ex.getMessage(), ex);
}
}
}
}
In my war logs, I get the
log.info("Attempting to send process batch message for batch ID: " + batchID);
statement, but in the ear logs, I get nothing that would indicate that the MDB is receiving the message.
My understanding is that I should be able to "just" deploy the ear with the MDB and it should start receiving the messages. Is there a config step that I've missed?
Is there some way to confirm that the message generated in the servlet is making it to the queue in the first place? There are no errors in any log including server.log.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 bean 没有实现
javax.jms.MessageListener
,它只有一个具有相同签名的onMessage()
方法。您也可能错过了注释的
activationConfig
部分,但我不确定 Java EE 6 中是否需要它。看看是否是这样:Your bean does not implement
javax.jms.MessageListener
, it just has anonMessage()
method with a the same signature.It's also possible that you miss the
activationConfig
part of the annotation, but I'm not sure if it's required in Java EE 6. See if it's the case anyway: