jmsquarkusibm-mqquarkus-reactivesmallrye-reactive-messaging

Quarkus Reactive Messaging - Exception Handler


We use Quarkus Messaging (with the "smallrye-jms" extension in order to connect to IBM MQ) with a converter to transform the messages received in strings, into a Java object.

When receiving an incoming message in "queue_A", we must produce a message in one of the following 2 destinations:

How can we implement this error handling use-case with Quarkus Messaging?


Solution

  • You could do something like this:

    @Inject @Channel("queue_A_error") 
    Emitter<MyErrorMsg> emitter;
    
    @Inject 
    ObjectMapper mapper;
    
    @Incoming("incoming_queue")
    @Outgoing("queue_B")
    public MyMessage handleMsg(String payload) {
      try {
         return this.mapper.readValue(payload, MyMessage.class);
      } catch (Exception e) {
         this.emitter.send(new MyErrorMsg(payload, e));
         return null;
      }
    }
    

    Note, I didn't test this code and I hope it helps you.

    Edit: @JYC, I see that it is also possible to intercept incoming messages, see this link: https://smallrye.io/smallrye-reactive-messaging/4.25.0/concepts/decorators/#incominginterceptor

    This can be handy if you want to globally implement error handling.

    Edit 2: another option is to configure a DLQ:

    mp.messaging.incoming.your-channel-name.dead-letter-queue=your-dead-letter-queue-name