Search code examples
jakarta-eeglassfishpayara

jms message non persistent redelivery after stop-start appServer


use jms queue.

ejb sender :

@Resource(lookup = "jms/csPdvQueue")
private Queue csPdvQueue;
@Inject
private JMSContext jmsContext;

method send message :

public void asynchWatched(Pdv pdv, boolean pending) {
   jmsContext.createProducer().setDeliveryMode(DeliveryMode.NON_PERSISTENT);
   jmsContext.createProducer().send(csPdvQueue, pdv);
}

ejb consumer:

@MessageDriven(mappedName = "jms/csPdvQueue")
public class PdvProcessorMdb implements MessageListener {

    @Override
    public void onMessage(Message message) {
    ... execute some businesslogic...
    }

}

if application server (payara) quits during message consuming (onMessage), the transaction correctly rollback.

When application server starts again the message is redelivered even if I've set DeliveryMode.NON_PERSISTENT.

I want to avoid the message redelivering.

Is it possible (and how) ?


Solution

  • When sending message, you do setDeliveryMode(DeliveryMode.NON_PERSISTENT);, which means the message will not be saved between restarts of the message broker. If you use the OpenMQ broker in embedded mode (the default), it will be restarted together with the server. Therefore after restart, the message doesn't exist and cannot be sent again.

    Update:

    However, your code sets the delivery mode on a different producer than which one sends the message (a producer is created, set delivery mode, then thrown away; the next line creates a new producer, which sends the message). You need to store the created producer in a variable, set the delivery mode, and then use the same producer to send the message:

    public void asynchWatched(Pdv pdv, boolean pending) {
       JMSProducer producer = jmsContext.createProducer();
       producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
       producer.send(csPdvQueue, pdv);
    }
    

    You can make it shorter, taking advantage of the fluent API:

    public void asynchWatched(Pdv pdv, boolean pending) {
       jmsContext.createProducer()
         .setDeliveryMode(DeliveryMode.NON_PERSISTENT);
         .send(csPdvQueue, pdv);
    }