public class ReactiveAdapterRegistry extends Object
Publisher
to/from various
async/reactive types such as CompletableFuture
, RxJava Flowable
, etc.
This is designed to complement Spring's Reactor Mono
/Flux
support while
also being usable without Reactor, e.g. just for org.reactivestreams
bridging.
By default, depending on classpath availability, adapters are registered for Reactor
(including CompletableFuture
and Flow.Publisher
adapters), RxJava 3,
Kotlin Coroutines' Deferred
(bridged via Reactor) and SmallRye Mutiny 1.x.
Note: As of Spring Framework 5.3.11, support for RxJava 1.x and 2.x is deprecated in favor of RxJava 3.
Modifier and Type | Class and Description |
---|---|
static class |
ReactiveAdapterRegistry.SpringCoreBlockHoundIntegration
BlockHoundIntegration for spring-core classes. |
Constructor and Description |
---|
ReactiveAdapterRegistry()
Create a registry and auto-register default adapters.
|
Modifier and Type | Method and Description |
---|---|
ReactiveAdapter |
getAdapter(Class<?> reactiveType)
Get the adapter for the given reactive type.
|
ReactiveAdapter |
getAdapter(Class<?> reactiveType,
Object source)
Get the adapter for the given reactive type.
|
static ReactiveAdapterRegistry |
getSharedInstance()
Return a shared default
ReactiveAdapterRegistry instance,
lazily building it once needed. |
boolean |
hasAdapters()
Whether the registry has any adapters.
|
void |
registerReactiveType(ReactiveTypeDescriptor descriptor,
Function<Object,Publisher<?>> toAdapter,
Function<Publisher<?>,Object> fromAdapter)
Register a reactive type along with functions to adapt to and from a
Reactive Streams
Publisher . |
void |
registerReactiveTypeOverride(ReactiveTypeDescriptor descriptor,
Function<Object,Publisher<?>> toAdapter,
Function<Publisher<?>,Object> fromAdapter)
Register a reactive type along with functions to adapt to and from a
Reactive Streams
Publisher . |
public ReactiveAdapterRegistry()
getSharedInstance()
public boolean hasAdapters()
public void registerReactiveType(ReactiveTypeDescriptor descriptor, Function<Object,Publisher<?>> toAdapter, Function<Publisher<?>,Object> fromAdapter)
Publisher
. The function arguments assume that
their input is neither null
nor Optional
.
This variant registers the new adapter after existing adapters. It will be matched for the exact reactive type if no earlier adapter was registered for the specific type, and it will be matched for assignability in a second pass if no earlier adapter had an assignable type before.
public void registerReactiveTypeOverride(ReactiveTypeDescriptor descriptor, Function<Object,Publisher<?>> toAdapter, Function<Publisher<?>,Object> fromAdapter)
Publisher
. The function arguments assume that
their input is neither null
nor Optional
.
This variant registers the new adapter first, effectively overriding any previously registered adapters for the same reactive type. This allows for overriding existing adapters, in particular default adapters.
Note that existing adapters for specific types will still match before an assignability match with the new adapter. In order to override all existing matches, a new reactive type adapter needs to be registered for every specific type, not relying on subtype assignability matches.
@Nullable public ReactiveAdapter getAdapter(Class<?> reactiveType)
null
if none available@Nullable public ReactiveAdapter getAdapter(@Nullable Class<?> reactiveType, @Nullable Object source)
reactiveType
- the reactive type
(may be null
if a concrete source object is given)source
- an instance of the reactive type
(i.e. to adapt from; may be null
if the reactive type is specified)null
if none availablepublic static ReactiveAdapterRegistry getSharedInstance()
ReactiveAdapterRegistry
instance,
lazily building it once needed.
NOTE: We highly recommend passing a long-lived, pre-configured
ReactiveAdapterRegistry
instance for customization purposes.
This accessor is only meant as a fallback for code paths that want to
fall back on a default instance if one isn't provided.
ReactiveAdapterRegistry
instance