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(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.
|
Modifier and Type | Method and 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 a
Consumer 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 a
Consumer as they
become available. |
<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(SubmitterScheduler scheduler, long pollFrequency)
scheduler
- Scheduler to run polling task onpollFrequency
- Time in milliseconds to wait between polling eventspublic Poller(SubmitterScheduler 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 completetionpublic <T> ListenableFuture<?> consumeQueue(java.util.Queue<? extends T> queue, java.util.function.Consumer<? super T> consumer)
Consumer
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 the Consumer
as fast as possible. The
Consumer
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 returned ListenableFuture
can be
ignored.
If any errors occur ExceptionUtils.handleException(Throwable)
will be invoked.
T
- The type of object to consume from the queuequeue
- The queue to poll items from as they are availableconsumer
- The Consumer
to provide items to as they are availablecancel(boolean)
public <T> ListenableFuture<?> consumeQueue(java.util.Queue<? extends T> queue, java.util.function.Consumer<? super T> consumer, ExceptionHandler exceptionHandler)
Consumer
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 the Consumer
as fast as possible. The
Consumer
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
was null
), or if it is canceled. Invoking
Future.cancel(boolean)
on the returned future is how you would stop
providing queue items to the Consumer
. If an exceptionHandler
is provided,
and you never want to stop with a cancel(boolean)
, the returned future can be
ignored.
T
- The type of object to consume from the queuequeue
- The queue to poll items from as they are availableconsumer
- The Consumer
to provide items to as they are availableexceptionHandler
- An optional handler for unexpected errors, or null
to stop consumption on errorcancel(boolean)
, and will report unhandled errors