Class DelayedProcessor<T extends DelayedProcessor.Item>

  • Type Parameters:
    T - Type of item to process.
    All Implemented Interfaces:
    java.lang.AutoCloseable

    public class DelayedProcessor<T extends DelayedProcessor.Item>
    extends java.lang.Object
    implements java.lang.AutoCloseable
    An async processor that executes items after a predetermined (yet fixed) delay of time. Items are added to the processor using process(T). The "clock" starts ticking from the moment they were added and the processor will guarantee that the item will not be processed before the preconfigured delay time (note that it may be executed some time after the delay expired, mostly due to other items in front of it that need execution as well). Each item must implement DelayedProcessor.Item and hence provide a unique DelayedProcessor.Item.key(). The Key is used to differentiate between different instances of DelayedProcessor.Item that refer to the same task (i.e., the same segment). If an DelayedProcessor.Item with a same Key is already queued up for processing (but has not yet finished executing), a call to process(T) with it as argument will not add it again.

    Items may be dequeued (cancelled) using cancel(java.lang.String). See that method for more details.

    • Constructor Summary

      Constructors 
      Constructor Description
      DelayedProcessor​(java.util.function.Function<T,​java.util.concurrent.CompletableFuture<java.lang.Void>> itemProcessor, java.time.Duration itemDelay, java.util.concurrent.ScheduledExecutorService executor, java.lang.String traceObjectId)
      Creates a new instance of the DelayedProcessor class.
    • Constructor Detail

      • DelayedProcessor

        public DelayedProcessor​(java.util.function.Function<T,​java.util.concurrent.CompletableFuture<java.lang.Void>> itemProcessor,
                                java.time.Duration itemDelay,
                                java.util.concurrent.ScheduledExecutorService executor,
                                java.lang.String traceObjectId)
        Creates a new instance of the DelayedProcessor class.
        Parameters:
        itemProcessor - A Function that, when given an item of type T, returns a CompletableFuture which will be completed when that item has finished processing. The DelayedProcessor does not care if the CompletableFuture completes normally or exceptionally - it will consider the item as processed when the CompletableFuture completes (one way or another).
        itemDelay - A Duration indicating the minimum amount of time that must elapse between when an item is first added using process(T) and when it is actually processed. Note that if multiple items with the same DelayedProcessor.Item.key() are added, only the time from the first invocation of process(T) is counted.
        executor - An Executor for async operations.
        traceObjectId - An identifier for logging purposes.
    • Method Detail

      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
      • cancel

        public void cancel​(@NonNull
                           @NonNull java.lang.String key)
        Cancels the DelayedProcessor.Item with given key, if any. If the given DelayedProcessor.Item hasn't yet begun executing, it will be removed from the queue. If it is in the process of executing, the execution cannot be stopped and this call will have no effect.
        Parameters:
        key - The Item's key.
      • createDelayedFuture

        protected java.util.concurrent.CompletableFuture<java.lang.Void> createDelayedFuture​(java.time.Duration delay)