Class Poller
- java.lang.Object
-
- org.threadly.concurrent.Poller
-
public class Poller extends java.lang.Object
Simple class for watching a condition and getting notified when a state has changed. One handy tool is the ability to transition java'sFuture
into Threadly's much nicerListenableFuture
.The frequency at which this polls should be determined based off how cheap polling is, and how many items will be polled on average.
If being allowed to garbage collect, this poller will continue to schedule itself as long as there are outstanding futures. Once all have completed (from timeout or naturally), then this will stop scheduling itself to poll for updates. Thus no explicit cleanup is needed. As long as the
Supplier
's are quick/fast (Future
conversions are always quick/fast), it's most efficient to reuse thePoller
instance. But if you need dynamic timeout's/max wait time you could construct aPoller
and toss it away once you get the returnedListenableFuture
from it.- Since:
- 5.0
-
-
Constructor Summary
Constructors Constructor Description Poller(SubmitterScheduler scheduler, long pollFrequency)
Construct a new poller which will run on the provided scheduler, and run at the specified frequency.Poller(SubmitterScheduler scheduler, long pollFrequency, long maxWaitTime)
Construct a new poller which will run on the provided scheduler, and run at the specified frequency.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> ListenableFuture<?>
consumeQueue(java.util.Queue<? extends T> queue, java.util.function.Consumer<? super T> consumer)
Consumes from a queue, checking for items and providing them to aConsumer
as they become available.<T> ListenableFuture<?>
consumeQueue(java.util.Queue<? extends T> queue, java.util.function.Consumer<? super T> consumer, ExceptionHandler exceptionHandler)
Consumes from a queue, checking for items and providing them to aConsumer
as they become available.<T> ListenableFuture<T>
watch(java.util.concurrent.Future<? extends T> f)
Convert a conventionalFuture
into aListenableFuture
.ListenableFuture<?>
watch(java.util.function.Supplier<java.lang.Boolean> p)
Watch suppliers returned condition.
-
-
-
Constructor Detail
-
Poller
public Poller(SubmitterScheduler scheduler, long pollFrequency)
Construct a new poller which will run on the provided scheduler, and run at the specified frequency.- Parameters:
scheduler
- Scheduler to run polling task onpollFrequency
- Time in milliseconds to wait between polling events
-
Poller
public Poller(SubmitterScheduler scheduler, long pollFrequency, long maxWaitTime)
Construct a new poller which will run on the provided scheduler, and run at the specified frequency.This constructor additionally allows you to specify the maximum time in millseconds we should wait for the condition to become true. At this point if we are still not seeing our expected polling state, then the return future will be canceled.
- Parameters:
scheduler
- Scheduler to run polling task onpollFrequency
- Time in milliseconds to wait between polling eventsmaxWaitTime
- Maximum time in milliseconds till returned futures should be canceled
-
-
Method Detail
-
watch
public ListenableFuture<?> watch(java.util.function.Supplier<java.lang.Boolean> p)
Watch suppliers returned condition. Once Supplier is witnessed in thetrue
state, the returned future is completed. Listeners and FutureCallback's executed on the returned future without a specified pool will run on the polling thread, and so should be kept to a minimum.- Parameters:
p
- Supplier to provide state for when poll has completed as expected- Returns:
- Future to complete once boolean state is witnessed as
true
-
watch
public <T> ListenableFuture<T> watch(java.util.concurrent.Future<? extends T> f)
Convert a conventionalFuture
into aListenableFuture
. As poller runs it will check if the provided future has completed. Once it does complete the returned future will also complete in the exact same way. Canceling the returned future will have NO impact on the provided future (and thus the use with a timeout is not a concern to interrupting the provided future). Because this is only checked at poll intervals the returned future's completion will be delayed by that polling delay.- Type Parameters:
T
- Type of object returned from future- Parameters:
f
- Future to monitor for completetion- Returns:
- ListenableFuture that will provide the result from the source future
-
consumeQueue
public <T> ListenableFuture<?> consumeQueue(java.util.Queue<? extends T> queue, java.util.function.Consumer<? super T> consumer)
Consumes from a queue, checking for items and providing them to aConsumer
as they become available. The poll interval is not the minimum consumption speed, but rather the resolution / delay that will occur after the queue is seen in an empty state. If items are available they will be provided to theConsumer
as fast as possible. TheConsumer
will not be invoked in parallel, so if concurrent consumption is desired it must execute to a pool.Because queue consumption is likely a long running process, this operation will ignore any timeout provided to the
Poller(SubmitterScheduler, long, long)
constructor.The returned future will never complete on its own. It can only be used for stopping the consumption from the queue by invoking
Future.cancel(boolean)
. If you never want to stop the queue consumption then the returnedListenableFuture
can be ignored.If any errors occur
ExceptionUtils.handleException(Throwable)
will be invoked.- Type Parameters:
T
- The type of object to consume from the queue- Parameters:
queue
- The queue to poll items from as they are availableconsumer
- TheConsumer
to provide items to as they are available- Returns:
- A future which can stop consumption through
cancel(boolean)
- Since:
- 5.37
-
consumeQueue
public <T> ListenableFuture<?> consumeQueue(java.util.Queue<? extends T> queue, java.util.function.Consumer<? super T> consumer, ExceptionHandler exceptionHandler)
Consumes from a queue, checking for items and providing them to aConsumer
as they become available. The poll interval is not the minimum consumption speed, but rather the resolution / delay that will occur after the queue is seen in an empty state. If items are available they will be provided to theConsumer
as fast as possible. TheConsumer
will not be invoked in parallel, so if concurrent consumption is desired it must execute to a pool.Because queue consumption is likely a long running process, this operation will ignore any timeout provided to the
Poller(SubmitterScheduler, long, long)
constructor.The returned future may never complete. It will only complete if an error occurs (and parameter
exceptionHandler
wasnull
), or if it is canceled. InvokingFuture.cancel(boolean)
on the returned future is how you would stop providing queue items to theConsumer
. If anexceptionHandler
is provided, and you never want to stop with acancel(boolean)
, the returned future can be ignored.- Type Parameters:
T
- The type of object to consume from the queue- Parameters:
queue
- The queue to poll items from as they are availableconsumer
- TheConsumer
to provide items to as they are availableexceptionHandler
- An optional handler for unexpected errors, ornull
to stop consumption on error- Returns:
- A future which can stop consumption through
cancel(boolean)
, and will report unhandled errors - Since:
- 5.37
-
-