Package io.pravega.common.concurrent
Class DelayedProcessor<T extends DelayedProcessor.Item>
- java.lang.Object
-
- io.pravega.common.concurrent.DelayedProcessor<T>
-
- 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.AutoCloseableAn async processor that executes items after a predetermined (yet fixed) delay of time. Items are added to the processor usingprocess(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 implementDelayedProcessor.Itemand hence provide a uniqueDelayedProcessor.Item.key(). The Key is used to differentiate between different instances ofDelayedProcessor.Itemthat refer to the same task (i.e., the same segment). If anDelayedProcessor.Itemwith a same Key is already queued up for processing (but has not yet finished executing), a call toprocess(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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceDelayedProcessor.ItemDefines aDelayedProcessoritem to process..
-
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 theDelayedProcessorclass.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcancel(@NonNull java.lang.String key)Cancels theDelayedProcessor.Itemwith given key, if any.voidclose()protected java.util.concurrent.CompletableFuture<java.lang.Void>createDelayedFuture(java.time.Duration delay)voidprocess(T item)Processes a newDelayedProcessor.Item.
-
-
-
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 theDelayedProcessorclass.- 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. TheDelayedProcessordoes 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- ADurationindicating the minimum amount of time that must elapse between when an item is first added usingprocess(T)and when it is actually processed. Note that if multiple items with the sameDelayedProcessor.Item.key()are added, only the time from the first invocation ofprocess(T)is counted.executor- An Executor for async operations.traceObjectId- An identifier for logging purposes.
-
-
Method Detail
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
process
public void process(@NonNull T item)Processes a newDelayedProcessor.Item. The item will NOT be queued up if there exists anotherDelayedProcessor.Itemwith the sameDelayedProcessor.Item.key()already in the queue. TheDelayedProcessor.Itemwill be processed using the handler passed in via this class' constructor at the earliest after the delay specified in this class' constructor.- Parameters:
item- The Item to process.
-
cancel
public void cancel(@NonNull @NonNull java.lang.String key)Cancels theDelayedProcessor.Itemwith given key, if any. If the givenDelayedProcessor.Itemhasn'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)
-
-