Interface PublishInterceptor

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface PublishInterceptor
Runs around every publish on a connection.

The extension point for the things every message in an organisation needs and no library can guess: a tenant identifier, an authorisation token, a schema version, a size limit, a log context. Without it these end up copied into every call site, where one of them is always missing.


 mq.intercept((PublishInterceptor) context ->
         context.withEnvelope(context.envelope().toBuilder()
                 .header("tenant", TenantContext.current())
                 .build()));
 

Failure

Throwing from beforePublish(org.acemq.amqp.api.PublishContext) fails the publish, and nothing is sent. That is the point: an interceptor enforcing a policy has to be able to refuse. Throwing from afterConfirm(org.acemq.amqp.api.PublishContext, org.acemq.amqp.api.PublishResult) or onError(org.acemq.amqp.api.PublishContext, java.lang.Throwable) is logged and otherwise ignored, because the message has already gone and there is nothing useful left to decide.

Implementations must be thread safe. One instance serves every publisher on the connection, on whatever thread is publishing.

  • Method Details

    • beforePublish

      PublishContext beforePublish(PublishContext context)
      Called before the payload is encoded, with whatever the previous interceptor returned.
      Parameters:
      context - the message as it stands
      Returns:
      the context to carry on with; return the argument unchanged to leave it alone
      Throws:
      RuntimeException - to refuse the publish
    • afterConfirm

      default void afterConfirm(PublishContext context, PublishResult result)
      Called once the broker has confirmed the message.
      Parameters:
      context - the context as beforePublish(org.acemq.amqp.api.PublishContext) left it
      result - what the broker said
    • onError

      default void onError(PublishContext context, Throwable failure)
      Called when a publish fails, including when another interceptor refused it.
      Parameters:
      context - the context as it stood when the failure happened
      failure - what went wrong
    • order

      default int order()
      Returns:
      where this interceptor sits; lower runs first, and equal orders run in the order they were registered. Matters whenever one interceptor reads what another wrote