Search code examples
javajakarta-eejmsmessage-queuemessage-driven-bean

JMS - Redelivery flow in a queue


Configuration

I´m using EJB 3 in OC4J 10.1.3.x

The problem

I create a queue producer with CLIENT_ACKNOWLEDGE orientation, like:

    queueConnection = queueConnectionFactory.createQueueConnection();
    queueSession = queueConnection.createQueueSession(false,
            Session.CLIENT_ACKNOWLEDGE);
    queueSender = queueSession.createSender(queue);

    ObjectMessage objMessage = queueSession.createObjectMessage();
    Mail data = new Mail();
    data.setMessage("Some random message.");
    objMessage.setObject(data);
    queueSender.send(objMessage);

What I understand of it is that the consumer should handle the ack of the message, in other words, if the consumer DO NOT call the message.acknowledge() method, so the message should be redelivery. Is it right?

What is happen is, when my MDB just read the message (by the listener onMessage) that message just get off from the queue (independent if I call or not the message.acknowledge() method).

   public void onMessage(Message message) {
            try {
                if (message instanceof ObjectMessage) {
                    ObjectMessage objectMessage = (ObjectMessage) message;
                    Mail mail = (Mail) objectMessage.getObject();
                    System.out.println(mail.getMessage());
                    throw new RuntimeException("Error");
                }
            } catch (JMSException e) {
            }

What I am doing wrong?


Solution

  • Well, I did lots of research about it and I found that the container usually ignores the client control on session creation. In this case: queueConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE); So, if you need to handle message process in a MDB with acknowledge or not you need to work with an EJB transaction.