Class ExecutionThrottler

java.lang.Object
com.codebarrel.automation.api.util.ExecutionThrottler

public class ExecutionThrottler extends Object
ExecutionThrottler is the utility that enables an operation/method to be executed with configured limits: time limit and data volume limit; Data volume limit is measured in "abstract" data chunks - it might be simple collection element or composite element (part of object graph) depending on the implementation. When one of the limits is passed operation should stop.

Actual state of the execution is encapsulated in ThrottlingContext. Throttled operation must be aware of ThrottlingContext so the ExecutionThrottler accepts operations in the form of ThrottlingContextConsumer. Operation should report processed data chunks to the ThrottlingContext and follow the state. Simply ThrottlingContextConsumer should finish when ThrottlingContext.shouldContinue() returns false.

Example usage:


     // this is the operation to be throttled
     List<String> generateData(final ThrottlingContext<String> context) {
         try(final Iterator<String> it = queryData()) {
             // loop contains additional check against ThrottlingContext in order to continue
             while(it.hasNext && context.shouldContinue()) {
                 // here is hoe each data chunk is reported
                 context.addDataChunk(it.next());
             }
         }
         return result;
     }

     List<String> generateDataThrottled() {
         // throttler that allows operation to run 5 seconds and collect maximum 1000 items
         final ExecutionThrottler executor = ExecutionThrottler.create(TimeUnit.SECONDS.toMillis(5L), 1000L);
         final ThrottlingContext<String> result = executor.executeFailFast(context -> generateData(context));
         // throttling context might be examined by accessing the right element of Pair
         final StateType executionState = result.getState();
         if (StateType.FINISHED.equals(executionState)) {
             LOG.debug("executed fully without throttling")
         } else if (StateType.ABORTED.equals(executionState)) {
             final AbortReasonType reason = result.right().getAbortReason().get();
             LOG.debug("execution throttled because of " + reason);
         }
         // operation result is the left item of Pair
         return result.getDataChunks();
     }
 
 
  • Field Details

    • MIN_EXECUTION_TIME

      public static final long MIN_EXECUTION_TIME
    • MAX_EXECUTION_TIME

      public static final long MAX_EXECUTION_TIME
  • Method Details

    • create

      public static ExecutionThrottler create()
      Creates ExecutionThrottler that runs within the same thread and has no throttling configured
      Returns:
      ExecutionThrottler instance
    • create

      public static ExecutionThrottler create(Long timeLimitMs, Long dataChunkLimit)
      Creates ExecutionThrottler that runs within the same thread and has execution limits. There is a time limit in milliseconds that means maximum execution time for any operation throttled by returned ExecutionThrottler. Data chunk limit means maximum number of data items reported in the throttling context, before the state is set to ThrottlingContext.StateType.ABORTED
      Parameters:
      timeLimitMs - maximum execution time for throttled operation
      dataChunkLimit - maximum number of data items
      Returns:
      ExecutionThrottler instance
    • executeFailSafe

      Executes given job with throttling in fail-safe mode. Exception raised during job execution is captured in ThrotlingContext and is the part of method result.
      Type Parameters:
      V - job generic result type
      Parameters:
      consumer - job to be throttled in form of Consumer that accepts throttling context
      Returns:
      ThrottlingContext with collected result
    • executeFailFast

      Executes given job with throttling in fail-fast mode. Exception raised during job execution is not handled and propagated in execution stack.
      Type Parameters:
      V - job generic result type
      Parameters:
      consumer - job to be throttled in form of Consumer that accepts throttling context
      Returns:
      ThrottlingContext with collected result