public abstract class Schedulers extends Object
Schedulers
provides various Scheduler
flavors usable by publishOn
or subscribeOn
:
parallel()
: Optimized for fast Runnable
non-blocking executions single()
: Optimized for low-latency Runnable
one-off executions elastic()
: Optimized for longer executions, an alternative for blocking tasks where the number of active tasks (and threads) can grow indefinitelyboundedElastic()
: Optimized for longer executions, an alternative for blocking tasks where the number of active tasks (and threads) is cappedimmediate()
: to immediately run submitted Runnable
instead of scheduling them (somewhat of a no-op or "null object" Scheduler
)fromExecutorService(ExecutorService)
to create new instances around Executors
Factories prefixed with new
(eg. newBoundedElastic(int, int, String)
return a new instance of their flavor of Scheduler
,
while other factories like boundedElastic()
return a shared instance - which is the one used by operators requiring that flavor as their default Scheduler.
Modifier and Type | Class and Description |
---|---|
static interface |
Schedulers.Factory
Public factory hook to override Schedulers behavior globally
|
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BOUNDED_ELASTIC_QUEUESIZE
Default maximum number of enqueued tasks PER THREAD for the global
boundedElastic() Scheduler ,
initialized by system property reactor.schedulers.defaultBoundedElasticQueueSize and falls back to
a bound of 100 000 tasks per backing thread. |
static int |
DEFAULT_BOUNDED_ELASTIC_SIZE
Default maximum size for the global
boundedElastic() Scheduler , initialized
by system property reactor.schedulers.defaultBoundedElasticSize and falls back to 10 x number
of processors available to the runtime on init. |
static int |
DEFAULT_POOL_SIZE
Default pool size, initialized by system property
reactor.schedulers.defaultPoolSize
and falls back to the number of processors available to the runtime on init. |
Constructor and Description |
---|
Schedulers() |
Modifier and Type | Method and Description |
---|---|
static boolean |
addExecutorServiceDecorator(String key,
BiFunction<Scheduler,ScheduledExecutorService,ScheduledExecutorService> decorator)
Set up an additional
ScheduledExecutorService decorator for a given key
only if that key is not already present. |
static Scheduler |
boundedElastic()
Scheduler that dynamically creates a bounded number of ExecutorService-based
Workers, reusing them once the Workers have been shut down. |
static ScheduledExecutorService |
decorateExecutorService(Scheduler owner,
ScheduledExecutorService original)
This method is aimed at
Scheduler implementors, enabling custom implementations
that are backed by a ScheduledExecutorService to also have said executors
decorated (ie. |
static void |
disableMetrics()
If
enableMetrics() has been previously called, removes the decorator. |
static Scheduler |
elastic()
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down. |
static void |
enableMetrics()
If Micrometer is available, set-up a decorator that will instrument any
ExecutorService that backs a Scheduler . |
static Scheduler |
fromExecutor(Executor executor)
|
static Scheduler |
fromExecutor(Executor executor,
boolean trampoline)
|
static Scheduler |
fromExecutorService(ExecutorService executorService)
Create a
Scheduler which uses a backing ExecutorService to schedule
Runnables for async operators. |
static Scheduler |
fromExecutorService(ExecutorService executorService,
String executorName)
Create a
Scheduler which uses a backing ExecutorService to schedule
Runnables for async operators. |
static Scheduler |
immediate()
Executes tasks immediately instead of scheduling them.
|
static boolean |
isInNonBlockingThread()
Check if calling a Reactor blocking API in the current
Thread is forbidden
or not, by checking if the thread implements NonBlocking (in which case it is
forbidden and this method returns true ). |
static boolean |
isNonBlockingThread(Thread t)
Check if calling a Reactor blocking API in the given
Thread is forbidden
or not, by checking if the thread implements NonBlocking (in which case it is
forbidden and this method returns true ). |
static Scheduler |
newBoundedElastic(int threadCap,
int queuedTaskCap,
String name)
Scheduler that dynamically creates a bounded number of ExecutorService-based
Workers, reusing them once the Workers have been shut down. |
static Scheduler |
newBoundedElastic(int threadCap,
int queuedTaskCap,
String name,
int ttlSeconds)
Scheduler that dynamically creates a bounded number of ExecutorService-based
Workers, reusing them once the Workers have been shut down. |
static Scheduler |
newBoundedElastic(int threadCap,
int queuedTaskCap,
String name,
int ttlSeconds,
boolean daemon)
Scheduler that dynamically creates a bounded number of ExecutorService-based
Workers, reusing them once the Workers have been shut down. |
static Scheduler |
newBoundedElastic(int threadCap,
int queuedTaskCap,
ThreadFactory threadFactory,
int ttlSeconds)
Scheduler that dynamically creates a bounded number of ExecutorService-based
Workers, reusing them once the Workers have been shut down. |
static Scheduler |
newElastic(int ttlSeconds,
ThreadFactory threadFactory)
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down. |
static Scheduler |
newElastic(String name)
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down. |
static Scheduler |
newElastic(String name,
int ttlSeconds)
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down. |
static Scheduler |
newElastic(String name,
int ttlSeconds,
boolean daemon)
Scheduler that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down. |
static Scheduler |
newParallel(int parallelism,
ThreadFactory threadFactory)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. |
static Scheduler |
newParallel(String name)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. |
static Scheduler |
newParallel(String name,
int parallelism)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. |
static Scheduler |
newParallel(String name,
int parallelism,
boolean daemon)
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. |
static Scheduler |
newSingle(String name)
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. |
static Scheduler |
newSingle(String name,
boolean daemon)
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. |
static Scheduler |
newSingle(ThreadFactory threadFactory)
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. |
static void |
onHandleError(BiConsumer<Thread,? super Throwable> c)
Define a hook that is executed when a
Scheduler has
handled an error . |
static Runnable |
onSchedule(Runnable runnable)
Applies the hooks registered with
onScheduleHook(String, Function) . |
static void |
onScheduleHook(String key,
Function<Runnable,Runnable> decorator)
Add or replace a named scheduling
decorator . |
static Scheduler |
parallel()
Scheduler that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. |
static BiFunction<Scheduler,ScheduledExecutorService,ScheduledExecutorService> |
removeExecutorServiceDecorator(String key)
Remove an existing
ScheduledExecutorService decorator if it has been set up
via addExecutorServiceDecorator(String, BiFunction) . |
static void |
resetFactory()
Re-apply default factory to
Schedulers |
static void |
resetOnHandleError()
Reset the
onHandleError(BiConsumer) hook to the default no-op behavior. |
static void |
resetOnScheduleHook(String key)
Reset a specific onScheduleHook
sub-hook if it has been set up
via onScheduleHook(String, Function) . |
static void |
resetOnScheduleHooks()
Remove all onScheduleHook
sub-hooks . |
static void |
setExecutorServiceDecorator(String key,
BiFunction<Scheduler,ScheduledExecutorService,ScheduledExecutorService> decorator)
Set up an additional
ScheduledExecutorService decorator for a given key,
even if that key is already present. |
static void |
setFactory(Schedulers.Factory factoryInstance)
|
static void |
shutdownNow()
Clear any cached
Scheduler and call dispose on them. |
static Scheduler |
single()
Scheduler that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. |
static Scheduler |
single(Scheduler original)
Wraps a single
Scheduler.Worker from some other
Scheduler and provides Scheduler.Worker
services on top of it. |
public static final int DEFAULT_POOL_SIZE
reactor.schedulers.defaultPoolSize
and falls back to the number of processors available to the runtime on init.Runtime.availableProcessors()
public static final int DEFAULT_BOUNDED_ELASTIC_SIZE
boundedElastic()
Scheduler
, initialized
by system property reactor.schedulers.defaultBoundedElasticSize
and falls back to 10 x number
of processors available to the runtime on init.Runtime.availableProcessors()
,
boundedElastic()
public static final int DEFAULT_BOUNDED_ELASTIC_QUEUESIZE
boundedElastic()
Scheduler
,
initialized by system property reactor.schedulers.defaultBoundedElasticQueueSize
and falls back to
a bound of 100 000 tasks per backing thread.boundedElastic()
public static Scheduler fromExecutor(Executor executor)
Scheduler
which uses a backing Executor
to schedule
Runnables for async operators.
Tasks scheduled with workers of this Scheduler are not guaranteed to run in FIFO
order and strictly non-concurrently.
If FIFO order is desired, use trampoline parameter of fromExecutor(Executor, boolean)
public static Scheduler fromExecutor(Executor executor, boolean trampoline)
Scheduler
which uses a backing Executor
to schedule
Runnables for async operators.
Trampolining here means tasks submitted in a burst are queued by the Worker itself,
which acts as a sole task from the perspective of the ExecutorService
,
so no reordering (but also no threading).public static Scheduler fromExecutorService(ExecutorService executorService)
Scheduler
which uses a backing ExecutorService
to schedule
Runnables for async operators.
Prefer using fromExecutorService(ExecutorService, String)
,
especially if you plan on using metrics as this gives the executor a meaningful identifier.
executorService
- an ExecutorService
Scheduler
public static Scheduler fromExecutorService(ExecutorService executorService, String executorName)
Scheduler
which uses a backing ExecutorService
to schedule
Runnables for async operators.executorService
- an ExecutorService
Scheduler
public static Scheduler elastic()
Scheduler
that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down.
The maximum number of created thread pools is unbounded.
The default time-to-live for unused thread pools is 60 seconds, use the appropriate factory to set a different value.
This scheduler is not restartable.
Scheduler
that dynamically creates ExecutorService-based
Workers and caches the threads, reusing them once the Workers have been shut
downpublic static Scheduler boundedElastic()
Scheduler
that dynamically creates a bounded number of ExecutorService-based
Workers, reusing them once the Workers have been shut down. The underlying daemon
threads can be evicted if idle for more than 60
seconds.
The maximum number of created threads is bounded by a cap
(by default
ten times the number of available CPU cores, see DEFAULT_BOUNDED_ELASTIC_SIZE
).
The maximum number of task submissions that can be enqueued and deferred on each of these
backing threads is bounded (by default 100K additional tasks, see
DEFAULT_BOUNDED_ELASTIC_QUEUESIZE
). Past that point, a RejectedExecutionException
is thrown.
By order of preference, threads backing a new Scheduler.Worker
are
picked from the idle pool, created anew or reused from the busy pool. In the later case, a best effort
attempt at picking the thread backing the least amount of workers is made.
Note that if a thread is backing a low amount of workers, but these workers submit a lot of pending tasks, a second worker could end up being backed by the same thread and see tasks rejected. The picking of the backing thread is also done once and for all at worker creation, so tasks could be delayed due to two workers sharing the same backing thread and submitting long-running tasks, despite another backing thread becoming idle in the meantime.
Scheduler
that dynamically create workers with an upper bound to
the number of backing threads and after that on the number of enqueued tasks,
that reuses threads and evict idle onespublic static Scheduler parallel()
Scheduler
that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work.Scheduler
that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler immediate()
As a consequence tasks run on the thread that submitted them (eg. the
thread on which an operator is currently processing its onNext/onComplete/onError signals).
This Scheduler
is typically used as a "null object" for APIs that require a
Scheduler but one doesn't want to change threads.
Scheduler
that executes tasks immediately instead of scheduling thempublic static Scheduler newElastic(String name)
Scheduler
that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down.
The maximum number of created thread pools is unbounded.
The default time-to-live for unused thread pools is 60 seconds, use the appropriate factory to set a different value.
This scheduler is not restartable.
name
- Thread prefixScheduler
that dynamically creates ExecutorService-based
Workers and caches the thread pools, reusing them once the Workers have been shut
downpublic static Scheduler newElastic(String name, int ttlSeconds)
Scheduler
that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down.
The maximum number of created thread pools is unbounded.
This scheduler is not restartable.
name
- Thread prefixttlSeconds
- Time-to-live for an idle Scheduler.Worker
Scheduler
that dynamically creates ExecutorService-based
Workers and caches the thread pools, reusing them once the Workers have been shut
downpublic static Scheduler newElastic(String name, int ttlSeconds, boolean daemon)
Scheduler
that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down.
The maximum number of created thread pools is unbounded.
This scheduler is not restartable.
name
- Thread prefixttlSeconds
- Time-to-live for an idle Scheduler.Worker
daemon
- false if the Scheduler
requires an explicit Scheduler.dispose()
to exit the VM.Scheduler
that dynamically creates ExecutorService-based
Workers and caches the thread pools, reusing them once the Workers have been shut
downpublic static Scheduler newElastic(int ttlSeconds, ThreadFactory threadFactory)
Scheduler
that dynamically creates ExecutorService-based Workers and caches
the thread pools, reusing them once the Workers have been shut down.
The maximum number of created thread pools is unbounded.
This scheduler is not restartable.
ttlSeconds
- Time-to-live for an idle Scheduler.Worker
threadFactory
- a ThreadFactory
to use each thread initializationScheduler
that dynamically creates ExecutorService-based
Workers and caches the thread pools, reusing them once the Workers have been shut
downpublic static Scheduler newBoundedElastic(int threadCap, int queuedTaskCap, String name)
Scheduler
that dynamically creates a bounded number of ExecutorService-based
Workers, reusing them once the Workers have been shut down. The underlying (user)
threads can be evicted if idle for more than 60
seconds.
The maximum number of created threads is bounded by the provided threadCap
.
The maximum number of task submissions that can be enqueued and deferred on each of these
backing threads is bounded by the provided queuedTaskCap
. Past that point,
a RejectedExecutionException
is thrown.
By order of preference, threads backing a new Scheduler.Worker
are
picked from the idle pool, created anew or reused from the busy pool. In the later case, a best effort
attempt at picking the thread backing the least amount of workers is made.
Note that if a thread is backing a low amount of workers, but these workers submit a lot of pending tasks, a second worker could end up being backed by the same thread and see tasks rejected. The picking of the backing thread is also done once and for all at worker creation, so tasks could be delayed due to two workers sharing the same backing thread and submitting long-running tasks, despite another backing thread becoming idle in the meantime.
This scheduler is restartable. Backing threads are user threads, so they will prevent the JVM
from exiting until their worker has been disposed AND they've been evicted by TTL, or the whole
scheduler has been disposed
.
threadCap
- maximum number of underlying threads to createqueuedTaskCap
- maximum number of tasks to enqueue when no more threads can be created. Can be Integer.MAX_VALUE
for unbounded enqueueing.name
- Thread prefixScheduler
that dynamically create workers with an upper bound to
the number of backing threads and after that on the number of enqueued tasks,
that reuses threads and evict idle onespublic static Scheduler newBoundedElastic(int threadCap, int queuedTaskCap, String name, int ttlSeconds)
Scheduler
that dynamically creates a bounded number of ExecutorService-based
Workers, reusing them once the Workers have been shut down. The underlying (user)
threads can be evicted if idle for more than the provided ttlSeconds
.
The maximum number of created threads is bounded by the provided threadCap
.
The maximum number of task submissions that can be enqueued and deferred on each of these
backing threads is bounded by the provided queuedTaskCap
. Past that point,
a RejectedExecutionException
is thrown.
By order of preference, threads backing a new Scheduler.Worker
are
picked from the idle pool, created anew or reused from the busy pool. In the later case, a best effort
attempt at picking the thread backing the least amount of workers is made.
Note that if a thread is backing a low amount of workers, but these workers submit a lot of pending tasks, a second worker could end up being backed by the same thread and see tasks rejected. The picking of the backing thread is also done once and for all at worker creation, so tasks could be delayed due to two workers sharing the same backing thread and submitting long-running tasks, despite another backing thread becoming idle in the meantime.
This scheduler is restartable. Backing threads are user threads, so they will prevent the JVM
from exiting until their worker has been disposed AND they've been evicted by TTL, or the whole
scheduler has been disposed
.
threadCap
- maximum number of underlying threads to createqueuedTaskCap
- maximum number of tasks to enqueue when no more threads can be created. Can be Integer.MAX_VALUE
for unbounded enqueueing.name
- Thread prefixttlSeconds
- Time-to-live for an idle Scheduler.Worker
Scheduler
that dynamically create workers with an upper bound to
the number of backing threads and after that on the number of enqueued tasks,
that reuses threads and evict idle onespublic static Scheduler newBoundedElastic(int threadCap, int queuedTaskCap, String name, int ttlSeconds, boolean daemon)
Scheduler
that dynamically creates a bounded number of ExecutorService-based
Workers, reusing them once the Workers have been shut down. The underlying (user or daemon)
threads can be evicted if idle for more than the provided ttlSeconds
.
The maximum number of created threads is bounded by the provided threadCap
.
The maximum number of task submissions that can be enqueued and deferred on each of these
backing threads is bounded by the provided queuedTaskCap
. Past that point,
a RejectedExecutionException
is thrown.
By order of preference, threads backing a new Scheduler.Worker
are
picked from the idle pool, created anew or reused from the busy pool. In the later case, a best effort
attempt at picking the thread backing the least amount of workers is made.
Note that if a thread is backing a low amount of workers, but these workers submit a lot of pending tasks, a second worker could end up being backed by the same thread and see tasks rejected. The picking of the backing thread is also done once and for all at worker creation, so tasks could be delayed due to two workers sharing the same backing thread and submitting long-running tasks, despite another backing thread becoming idle in the meantime.
This scheduler is restartable. Depending on the daemon
parameter, backing threads can be
user threads or daemon threads. Note that user threads will prevent the JVM from exiting until their
worker has been disposed AND they've been evicted by TTL, or the whole scheduler has been
disposed
.
threadCap
- maximum number of underlying threads to createqueuedTaskCap
- maximum number of tasks to enqueue when no more threads can be created. Can be Integer.MAX_VALUE
for unbounded enqueueing.name
- Thread prefixttlSeconds
- Time-to-live for an idle Scheduler.Worker
daemon
- are backing threads daemon threads
Scheduler
that dynamically create workers with an upper bound to
the number of backing threads and after that on the number of enqueued tasks,
that reuses threads and evict idle onespublic static Scheduler newBoundedElastic(int threadCap, int queuedTaskCap, ThreadFactory threadFactory, int ttlSeconds)
Scheduler
that dynamically creates a bounded number of ExecutorService-based
Workers, reusing them once the Workers have been shut down. The underlying (user)
threads can be evicted if idle for more than the provided ttlSeconds
.
The maximum number of created threads is bounded by the provided threadCap
.
The maximum number of task submissions that can be enqueued and deferred on each of these
backing threads is bounded by the provided queuedTaskCap
. Past that point,
a RejectedExecutionException
is thrown.
By order of preference, threads backing a new Scheduler.Worker
are
picked from the idle pool, created anew or reused from the busy pool. In the later case, a best effort
attempt at picking the thread backing the least amount of workers is made.
Note that if a thread is backing a low amount of workers, but these workers submit a lot of pending tasks, a second worker could end up being backed by the same thread and see tasks rejected. The picking of the backing thread is also done once and for all at worker creation, so tasks could be delayed due to two workers sharing the same backing thread and submitting long-running tasks, despite another backing thread becoming idle in the meantime.
This scheduler is restartable. Backing threads are created by the provided ThreadFactory
,
which can decide whether to create user threads or daemon threads. Note that user threads
will prevent the JVM from exiting until their worker has been disposed AND they've been evicted by TTL,
or the whole scheduler has been disposed
.
threadCap
- maximum number of underlying threads to createqueuedTaskCap
- maximum number of tasks to enqueue when no more threads can be created. Can be Integer.MAX_VALUE
for unbounded enqueueing.threadFactory
- a ThreadFactory
to use each thread initializationttlSeconds
- Time-to-live for an idle Scheduler.Worker
Scheduler
that dynamically create workers with an upper bound to
the number of backing threads and after that on the number of enqueued tasks,
that reuses threads and evict idle onespublic static Scheduler newParallel(String name)
Scheduler
that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. This type of Scheduler
detects and
rejects usage of blocking Reactor APIs.name
- Thread prefixScheduler
that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newParallel(String name, int parallelism)
Scheduler
that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. This type of Scheduler
detects and
rejects usage of blocking Reactor APIs.name
- Thread prefixparallelism
- Number of pooled workers.Scheduler
that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newParallel(String name, int parallelism, boolean daemon)
Scheduler
that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work. This type of Scheduler
detects and
rejects usage of blocking Reactor APIs.name
- Thread prefixparallelism
- Number of pooled workers.daemon
- false if the Scheduler
requires an explicit Scheduler.dispose()
to exit the VM.Scheduler
that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newParallel(int parallelism, ThreadFactory threadFactory)
Scheduler
that hosts a fixed pool of single-threaded ExecutorService-based
workers and is suited for parallel work.parallelism
- Number of pooled workers.threadFactory
- a ThreadFactory
to use for the fixed initialized
number of Thread
Scheduler
that hosts a fixed pool of single-threaded
ExecutorService-based workers and is suited for parallel workpublic static Scheduler newSingle(String name)
Scheduler
that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. This type of Scheduler
detects and rejects usage
* of blocking Reactor APIs.name
- Component and thread name prefixScheduler
that hosts a single-threaded ExecutorService-based
workerpublic static Scheduler newSingle(String name, boolean daemon)
Scheduler
that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. This type of Scheduler
detects and rejects usage
of blocking Reactor APIs.name
- Component and thread name prefixdaemon
- false if the Scheduler
requires an explicit Scheduler.dispose()
to exit the VM.Scheduler
that hosts a single-threaded ExecutorService-based
workerpublic static Scheduler newSingle(ThreadFactory threadFactory)
Scheduler
that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work.threadFactory
- a ThreadFactory
to use for the unique thread of the
Scheduler
Scheduler
that hosts a single-threaded ExecutorService-based
workerpublic static void onHandleError(BiConsumer<Thread,? super Throwable> c)
Scheduler
has
handled an error
. Note that it is executed after
the error has been passed to the thread uncaughtErrorHandler, which is not the
case when a fatal error occurs (see Exceptions.throwIfJvmFatal(Throwable)
).c
- the new hook to set.public static boolean isInNonBlockingThread()
Thread
is forbidden
or not, by checking if the thread implements NonBlocking
(in which case it is
forbidden and this method returns true
).true
if blocking is forbidden in this thread, false
otherwisepublic static boolean isNonBlockingThread(Thread t)
Thread
is forbidden
or not, by checking if the thread implements NonBlocking
(in which case it is
forbidden and this method returns true
).true
if blocking is forbidden in that thread, false
otherwisepublic static void enableMetrics()
ExecutorService
that backs a Scheduler
.
No-op if Micrometer isn't available.
This instrumentation sends data to the Micrometer Global Registry.public static void disableMetrics()
enableMetrics()
has been previously called, removes the decorator.
No-op if enableMetrics()
hasn't been called.public static void resetFactory()
Schedulers
public static void resetOnHandleError()
onHandleError(BiConsumer)
hook to the default no-op behavior.public static void setFactory(Schedulers.Factory factoryInstance)
Schedulers
factories (newParallel
,
newSingle
and newElastic
). Also
shutdown Schedulers from the cached factories (like single()
) in order to
also use these replacements, re-creating the shared schedulers from the new factory
upon next use.
This method should be called safely and with caution, typically on app startup.
factoryInstance
- an arbitrary Schedulers.Factory
instance.public static boolean addExecutorServiceDecorator(String key, BiFunction<Scheduler,ScheduledExecutorService,ScheduledExecutorService> decorator)
ScheduledExecutorService
decorator for a given key
only if that key is not already present.
The decorator is a BiFunction
taking the Scheduler and the backing
ScheduledExecutorService
as second argument. It returns the
decorated ScheduledExecutorService
.
key
- the key under which to set up the decoratordecorator
- the executor service decorator to add, if key not already present.setExecutorServiceDecorator(String, BiFunction)
,
removeExecutorServiceDecorator(String)
,
onScheduleHook(String, Function)
public static void setExecutorServiceDecorator(String key, BiFunction<Scheduler,ScheduledExecutorService,ScheduledExecutorService> decorator)
ScheduledExecutorService
decorator for a given key,
even if that key is already present.
The decorator is a BiFunction
taking the Scheduler and the backing
ScheduledExecutorService
as second argument. It returns the
decorated ScheduledExecutorService
.
key
- the key under which to set up the decoratordecorator
- the executor service decorator to add, if key not already present.addExecutorServiceDecorator(String, BiFunction)
,
removeExecutorServiceDecorator(String)
,
onScheduleHook(String, Function)
public static BiFunction<Scheduler,ScheduledExecutorService,ScheduledExecutorService> removeExecutorServiceDecorator(String key)
ScheduledExecutorService
decorator if it has been set up
via addExecutorServiceDecorator(String, BiFunction)
.
In case the decorator implements Disposable
, it is also
disposed
.
key
- the key for the executor service decorator to removeaddExecutorServiceDecorator(String, BiFunction)
,
setExecutorServiceDecorator(String, BiFunction)
public static ScheduledExecutorService decorateExecutorService(Scheduler owner, ScheduledExecutorService original)
Scheduler
implementors, enabling custom implementations
that are backed by a ScheduledExecutorService
to also have said executors
decorated (ie. for instrumentation purposes).
It applies the decorators added via
addExecutorServiceDecorator(String, BiFunction)
, so it shouldn't be added
as a decorator. Note also that decorators are not guaranteed to be idempotent, so
this method should be called only once per executor.
owner
- a Scheduler
that owns the ScheduledExecutorService
original
- the ScheduledExecutorService
that the Scheduler
wants to use originallyScheduledExecutorService
, or the original if no decorator is set upaddExecutorServiceDecorator(String, BiFunction)
,
removeExecutorServiceDecorator(String)
public static void onScheduleHook(String key, Function<Runnable,Runnable> decorator)
decorator
. With subsequent calls
to this method, the onScheduleHook hook can be a composite of several sub-hooks, each
with a different key.
The sub-hook is a Function
taking the scheduled Runnable
.
It returns the decorated Runnable
.
key
- the key under which to set up the onScheduleHook sub-hookdecorator
- the Runnable
decorator to add (or replace, if key is already present)resetOnScheduleHook(String)
,
resetOnScheduleHooks()
public static void resetOnScheduleHook(String key)
sub-hook
if it has been set up
via onScheduleHook(String, Function)
.key
- the key for onScheduleHook sub-hook to removeonScheduleHook(String, Function)
,
resetOnScheduleHooks()
public static void resetOnScheduleHooks()
sub-hooks
.public static Runnable onSchedule(Runnable runnable)
onScheduleHook(String, Function)
.public static void shutdownNow()
Scheduler
and call dispose on them.public static Scheduler single()
Scheduler
that hosts a single-threaded ExecutorService-based worker and is
suited for parallel work. Will cache the returned schedulers for subsequent calls until dispose.Scheduler
that hosts a single-threaded
ExecutorService-based workerpublic static Scheduler single(Scheduler original)
Scheduler.Worker
from some other
Scheduler
and provides Scheduler.Worker
services on top of it.
Use the Scheduler.dispose()
to release the wrapped worker.
original
- a Scheduler
to call upon to get the single Scheduler.Worker
Scheduler
consistently returning a same worker from a
source Scheduler