JMS 和消息驱动 EJB Bean

发布于 2025-01-02 21:29:58 字数 1632 浏览 0 评论 0原文

我对消息驱动的 EJB 有疑问。 我也有包含 MessageDrivenBean 的应用程序 Web 服务和 EJB 应用程序。

为了向 JMS 发送消息,我使用 ObjectMessage: 这是我的代码:

        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, 1);
        MessageProducer messageProducer = session.createProducer(queue);
        ObjectMessage outMessage = session.createObjectMessage();
        outMessage.setObject(((Serializable) operation));
        LOGGER.debug("Sending message...");
        messageProducer.send(outMessage);
        LOGGER.debug("Sending message: done.");
        messageProducer.close();
        session.close();
        connection.close();

当我调用我的网络服务时,我也会调用这个方法。消息到达 MDB 并开始处理。这是我的 MDB 代码:

    @MessageDriven(mappedName = "jms/cbsDestination", activationConfig = {
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
    public class OperationsBackgroundService implements MessageListener {
          //Some code....
         public void onMessage(Message message) {
    LOGGER.debug("Got message: " + message.toString());
    if (message instanceof ObjectMessage) {

        ObjectMessage objectMessage = (ObjectMessage) message;
        Operation operation = null;
    }

一切正常,我收到消息,它开始处理并按我的预期结束。

但问题是: 当我向 MDB 发送第一条消息时,它开始处理它(确定),然后,当第一条消息正在处理时,我向 MDB 发送第二条消息,它也开始处理它。我知道 JMS 的特点是,如果我发送一条消息并且该消息正在处理,其他消息就会等待,直到第一个消息被处理。或者我在这里遗漏了什么?请帮忙。也许我忘记设置一些属性?

谢谢我提前。

I have a problem with message driven EJB.
I have too applications Web Service and EJB application which contains MessageDrivenBean.

To send message to JMS I'm using ObjectMessage:
Here is my code:

        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, 1);
        MessageProducer messageProducer = session.createProducer(queue);
        ObjectMessage outMessage = session.createObjectMessage();
        outMessage.setObject(((Serializable) operation));
        LOGGER.debug("Sending message...");
        messageProducer.send(outMessage);
        LOGGER.debug("Sending message: done.");
        messageProducer.close();
        session.close();
        connection.close();

When I call my web service I am calling this method as well. The message arives at MDB and starts to process. Here is my MDB code:

    @MessageDriven(mappedName = "jms/cbsDestination", activationConfig = {
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
    public class OperationsBackgroundService implements MessageListener {
          //Some code....
         public void onMessage(Message message) {
    LOGGER.debug("Got message: " + message.toString());
    if (message instanceof ObjectMessage) {

        ObjectMessage objectMessage = (ObjectMessage) message;
        Operation operation = null;
    }

Its all OK, I get the message, it starts to process and it ends as I expect.

But the problem is:
When I send first message to MDB it starts process it (OK), then, when first message is processing I send second message to my MDB, and it starts processes it as well. Ass I know the JMS is characterized by that if I send one message and that one is processing, other messages waits until the first is processed. Or am I missing something here? Please help. Maybe there is some properties I forgot to set?

Thanks id advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

笑红尘 2025-01-09 21:29:58

您的应用程序服务器创建了多个 OperationsBackgroundService 实例,并将每个实例注册为使用者。每个消费者一次只能处理一条消息,但是当有 2 个消费者时,可以同时处理 2 条消息。这是一个功能,而不是一个错误。

如果您想实现单线程处理,只需告诉您的应用程序服务器为每个 MDB 只创建一个使用者即可。请参阅您的应用程序服务器文档以了解如何进行配置。

Your application server created more than one instance of OperationsBackgroundService and registered each instance as a consumer. Each consumer can only process one message at a time, but when there are, say, 2 consumers, 2 messages can be processed concurrently. This is a feature, not a bug.

If you want to achieve single-threaded processing, simply tell your application server to create only one consumer per MDB. Consult your application server documentation to see how this can be configured.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文