11.2.1Filter Hook Interceptors

 

A filter hook is a hook that wraps a supplied function (i.e. Supplier). Filter hooks allow implementers to run custom code around the supplied function's execution (similar to Java Servlet Filters). Implementers can specify logic that should be executed:

  1. Before the supplied function call is made

  2. When the supplied function call throws an exception

  3. After the supplied function call is made

The example below shows how a Filter Hook Interceptor can be implemented with the BATCH2_CHUNK_PROCESS_FILTER pointcut:

import ca.uhn.fhir.batch2.model.JobInstance;
import ca.uhn.fhir.batch2.model.WorkChunk;
import ca.uhn.fhir.interceptor.api.Hook;
import ca.uhn.fhir.interceptor.api.IBaseInterceptorBroadcaster.IInterceptorFilterHook;
import ca.uhn.fhir.interceptor.api.Pointcut;

public class WorkChunkProcessingInterceptor {

   @Hook(Pointcut.BATCH2_CHUNK_PROCESS_FILTER)
   public IInterceptorFilterHook batch2ProcessFilter(JobInstance theJobInstance, WorkChunk theWorkChunk) {
      return theContinuation -> {
         try {
            // Perform pre-processing logic before the work chunk is processed

            // Process the work chunk (Note: If the continuation is not ran, an IllegalStateException will be
            // thrown)
            theContinuation.run();
         } catch (Exception e) {
            // Handle any exceptions that occur during work chunk processing

            // rethrow the exception
            throw e;
         } finally {
            // Perform any necessary cleanup or final operations
         }
      };
   }
}