Top Description Inners Fields Constructors Methods
reactor.core.scheduler

public abstract Class Schedulers

extends Object
Class Inheritance
Imports
java.util.Collection, .LinkedHashMap, .Map, .Objects, .Optional, java.util.concurrent.Callable, .Executor, .ExecutorService, .Future, .RejectedExecutionException, .ScheduledExecutorService, .ThreadFactory, .ThreadPoolExecutor, .TimeUnit, java.util.concurrent.atomic.AtomicReference, java.util.function.BiConsumer, .BiFunction, .Function, .Supplier, io.micrometer.core.instrument.MeterRegistry, reactor.core.Disposable, .Exceptions, .Scannable, reactor.util.Logger, .Loggers, .Metrics, reactor.util.annotation.Nullable

Schedulers provides various Scheduler flavors usable by publishOn or subscribeOn :

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. All instances are returned in a initialized state.

Author
Stephane Maldini

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
public static interface
Schedulers.Factory

Public factory hook to override Schedulers behavior globally

public static class
Schedulers.Snapshot

It is also Disposable in case you don't want to restore the live Schedulers

Field Summary

Modifier and TypeField and Description
pack-priv static final String
pack-priv static final Supplier<Scheduler>
pack-priv static AtomicReference<Schedulers.CachedScheduler>
pack-priv static AtomicReference<Schedulers.CachedScheduler>
pack-priv static AtomicReference<Schedulers.CachedScheduler>
pack-priv static final Map<String, BiFunction<Scheduler, ScheduledExecutorService, ScheduledExecutorService>>
pack-priv static final Schedulers.Factory
public static final boolean
DEFAULT_BOUNDED_ELASTIC_ON_VIRTUAL_THREADS

Default execution of enqueued tasks on Thread#ofVirtual for the global boundedElastic() Scheduler, initialized by system property reactor.schedulers.defaultBoundedElasticOnVirtualThreads and falls back to false .

public static final 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.

public static final 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.

public static final 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.

pack-priv static volatile Schedulers.Factory
pack-priv static final String
pack-priv static final String
pack-priv static final String
pack-priv static final Logger
pack-priv static final String
pack-priv static BiConsumer<Thread, ? super Throwable>
private static final LinkedHashMap<String, BiConsumer<Thread, Throwable>>
private static Function<Runnable, Runnable>
private static final LinkedHashMap<String, Function<Runnable, Runnable>>
pack-priv static final String
pack-priv static final Supplier<Scheduler>
pack-priv static final String
pack-priv static final Supplier<Scheduler>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static boolean

Returns:

true if the decorator was added, false if a decorator was already present for this key.
addExecutorServiceDecorator
(String
the key under which to set up the decorator
key
,
BiFunction<Scheduler, ScheduledExecutorService, ScheduledExecutorService>
the executor service decorator to add, if key not already present.
decorator
)

Set up an additional ScheduledExecutorService decorator for a given key only if that key is not already present.

public static Scheduler

Returns:

the common boundedElastic instance, a Scheduler that dynamically creates 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 ones
boundedElastic
()

The common boundedElastic instance, a Scheduler that dynamically creates a bounded number of ExecutorService-based Workers, reusing them once the Workers have been shut down.

pack-priv static Schedulers.CachedScheduler

Returns:

a CachedScheduler to be reused, either pre-existing or created
cache
(AtomicReference<Schedulers.CachedScheduler>
the cache reference that holds the scheduler
reference
,
String
the "name" for the Scheduler's category/type
key
,
Supplier<Scheduler>
the Scheduler generator to use and wrap into a CachedScheduler. Note that in case of a race, an extraneous Scheduler can be created, but it'll get immediately disposed.
supplier
)

Get a CachedScheduler out of the reference or create one using the Supplier if the reference is empty, effectively creating a single instance to be reused as a default scheduler for the given key category.

private static BiConsumer<Thread, ? super Throwable>
public static ScheduledExecutorService

Returns:

the decorated ScheduledExecutorService, or the original if no decorator is set up
decorateExecutorService
(Scheduler owner, ScheduledExecutorService
the ScheduledExecutorService that the Scheduler wants to use originally
original
)

This method is aimed at Scheduler implementors, enabling custom implementations that are backed by a ScheduledExecutorService to also have said executors decorated (ie.

pack-priv static final void
pack-priv static Disposable
directSchedule(ScheduledExecutorService exec, Runnable task, Disposable parent, long delay, TimeUnit unit)

pack-priv static Disposable
directSchedulePeriodically(ScheduledExecutorService exec, Runnable task, long initialDelay, long period, TimeUnit unit)

public static void
disableMetrics()

Deprecated prefer using Micrometer#timedScheduler from the reactor-core-micrometer module. To be removed at the earliest in 3.6.0.
If enableMetrics() has been previously called, removes the decorator.
public static void
enableMetrics()

Deprecated prefer using Micrometer#timedScheduler from the reactor-core-micrometer module. To be removed at the earliest in 3.6.0.
If Micrometer is available, set-up a decorator that will instrument any ExecutorService that backs a Scheduler.
public static Scheduler

Returns:

a new Scheduler
fromExecutor
(Executor executor)

Create a Scheduler which uses a backing Executor to schedule Runnables for async operators.

public static Scheduler

Returns:

a new Scheduler
fromExecutor
(Executor executor, boolean
set to false if this Scheduler is used by "operators" that already conflate Runnable executions (publishOn, subscribeOn...)
trampoline
)

Create a Scheduler which uses a backing Executor to schedule Runnables for async operators.

public static Scheduler

Returns:

a new Scheduler
fromExecutorService
(ExecutorService executorService)

Create a Scheduler which uses a backing ExecutorService to schedule Runnables for async operators.

public static Scheduler

Returns:

a new Scheduler
fromExecutorService
(ExecutorService executorService, String executorName)

Create a Scheduler which uses a backing ExecutorService to schedule Runnables for async operators.

pack-priv static void
public static Scheduler

Returns:

a reusable Scheduler that executes tasks immediately instead of scheduling them
immediate
()

Executes tasks immediately instead of scheduling them.

public static boolean

Returns:

true if blocking is forbidden in this thread, false otherwise
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).

public static boolean

Returns:

true if blocking is forbidden in that thread, false otherwise
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).

public static Scheduler

Returns:

a new Scheduler that dynamically creates 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 ones
newBoundedElastic
(int
maximum number of underlying threads to create
threadCap
,
int
maximum number of tasks to enqueue when no more threads can be created. Can be Integer#MAX_VALUE for unbounded enqueueing.
queuedTaskCap
,
String
Thread prefix
name
)

Scheduler that dynamically creates a bounded number of ExecutorService-based Workers, reusing them once the Workers have been shut down.

public static Scheduler

Returns:

a new Scheduler that dynamically creates 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 ones
newBoundedElastic
(int
maximum number of underlying threads to create
threadCap
,
int
maximum number of tasks to enqueue when no more threads can be created. Can be Integer#MAX_VALUE for unbounded enqueueing.
queuedTaskCap
,
String
Thread prefix
name
,
int ttlSeconds)

Scheduler that dynamically creates a bounded number of ExecutorService-based Workers, reusing them once the Workers have been shut down.

public static Scheduler

Returns:

a new Scheduler that dynamically creates 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 ones
newBoundedElastic
(int
maximum number of underlying threads to create
threadCap
,
int
maximum number of tasks to enqueue when no more threads can be created. Can be Integer#MAX_VALUE for unbounded enqueueing.
queuedTaskCap
,
String
Thread prefix
name
,
int ttlSeconds, boolean
are backing threads daemon threads
daemon
)

Scheduler that dynamically creates a bounded number of ExecutorService-based Workers, reusing them once the Workers have been shut down.

public static Scheduler

Returns:

a new Scheduler that dynamically creates 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 ones
newBoundedElastic
(int
maximum number of underlying threads to create
threadCap
,
int
maximum number of tasks to enqueue when no more threads can be created. Can be Integer#MAX_VALUE for unbounded enqueueing.
queuedTaskCap
,
ThreadFactory
a ThreadFactory to use each thread initialization
threadFactory
,
int ttlSeconds)

Scheduler that dynamically creates a bounded number of ExecutorService-based Workers, reusing them once the Workers have been shut down.

public static Scheduler

Returns:

a new Scheduler that hosts a fixed pool of single-threaded ExecutorService-based workers and is suited for parallel work
newParallel
(String
Thread prefix
name
)

Scheduler that hosts a fixed pool of single-threaded ExecutorService-based workers and is suited for parallel work.

public static Scheduler

Returns:

a new Scheduler that hosts a fixed pool of single-threaded ExecutorService-based workers and is suited for parallel work
newParallel
(String
Thread prefix
name
,
int
Number of pooled workers.
parallelism
)

Scheduler that hosts a fixed pool of single-threaded ExecutorService-based workers and is suited for parallel work.

public static Scheduler

Returns:

a new Scheduler that hosts a fixed pool of single-threaded ExecutorService-based workers and is suited for parallel work
newParallel
(String
Thread prefix
name
,
int
Number of pooled workers.
parallelism
,
boolean
false if the Scheduler requires an explicit Scheduler#dispose() to exit the VM.
daemon
)

Scheduler that hosts a fixed pool of single-threaded ExecutorService-based workers and is suited for parallel work.

public static Scheduler

Returns:

a new Scheduler that hosts a fixed pool of single-threaded ExecutorService-based workers and is suited for parallel work
newParallel
(int
Number of pooled workers.
parallelism
,
ThreadFactory
a ThreadFactory to use for the fixed initialized number of Thread
threadFactory
)

Scheduler that hosts a fixed pool of single-threaded ExecutorService-based workers and is suited for parallel work.

public static Scheduler

Returns:

a new Scheduler that hosts a single-threaded ExecutorService-based worker
newSingle
(String
Component and thread name prefix
name
)

Scheduler that hosts a single-threaded ExecutorService-based worker.

public static Scheduler

Returns:

a new Scheduler that hosts a single-threaded ExecutorService-based worker
newSingle
(String
Component and thread name prefix
name
,
boolean
false if the Scheduler requires an explicit Scheduler#dispose() to exit the VM.
daemon
)

Scheduler that hosts a single-threaded ExecutorService-based worker.

public static Scheduler

Returns:

a new Scheduler that hosts a single-threaded ExecutorService-based worker
newSingle
(ThreadFactory
a ThreadFactory to use for the unique thread of the Scheduler
threadFactory
)

Scheduler that hosts a single-threaded ExecutorService-based worker.

pack-priv static Scheduler
newThreadPerTaskBoundedElastic(int threadCap, int queuedTaskCap, ThreadFactory threadFactory)

public static void
onHandleError(BiConsumer<Thread, ? super Throwable>
the new BiConsumer to set as the hook's anonymous part.
subHook
)

Define a hook anonymous part that is executed alongside keyed parts when a Scheduler has handled an error.

public static void
onHandleError(String
the String key identifying the hook part to set/replace.
key
,
BiConsumer<Thread, ? super Throwable>
the new hook part to set for the given key.
subHook
)

Define a keyed hook part that is executed alongside other parts when a Scheduler has handled an error.

public static Runnable

Returns:

decorated Runnable if any hook is registered, the original otherwise.
onSchedule
(Runnable
a Runnable submitted to a Scheduler
runnable
)

Applies the hooks registered with Schedulers#onScheduleHook(String, Function).

public static void
onScheduleHook(String
the key under which to set up the onScheduleHook sub-hook
key
,
Function<Runnable, Runnable>
the Runnable decorator to add (or replace, if key is already present)
decorator
)

Add or replace a named scheduling decorator.

public static Scheduler

Returns:

the common parallel instance, a Scheduler that hosts a fixed pool of single-threaded ExecutorService-based workers and is suited for parallel work
parallel
()

The common parallel instance, a Scheduler that hosts a fixed pool of single-threaded ExecutorService-based workers and is suited for parallel work.

public static BiFunction<Scheduler, ScheduledExecutorService, ScheduledExecutorService>

Returns:

the removed decorator, or null if none was set for that key
removeExecutorServiceDecorator
(String
the key for the executor service decorator to remove
key
)

Remove an existing ScheduledExecutorService decorator if it has been set up via addExecutorServiceDecorator(String, BiFunction).

public static void
resetFactory()

Re-apply default factory to Schedulers

public static void
resetFrom(Schedulers.Snapshot snapshot)

Replace the current Factory and shared Schedulers with the ones saved in a previously captured snapshot.

public static void
resetOnHandleError()

Reset the onHandleError(BiConsumer) hook to the default no-op behavior, erasing all sub-hooks that might have individually added via onHandleError(String, BiConsumer) or the whole hook set via onHandleError(BiConsumer).

public static void
resetOnHandleError(String key)

Reset a specific onHandleError hook part keyed to the provided String, removing that sub-hook if it has previously been defined via onHandleError(String, BiConsumer).

public static void
resetOnScheduleHook(String
the key for onScheduleHook sub-hook to remove
key
)

Reset a specific onScheduleHook sub-hook if it has been set up via onScheduleHook(String, Function).

public static void
resetOnScheduleHooks()

Remove all onScheduleHook sub-hooks.

pack-priv static final Object

Returns:

an equivalent of Scannable#scanUnsafe(Scannable.Attr) but that can also work on some implementations of Executor
scanExecutor
(Executor
the executor to introspect in a best effort manner.
executor
,
Scannable.Attr<T>
the key to scan for. CAPACITY and BUFFERED mainly.
key
)

Scan an Executor or ExecutorService, recognizing several special implementations.

public static void
setExecutorServiceDecorator(String
the key under which to set up the decorator
key
,
BiFunction<Scheduler, ScheduledExecutorService, ScheduledExecutorService>
the executor service decorator to add, if key not already present.
decorator
)

Set up an additional ScheduledExecutorService decorator for a given key, even if that key is already present.

public static void
setFactory(Schedulers.Factory
an arbitrary Factory instance.
factoryInstance
)

Replace Schedulers factories (newParallel, newSingle and newBoundedElastic).

public static Schedulers.Snapshot

Returns:

a Snapshot representing a restorable snapshot of Schedulers
setFactoryWithSnapshot
(Schedulers.Factory
an arbitrary Factory instance
newFactory
)

Replace Schedulers factories (newParallel, newSingle and newBoundedElastic).

public static void
shutdownNow()

Clear any cached Scheduler and call dispose on them.

public static Scheduler

Returns:

the common single instance, a Scheduler that hosts a single-threaded ExecutorService-based worker
single
()

The common single instance, a Scheduler that hosts a single-threaded ExecutorService-based worker.

public static Scheduler

Returns:

a wrapping Scheduler consistently returning a same worker from a source Scheduler
single
(Scheduler
a Scheduler to call upon to get the single reactor.core.scheduler.Scheduler.Worker
original
)

Wraps a single reactor.core.scheduler.Scheduler.Worker from some other Scheduler and provides reactor.core.scheduler.Scheduler.Worker services on top of it.

pack-priv static Disposable
pack-priv static Disposable
workerSchedulePeriodically(ScheduledExecutorService exec, Disposable.Composite tasks, Runnable task, long initialDelay, long period, TimeUnit unit)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait