1

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.

Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143

1 Answers1

1

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.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • I'm using glassfish 2.1.1, but I can not find any information about how to create only one consumer per MDB. Maybe someone could show me? Thanks – Paulius Matulionis Feb 05 '12 at 20:50
  • @PauliusMatulionis: have a look at [Tuning Message-Driven Beans](http://docs.oracle.com/cd/E18930_01/html/821-2431/abebw.html#abedi) - it is for Glassfish 3, but it should point you to a proper direction. Hint: *pool size* – Tomasz Nurkiewicz Feb 05 '12 at 21:07
  • Exactly, if you want just a single message to be processed at a time you would have to limit your MDB pool to a single instance (strange use case but possible) – Kris Feb 05 '12 at 21:13
  • 1
    Thanks. This performance tuning guide helped me. Default MDB pool size was set to 32, I changed it to 1 And now its working. Thanks a lot. – Paulius Matulionis Feb 05 '12 at 21:20