Annotation Interface CommandHandlerInterceptor


@Retention(RUNTIME) @Target({METHOD,ANNOTATION_TYPE}) public @interface CommandHandlerInterceptor
Annotation marking a method as a command-specific interceptor handler on an AnnotatedCommandHandlingComponent.

An annotated interceptor is invoked before every @CommandHandler method on the same component instance. Two styles are supported:

  • Before-interceptor — a method with no MessageHandlerInterceptorChain parameter that returns either void or CompletableFuture<Void>. The method runs before the handler; the chain is automatically proceeded after the method returns (or the future completes) normally. If it throws (or the future completes exceptionally), the handler is not invoked.
  • Surround-interceptor — a method that declares a MessageHandlerInterceptorChain parameter and returns a MessageStream. The method controls whether and when the chain is proceeded by calling chain.proceed(command, ctx); it can also short-circuit by returning without calling proceed.

Example before-interceptor:


 @CommandHandlerInterceptor
 void audit(CommandMessage command) {
     auditLog.record(command.qualifiedName());
 }
 

Example surround-interceptor with short-circuit:


 @CommandHandlerInterceptor
 MessageStream<?> filterByTenant(
         CommandMessage command,
         MessageHandlerInterceptorChain<CommandMessage> chain,
         ProcessingContext ctx
 ) {
     if (!tenantId.equals(command.metaData().get("tenantId"))) {
         return MessageStream.failed(new AccessDeniedException("Wrong tenant"));
     }
     return chain.proceed(command, ctx);
 }
 
Since:
5.2.0
Author:
Allard Buijze
See Also: