Interface ConsumeInterceptor


public interface ConsumeInterceptor
Runs around every handler on a connection.

For the work that surrounds handling rather than being part of it: putting the correlation identifier into the logging context, adopting the tenant the message came from, opening and closing a unit of work, timing something the built-in metrics do not cover.


 mq.intercept(new ConsumeInterceptor() {
     public void beforeHandle(ConsumeContext context) {
         MDC.put("correlationId", context.envelope().correlationId());
     }
     public void afterHandle(ConsumeContext context, Ack ack) {
         MDC.remove("correlationId");
     }
 });
 

Failure

Throwing from beforeHandle(org.acemq.amqp.api.ConsumeContext) means the handler never runs and the delivery is treated exactly as a failed handler would be — retried, then dead-lettered. An interceptor that refuses a message must be willing for that message to end up in the dead-letter queue, which is the honest outcome: the alternative is acknowledging something nothing processed.

afterHandle(org.acemq.amqp.api.ConsumeContext, org.acemq.amqp.api.Ack) runs whether the handler succeeded or failed, so it is the place to undo whatever beforeHandle(org.acemq.amqp.api.ConsumeContext) set up. Throwing from it is logged and ignored: the message has already been settled, and an exception cannot un-settle it.

Implementations must be thread safe, and must not assume that beforeHandle and afterHandle run on the same thread as any other message.

  • Method Details

    • beforeHandle

      void beforeHandle(ConsumeContext context)
      Called after the payload is decoded and before the handler runs.
      Parameters:
      context - the message about to be handled
      Throws:
      RuntimeException - to refuse the message, which fails the delivery
    • afterHandle

      default void afterHandle(ConsumeContext context, Ack ack)
      Called after the handler returns or throws, before the delivery is settled.
      Parameters:
      context - the message that was handled
      ack - how the delivery is about to be settled
    • onError

      default void onError(ConsumeContext context, Throwable failure)
      Parameters:
      context - the message being handled
      failure - what the handler threw
    • order

      default int order()
      Returns:
      where this interceptor sits; lower runs first on the way in, and the reverse on the way out, so a pair that sets up and tears down state nests properly