我第一次使用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部署了一个warlet,它接受一个文件,解析它并将内容保存到数据库中。如果这一切都成功,它会向队列发送一条消息:
@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的耳朵,该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在内的任何日志都没有错误。
答案 0 :(得分:2)
您的bean没有实现javax.jms.MessageListener
,它只有一个onMessage()
方法,具有相同的签名。
您也可能错过了注释的activationConfig
部分,但我不确定它是否在Java EE 6中是必需的。无论如何,请查看是否是这样:
@MessageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "ProcessBatchQueue")},
mappedName = "jms/ProcessBatchQueue")