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.
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 Summary
Modifier and TypeMethodDescriptiondefault voidafterConfirm(PublishContext context, PublishResult result) Called once the broker has confirmed the message.beforePublish(PublishContext context) Called before the payload is encoded, with whatever the previous interceptor returned.default voidonError(PublishContext context, Throwable failure) Called when a publish fails, including when another interceptor refused it.default intorder()
-
Method Details
-
beforePublish
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
Called once the broker has confirmed the message.- Parameters:
context- the context asbeforePublish(org.acemq.amqp.api.PublishContext)left itresult- what the broker said
-
onError
Called when a publish fails, including when another interceptor refused it.- Parameters:
context- the context as it stood when the failure happenedfailure- 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
-