JMS/Glassfish - MDB 不使用队列消息

发布于 2024-12-17 06:04:22 字数 2635 浏览 3 评论 0原文

我是第一次使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2024-12-24 06:04:22

您的 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")

Your bean does not implement javax.jms.MessageListener, it just has an onMessage() 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:

@MessageDriven(
    activationConfig = { 
            @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
            @ActivationConfigProperty(propertyName = "destination", propertyValue = "ProcessBatchQueue")},
    mappedName = "jms/ProcessBatchQueue")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文