Class Netty4ClientHttpRequestFactory
- java.lang.Object
- org.springframework.http.client.Netty4ClientHttpRequestFactory
- All Implemented Interfaces:
DisposableBean,InitializingBean,AsyncClientHttpRequestFactory,ClientHttpRequestFactory
public class Netty4ClientHttpRequestFactory extends Object implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory, InitializingBean, DisposableBean
ClientHttpRequestFactoryimplementation that uses Netty 4 to create requests.Allows to use a pre-configured
EventLoopGroupinstance: useful for sharing across multiple clients.Note that this implementation consistently closes the HTTP connection on each request.
- Since:
- 4.1.2
- Author:
- Arjen Poutsma, Rossen Stoyanchev, Brian Clozel, Mark Paluch
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_MAX_RESPONSE_SIZEThe default maximum response size.
Constructor Summary
Constructors Constructor Description Netty4ClientHttpRequestFactory()Create a newNetty4ClientHttpRequestFactorywith a defaultNioEventLoopGroup.Netty4ClientHttpRequestFactory(io.netty.channel.EventLoopGroup eventLoopGroup)Create a newNetty4ClientHttpRequestFactorywith the givenEventLoopGroup.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()Invoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.protected voidconfigureChannel(io.netty.channel.socket.SocketChannelConfig config)Template method for changing properties on the givenSocketChannelConfig.AsyncClientHttpRequestcreateAsyncRequest(URI uri, HttpMethod httpMethod)Create a new asynchronousAsyncClientHttpRequestfor the specified URI and HTTP method.ClientHttpRequestcreateRequest(URI uri, HttpMethod httpMethod)Create a newClientHttpRequestfor the specified URI and HTTP method.voiddestroy()Invoked by the containingBeanFactoryon destruction of a bean.voidsetConnectTimeout(int connectTimeout)Set the underlying connect timeout (in milliseconds).voidsetMaxResponseSize(int maxResponseSize)Set the default maximum response size.voidsetReadTimeout(int readTimeout)Set the underlying URLConnection's read timeout (in milliseconds).voidsetSslContext(io.netty.handler.ssl.SslContext sslContext)Set the SSL context.
Field Detail
DEFAULT_MAX_RESPONSE_SIZE
public static final int DEFAULT_MAX_RESPONSE_SIZE
The default maximum response size.- See Also:
setMaxResponseSize(int), Constant Field Values
Constructor Detail
Netty4ClientHttpRequestFactory
public Netty4ClientHttpRequestFactory()
Create a newNetty4ClientHttpRequestFactorywith a defaultNioEventLoopGroup.
Netty4ClientHttpRequestFactory
public Netty4ClientHttpRequestFactory(io.netty.channel.EventLoopGroup eventLoopGroup)
Create a newNetty4ClientHttpRequestFactorywith the givenEventLoopGroup.NOTE: the given group will not be shutdown by this factory; doing so becomes the responsibility of the caller.
Method Detail
setMaxResponseSize
public void setMaxResponseSize(int maxResponseSize)
Set the default maximum response size.By default this is set to
DEFAULT_MAX_RESPONSE_SIZE.- Since:
- 4.1.5
- See Also:
HttpObjectAggregator(int)
setSslContext
public void setSslContext(io.netty.handler.ssl.SslContext sslContext)
Set the SSL context. When configured it is used to create and insert anSslHandlerin the channel pipeline.A default client SslContext is configured if none has been provided.
setConnectTimeout
public void setConnectTimeout(int connectTimeout)
Set the underlying connect timeout (in milliseconds). A timeout value of 0 specifies an infinite timeout.- See Also:
ChannelConfig.setConnectTimeoutMillis(int)
setReadTimeout
public void setReadTimeout(int readTimeout)
Set the underlying URLConnection's read timeout (in milliseconds). A timeout value of 0 specifies an infinite timeout.- See Also:
ReadTimeoutHandler
afterPropertiesSet
public void afterPropertiesSet()
Description copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSetin interfaceInitializingBean
createRequest
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException
Description copied from interface:ClientHttpRequestFactoryCreate a newClientHttpRequestfor the specified URI and HTTP method.The returned request can be written to, and then executed by calling
ClientHttpRequest.execute().- Specified by:
createRequestin interfaceClientHttpRequestFactory- Parameters:
uri- the URI to create a request forhttpMethod- the HTTP method to execute- Returns:
- the created request
- Throws:
IOException- in case of I/O errors
createAsyncRequest
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException
Description copied from interface:AsyncClientHttpRequestFactoryCreate a new asynchronousAsyncClientHttpRequestfor the specified URI and HTTP method.The returned request can be written to, and then executed by calling
AsyncClientHttpRequest.executeAsync().- Specified by:
createAsyncRequestin interfaceAsyncClientHttpRequestFactory- Parameters:
uri- the URI to create a request forhttpMethod- the HTTP method to execute- Returns:
- the created request
- Throws:
IOException- in case of I/O errors
configureChannel
protected void configureChannel(io.netty.channel.socket.SocketChannelConfig config)
Template method for changing properties on the givenSocketChannelConfig.The default implementation sets the connect timeout based on the set property.
- Parameters:
config- the channel configuration
destroy
public void destroy() throws InterruptedException
Description copied from interface:DisposableBeanInvoked by the containingBeanFactoryon destruction of a bean.- Specified by:
destroyin interfaceDisposableBean- Throws:
InterruptedException