Class ReactorClientHttpConnector
- All Implemented Interfaces:
Lifecycle
,Phased
,SmartLifecycle
,ClientHttpConnector
ClientHttpConnector
.
This class implements Lifecycle
and can be optionally declared
as a Spring-managed bean.
- Since:
- 5.0
- Author:
- Brian Clozel, Rossen Stoyanchev, Sebastien Deleuze
- See Also:
-
HttpClient
-
Field Summary
Fields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE
-
Constructor Summary
ConstructorDescriptionDefault constructor.ReactorClientHttpConnector
(ReactorResourceFactory resourceFactory, Function<reactor.netty.http.client.HttpClient, reactor.netty.http.client.HttpClient> mapper) Constructor with externally managed Reactor Netty resources, includingLoopResources
for event loop threads, andConnectionProvider
for the connection pool.ReactorClientHttpConnector
(reactor.netty.http.client.HttpClient httpClient) Constructor with a pre-configuredHttpClient
instance. -
Method Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<ClientHttpResponse>
connect
(HttpMethod method, URI uri, Function<? super ClientHttpRequest, reactor.core.publisher.Mono<Void>> requestCallback) Connect to the origin server using the givenHttpMethod
andURI
and apply the givenrequestCallback
when the HTTP request of the underlying API can be initialized and written to.int
getPhase()
Return the phase that this lifecycle object is supposed to run in.boolean
Returnstrue
if thisLifecycle
component should get started automatically by the container at the time that the containingApplicationContext
gets refreshed.boolean
Check whether this component is currently running.void
start()
Start this component.void
stop()
Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.final void
Indicates that a Lifecycle component must stop if it is currently running.
-
Constructor Details
-
ReactorClientHttpConnector
public ReactorClientHttpConnector()Default constructor. InitializesHttpClient
via:HttpClient.create().compress()
-
ReactorClientHttpConnector
public ReactorClientHttpConnector(ReactorResourceFactory resourceFactory, Function<reactor.netty.http.client.HttpClient, reactor.netty.http.client.HttpClient> mapper) Constructor with externally managed Reactor Netty resources, includingLoopResources
for event loop threads, andConnectionProvider
for the connection pool.This constructor should be used only when you don't want the client to participate in the Reactor Netty global resources. By default the client participates in the Reactor Netty global resources held in
HttpResources
, which is recommended since fixed, shared resources are favored for event loop concurrency. However, consider declaring aReactorResourceFactory
bean withglobalResources=true
in order to ensure the Reactor Netty global resources are shut down when the Spring ApplicationContext is closed.- Parameters:
resourceFactory
- the resource factory to obtain the resources frommapper
- a mapper for further initialization of the created client- Since:
- 5.1
-
ReactorClientHttpConnector
public ReactorClientHttpConnector(reactor.netty.http.client.HttpClient httpClient) Constructor with a pre-configuredHttpClient
instance.- Parameters:
httpClient
- the client to use- Since:
- 5.1
-
-
Method Details
-
connect
public reactor.core.publisher.Mono<ClientHttpResponse> connect(HttpMethod method, URI uri, Function<? super ClientHttpRequest, reactor.core.publisher.Mono<Void>> requestCallback) Description copied from interface:ClientHttpConnector
Connect to the origin server using the givenHttpMethod
andURI
and apply the givenrequestCallback
when the HTTP request of the underlying API can be initialized and written to.- Specified by:
connect
in interfaceClientHttpConnector
- Parameters:
method
- the HTTP request methoduri
- the HTTP request URIrequestCallback
- a function that prepares and writes to the request, returning a publisher that signals when it's done writing. Implementations can return aMono<Void>
by callingReactiveHttpOutputMessage.writeWith(org.reactivestreams.Publisher<? extends org.springframework.core.io.buffer.DataBuffer>)
orReactiveHttpOutputMessage.setComplete()
.- Returns:
- publisher for the
ClientHttpResponse
-
start
public void start()Description copied from interface:Lifecycle
Start this component.Should not throw an exception if the component is already running.
In the case of a container, this will propagate the start signal to all components that apply.
-
stop
public void stop()Description copied from interface:Lifecycle
Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementingSmartLifecycle
and itsstop(Runnable)
variant when asynchronous stop behavior is necessary.Note that this stop notification is not guaranteed to come before destruction: On regular shutdown,
Lifecycle
beans will first receive a stop notification before the general destruction callbacks are being propagated; however, on hot refresh during a context's lifetime or on aborted refresh attempts, a given bean's destroy method will be called without any consideration of stop signals upfront.Should not throw an exception if the component is not running (not started yet).
In the case of a container, this will propagate the stop signal to all components that apply.
-
stop
Description copied from interface:SmartLifecycle
Indicates that a Lifecycle component must stop if it is currently running.The provided callback is used by the
LifecycleProcessor
to support an ordered, and potentially concurrent, shutdown of all components having a common shutdown order value. The callback must be executed after theSmartLifecycle
component does indeed stop.The
LifecycleProcessor
will call only this variant of thestop
method; i.e.Lifecycle.stop()
will not be called forSmartLifecycle
implementations unless explicitly delegated to within the implementation of this method.The default implementation delegates to
Lifecycle.stop()
and immediately triggers the given callback in the calling thread. Note that there is no synchronization between the two, so custom implementations may at least want to put the same steps within their common lifecycle monitor (if any).- Specified by:
stop
in interfaceSmartLifecycle
- See Also:
-
isRunning
public boolean isRunning()Description copied from interface:Lifecycle
Check whether this component is currently running.In the case of a container, this will return
true
only if all components that apply are currently running. -
isAutoStartup
public boolean isAutoStartup()Description copied from interface:SmartLifecycle
Returnstrue
if thisLifecycle
component should get started automatically by the container at the time that the containingApplicationContext
gets refreshed.A value of
false
indicates that the component is intended to be started through an explicitLifecycle.start()
call instead, analogous to a plainLifecycle
implementation.The default implementation returns
true
.- Specified by:
isAutoStartup
in interfaceSmartLifecycle
- See Also:
-
getPhase
public int getPhase()Description copied from interface:SmartLifecycle
Return the phase that this lifecycle object is supposed to run in.The default implementation returns
SmartLifecycle.DEFAULT_PHASE
in order to letstop()
callbacks execute after regularLifecycle
implementations.- Specified by:
getPhase
in interfacePhased
- Specified by:
getPhase
in interfaceSmartLifecycle
- See Also:
-