Annotation Interface QueryHandlerInterceptor


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

An annotated interceptor is invoked before every @QueryHandler 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(query, ctx); it can also short-circuit by returning without calling proceed.

Example before-interceptor:


 @QueryHandlerInterceptor
 void audit(QueryMessage query) {
     auditLog.record(query.qualifiedName());
 }
 

Example surround-interceptor with short-circuit:


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