简体   繁体   English

JMS和MessageDriven EJB Bean

[英]JMS and MessageDriven EJB Bean

I have a problem with message driven EJB. 我对消息驱动的EJB有问题。 I have too applications Web Service and EJB application which contains MessageDrivenBean. 我也有包含MessageDrivenBean的应用程序Web Service和EJB应用程序。

To send message to JMS I'm using ObjectMessage: Here is my code: 要将消息发送到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();

When I call my web service I am calling this method as well. 当我调用Web服务时,我也在调用此方法。 The message arives at MDB and starts to process. 该消息到达MDB并开始处理。 Here is my MDB code: 这是我的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;
    }

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. 但是问题是:当我向MDB发送第一条消息时,它开始处理它(确定),然后,当第一条消息正在处理时,我将第二条消息发送到我的MDB,它也开始处理它。 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. 屁股我知道JMS的特点是,如果我发送一条消息并且正在处理,则其他消息会等到第一个消息被处理。 Or am I missing something here? 还是我在这里想念什么? Please help. 请帮忙。 Maybe there is some properties I forgot to set? 也许有些属性我忘记设置了?

Thanks id advance. 谢谢id的提前。

Your application server created more than one instance of OperationsBackgroundService and registered each instance as a consumer. 您的应用程序服务器创建了一个以上的OperationsBackgroundService实例,并将每个实例注册为使用者。 Each consumer can only process one message at a time, but when there are, say, 2 consumers, 2 messages can be processed concurrently. 每个使用方一次只能处理一条消息,但是当有2个使用方时,可以同时处理2条消息。 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. 如果要实现单线程处理,只需告诉您的应用服务器每个MDB仅创建一个使用者即可。 Consult your application server documentation to see how this can be configured. 请查阅您的应用程序服务器文档,以了解如何进行配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM