public class Poller
extends java.lang.Object
Future
into Threadly's much nicer
ListenableFuture
.
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 the Poller
instance. But if you need dynamic
timeout's/max wait time you could construct a Poller
and toss it away once you get the
returned ListenableFuture
from it.
Constructor and Description |
---|
Poller(SchedulerService scheduler,
long pollFrequency)
Construct a new poller which will run on the provided scheduler, and run at the specified
frequency.
|
Poller(SchedulerService scheduler,
long pollFrequency,
long maxWaitTime)
Construct a new poller which will run on the provided scheduler, and run at the specified
frequency.
|
Modifier and Type | Method and Description |
---|---|
<T> ListenableFuture<T> |
watch(java.util.concurrent.Future<? extends T> f)
Convert a conventional
Future into a ListenableFuture . |
ListenableFuture<?> |
watch(java.util.function.Supplier<java.lang.Boolean> p)
Watch suppliers returned condition.
|
public Poller(SchedulerService scheduler, long pollFrequency)
scheduler
- Scheduler to run polling task onpollFrequency
- Time in milliseconds to wait between polling eventspublic Poller(SchedulerService scheduler, long pollFrequency, long maxWaitTime)
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.
scheduler
- Scheduler to run polling task onpollFrequency
- Time in milliseconds to wait between polling eventsmaxWaitTime
- Maximum time in milliseconds till returned futures should be canceledpublic ListenableFuture<?> watch(java.util.function.Supplier<java.lang.Boolean> p)
true
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.p
- Supplier to provide state for when poll has completed as expectedtrue
public <T> ListenableFuture<T> watch(java.util.concurrent.Future<? extends T> f)
Future
into a ListenableFuture
. 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.T
- Type of object returned from futuref
- Future to monitor for completetion