Top Description Fields Constructors Methods
org.springframework.http.client.reactive

public Class ReactorClientHttpConnector

extends Object
implements ClientHttpConnector, SmartLifecycle
Class Inheritance
All Implemented Interfaces
org.springframework.context.SmartLifecycle, org.springframework.context.Phased, org.springframework.context.Lifecycle, org.springframework.http.client.reactive.ClientHttpConnector
Imports
java.net.URI, java.util.concurrent.atomic.AtomicReference, java.util.function.Function, org.apache.commons.logging.Log, .LogFactory, reactor.core.publisher.Mono, reactor.netty.NettyOutbound, reactor.netty.http.client.HttpClient, .HttpClientRequest, reactor.netty.resources.ConnectionProvider, .LoopResources, org.springframework.context.SmartLifecycle, org.springframework.http.HttpMethod, org.springframework.http.client.ReactorResourceFactory, org.springframework.lang.Nullable, org.springframework.util.Assert

Reactor-Netty implementation of ClientHttpConnector.

This class implements SmartLifecycle and can be optionally declared as a Spring-managed bean.

Authors
Brian Clozel, Rossen Stoyanchev, Sebastien Deleuze
Since
5.0
See Also
reactor.netty.http.client.HttpClient

Field Summary

Modifier and TypeField and Description
private static final Function<HttpClient, HttpClient>
private HttpClient
private final Object
private static final Log
private final Function<HttpClient, HttpClient>
private final ReactorResourceFactory
private volatile boolean

Constructor Summary

AccessConstructor and Description
public
ReactorClientHttpConnector()

Default constructor.

public
ReactorClientHttpConnector(ReactorResourceFactory
the resource factory to obtain the resources from
resourceFactory
,
Function<HttpClient, HttpClient>
a mapper for further initialization of the created client
mapper
)

Constructor with externally managed Reactor Netty resources, including LoopResources for event loop threads, and ConnectionProvider for the connection pool.

public
ReactorClientHttpConnector(HttpClient
the client to use
httpClient
)

Constructor with a pre-configured HttpClient instance.

Method Summary

Modifier and TypeMethod and Description
private ReactorClientHttpRequest
adaptRequest(HttpMethod method, URI uri, HttpClientRequest request, NettyOutbound nettyOutbound)

private static Function<HttpClient, HttpClient>
public Mono<ClientHttpResponse>
connect(HttpMethod
the HTTP request method
method
,
URI
the HTTP request URI
uri
,
Function<? super ClientHttpRequest, Mono<Void>>
a function that prepares and writes to the request, returning a publisher that signals when it's done writing. Implementations can return a Mono<Void> by calling ClientHttpRequest#writeWith or ClientHttpRequest#setComplete.
requestCallback
)

Implements org.springframework.http.client.reactive.ClientHttpConnector.connect.

Connect to the origin server using the given HttpMethod and URI and apply the given requestCallback when the HTTP request of the underlying API can be initialized and written to.

private static HttpClient
public int
getPhase()

Overrides default org.springframework.context.SmartLifecycle.getPhase.

Return the phase that this lifecycle object is supposed to run in.

The default implementation returns DEFAULT_PHASE in order to let stop() callbacks execute before regular Lifecycle implementations.

public boolean
isAutoStartup()

Overrides default org.springframework.context.SmartLifecycle.isAutoStartup.

Returns true if this Lifecycle component should get started automatically by the container at the time that the containing ApplicationContext gets refreshed.

public boolean
isRunning()

Implements org.springframework.context.Lifecycle.isRunning.

Check whether this component is currently running.

private static HttpClient.RequestSender
setUri(HttpClient.RequestSender requestSender, URI uri)

public void
start()

Implements org.springframework.context.Lifecycle.start.

Start this component.

public void
stop()

Implements org.springframework.context.Lifecycle.stop.

Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.

public final void
stop(Runnable callback)

Overrides default org.springframework.context.SmartLifecycle.stop.

Indicates that a Lifecycle component must stop if it is currently running.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultInitializerback to summary
private static final Function<HttpClient, HttpClient> defaultInitializer
httpClientback to summary
private HttpClient httpClient
lifecycleMonitorback to summary
private final Object lifecycleMonitor
loggerback to summary
private static final Log logger
mapperback to summary
private final Function<HttpClient, HttpClient> mapper
Annotations
@Nullable
resourceFactoryback to summary
private final ReactorResourceFactory resourceFactory
Annotations
@Nullable
runningback to summary
private volatile boolean running

Constructor Detail

ReactorClientHttpConnectorback to summary
public ReactorClientHttpConnector()

Default constructor. Initializes HttpClient via:

HttpClient.create().compress()
ReactorClientHttpConnectorback to summary
public ReactorClientHttpConnector(ReactorResourceFactory resourceFactory, Function<HttpClient, HttpClient> mapper)

Constructor with externally managed Reactor Netty resources, including LoopResources for event loop threads, and ConnectionProvider 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 reactor.netty.http.HttpResources, which is recommended since fixed, shared resources are favored for event loop concurrency. However, consider declaring a ReactorResourceFactory bean with globalResources=true in order to ensure the Reactor Netty global resources are shut down when the Spring ApplicationContext is stopped or closed and restarted properly when the Spring ApplicationContext is (with JVM Checkpoint Restore for example).

Parameters
resourceFactory:ReactorResourceFactory

the resource factory to obtain the resources from

mapper:Function<HttpClient, HttpClient>

a mapper for further initialization of the created client

Since
5.1
ReactorClientHttpConnectorback to summary
public ReactorClientHttpConnector(HttpClient httpClient)

Constructor with a pre-configured HttpClient instance.

Parameters
httpClient:HttpClient

the client to use

Since
5.1

Method Detail

adaptRequestback to summary
private ReactorClientHttpRequest adaptRequest(HttpMethod method, URI uri, HttpClientRequest request, NettyOutbound nettyOutbound)
applyLoopResourcesback to summary
private static Function<HttpClient, HttpClient> applyLoopResources(ReactorResourceFactory factory)
connectback to summary
public Mono<ClientHttpResponse> connect(HttpMethod method, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback)

Implements org.springframework.http.client.reactive.ClientHttpConnector.connect.

Doc from org.springframework.http.client.reactive.ClientHttpConnector.connect.

Connect to the origin server using the given HttpMethod and URI and apply the given requestCallback when the HTTP request of the underlying API can be initialized and written to.

Parameters
method:HttpMethod

the HTTP request method

uri:URI

the HTTP request URI

requestCallback:Function<? super ClientHttpRequest, Mono<Void>>

a function that prepares and writes to the request, returning a publisher that signals when it's done writing. Implementations can return a Mono<Void> by calling ClientHttpRequest#writeWith or ClientHttpRequest#setComplete.

Returns:Mono<ClientHttpResponse>

publisher for the ClientHttpResponse

Annotations
@Override
createHttpClientback to summary
private static HttpClient createHttpClient(ReactorResourceFactory resourceFactory, Function<HttpClient, HttpClient> mapper)
getPhaseback to summary
public int getPhase()

Overrides default org.springframework.context.SmartLifecycle.getPhase.

Doc from org.springframework.context.SmartLifecycle.getPhase.

Return the phase that this lifecycle object is supposed to run in.

The default implementation returns DEFAULT_PHASE in order to let stop() callbacks execute before regular Lifecycle implementations.

Annotations
@Override
isAutoStartupback to summary
public boolean isAutoStartup()

Overrides default org.springframework.context.SmartLifecycle.isAutoStartup.

Doc from org.springframework.context.SmartLifecycle.isAutoStartup.

Returns true if this Lifecycle component should get started automatically by the container at the time that the containing ApplicationContext gets refreshed.

A value of false indicates that the component is intended to be started through an explicit start() call instead, analogous to a plain Lifecycle implementation.

The default implementation returns true.

Annotations
@Override
isRunningback to summary
public boolean isRunning()

Implements org.springframework.context.Lifecycle.isRunning.

Doc from org.springframework.context.Lifecycle.isRunning.

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.

Returns:boolean

whether the component is currently running

Annotations
@Override
setUriback to summary
private static HttpClient.RequestSender setUri(HttpClient.RequestSender requestSender, URI uri)
startback to summary
public void start()

Implements org.springframework.context.Lifecycle.start.

Doc from org.springframework.context.Lifecycle.start.

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.

Annotations
@Override
stopback to summary
public void stop()

Implements org.springframework.context.Lifecycle.stop.

Doc from org.springframework.context.Lifecycle.stop.

Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementing SmartLifecycle and its stop(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.

Annotations
@Override
stopback to summary
public final void stop(Runnable callback)

Overrides default org.springframework.context.SmartLifecycle.stop.

Doc from org.springframework.context.SmartLifecycle.stop.

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 the SmartLifecycle component does indeed stop.

The LifecycleProcessor will call only this variant of the stop method; i.e. Lifecycle#stop() will not be called for SmartLifecycle implementations unless explicitly delegated to within the implementation of this method.

The default implementation delegates to 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).

Annotations
@Override