Class NettyAsyncHttpClientBuilder
- java.lang.Object
-
- com.azure.core.http.netty.NettyAsyncHttpClientBuilder
-
public class NettyAsyncHttpClientBuilder extends Object
Builder class responsible for creating instances ofHttpClientbacked by Reactor Netty.Building a new HttpClient instance
HttpClient client = new NettyAsyncHttpClientBuilder() .port(8080) .wiretap(true) .build();- See Also:
HttpClient
-
-
Constructor Summary
Constructors Constructor Description NettyAsyncHttpClientBuilder()Creates a new builder instance, where a builder is capable of generating multiple instances ofHttpClientbacked by Reactor Netty.NettyAsyncHttpClientBuilder(HttpClient nettyHttpClient)Creates a new builder instance, where a builder is capable of generating multiple instances ofHttpClientbased on the provided Reactor Netty HttpClient.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description HttpClientbuild()Creates a new Netty-backedHttpClientinstance on every call, using the configuration set in the builder at the time of the build method call.NettyAsyncHttpClientBuilderconfiguration(Configuration configuration)Sets the configuration store that is used during construction of the HTTP client.NettyAsyncHttpClientBuilderconnectionProvider(ConnectionProvider connectionProvider)Sets the connection provider.NettyAsyncHttpClientBuilderconnectTimeout(Duration connectTimeout)Sets the connection timeout for a request to be sent.NettyAsyncHttpClientBuilderdisableBufferCopy(boolean disableBufferCopy)Disables deep copy of responseByteBufferinto a heap location that is managed by this client as opposed to the underlying netty library which may use direct buffer pool.NettyAsyncHttpClientBuildereventLoopGroup(EventLoopGroup eventLoopGroup)Sets the IO event loop group that will be used to run IO loops.NettyAsyncHttpClientBuildernioEventLoopGroup(NioEventLoopGroup nioEventLoopGroup)Deprecated.deprecated in favor ofeventLoopGroup(EventLoopGroup).NettyAsyncHttpClientBuilderport(int port)Sets the port which this client should connect, which by default will be set to port 80.NettyAsyncHttpClientBuilderproxy(ProxyOptions proxyOptions)Sets theproxy optionsthat the client will use.NettyAsyncHttpClientBuilderreadTimeout(Duration readTimeout)Sets the read timeout duration used when reading the server response.NettyAsyncHttpClientBuilderresponseTimeout(Duration responseTimeout)Sets the response timeout duration used when waiting for a server to reply.NettyAsyncHttpClientBuilderwiretap(boolean enableWiretap)Enables the Netty wiretap feature.NettyAsyncHttpClientBuilderwriteTimeout(Duration writeTimeout)Sets the writing timeout for a request to be sent.
-
-
-
Constructor Detail
-
NettyAsyncHttpClientBuilder
public NettyAsyncHttpClientBuilder()
Creates a new builder instance, where a builder is capable of generating multiple instances ofHttpClientbacked by Reactor Netty.
-
NettyAsyncHttpClientBuilder
public NettyAsyncHttpClientBuilder(HttpClient nettyHttpClient)
Creates a new builder instance, where a builder is capable of generating multiple instances ofHttpClientbased on the provided Reactor Netty HttpClient.// Creates a reactor-netty client with netty logging enabled. reactor.netty.http.client.HttpClient baseHttpClient = reactor.netty.http.client.HttpClient.create() .wiretap(TcpClient.class.getName(), LogLevel.INFO); // Create an HttpClient based on above reactor-netty client and configure EventLoop count. HttpClient client = new NettyAsyncHttpClientBuilder(baseHttpClient) .eventLoopGroup(new NioEventLoopGroup(5)) .build();- Parameters:
nettyHttpClient- base reactor netty HttpClient
-
-
Method Detail
-
build
public HttpClient build()
Creates a new Netty-backedHttpClientinstance on every call, using the configuration set in the builder at the time of the build method call.- Returns:
- A new Netty-backed
HttpClientinstance. - Throws:
IllegalStateException- If the builder is configured to use an unknown proxy type.
-
connectionProvider
public NettyAsyncHttpClientBuilder connectionProvider(ConnectionProvider connectionProvider)
Sets the connection provider.Code Sample
// The following creates a connection provider which will have each connection use the base name // 'myHttpConnection', has a limit of 500 concurrent connections in the connection pool, has no limit on the // number of connection requests that can be pending when all connections are in use, and removes a connection // from the pool if the connection isn't used for 60 seconds. ConnectionProvider connectionProvider = ConnectionProvider.builder("myHttpConnection") .maxConnections(500) .pendingAcquireMaxCount(-1) .maxIdleTime(Duration.ofSeconds(60)) .build(); HttpClient client = new NettyAsyncHttpClientBuilder() .connectionProvider(connectionProvider) .build();- Parameters:
connectionProvider- the connection provider- Returns:
- the updated
NettyAsyncHttpClientBuilderobject.
-
proxy
public NettyAsyncHttpClientBuilder proxy(ProxyOptions proxyOptions)
Sets theproxy optionsthat the client will use.- Parameters:
proxyOptions- The proxy configuration to use.- Returns:
- the updated NettyAsyncHttpClientBuilder object.
-
wiretap
public NettyAsyncHttpClientBuilder wiretap(boolean enableWiretap)
Enables the Netty wiretap feature.- Parameters:
enableWiretap- Flag indicating wiretap status- Returns:
- the updated NettyAsyncHttpClientBuilder object.
-
port
public NettyAsyncHttpClientBuilder port(int port)
Sets the port which this client should connect, which by default will be set to port 80.- Parameters:
port- The port to connect to.- Returns:
- the updated NettyAsyncHttpClientBuilder object.
-
nioEventLoopGroup
@Deprecated public NettyAsyncHttpClientBuilder nioEventLoopGroup(NioEventLoopGroup nioEventLoopGroup)
Deprecated.deprecated in favor ofeventLoopGroup(EventLoopGroup).Sets the NIO event loop group that will be used to run IO loops.- Parameters:
nioEventLoopGroup- TheNioEventLoopGroupthat will run IO loops.- Returns:
- the updated NettyAsyncHttpClientBuilder object.
-
eventLoopGroup
public NettyAsyncHttpClientBuilder eventLoopGroup(EventLoopGroup eventLoopGroup)
Sets the IO event loop group that will be used to run IO loops.Code Samples
int threadCount = 5; HttpClient client = new NettyAsyncHttpClientBuilder() .eventLoopGroup(new NioEventLoopGroup(threadCount)) .build();- Parameters:
eventLoopGroup- TheEventLoopGroupthat will run IO loops.- Returns:
- the updated NettyAsyncHttpClientBuilder object.
-
configuration
public NettyAsyncHttpClientBuilder configuration(Configuration configuration)
Sets the configuration store that is used during construction of the HTTP client.The default configuration store is a clone of the
global configuration store, useConfiguration.NONEto bypass using configuration settings during construction.- Parameters:
configuration- The configuration store used to- Returns:
- The updated NettyAsyncHttpClientBuilder object.
-
disableBufferCopy
public NettyAsyncHttpClientBuilder disableBufferCopy(boolean disableBufferCopy)
Disables deep copy of responseByteBufferinto a heap location that is managed by this client as opposed to the underlying netty library which may use direct buffer pool.
Caution: Disabling this is not recommended as it can lead to data corruption if the downstream consumers of the response do not handle the byte buffers before netty releases them. If copy is disabled, underlying Netty layer can potentially reclaim byte array backed by theByteBufferupon the return ofonNext(). So, users should ensure they process theByteBufferimmediately and then return.HttpClient client = new NettyAsyncHttpClientBuilder() .port(8080) .disableBufferCopy(true) .build(); client.send(httpRequest) .flatMapMany(response -> response.getBody()) .map(byteBuffer -> completeProcessingByteBuffer(byteBuffer)) .subscribe();- Parameters:
disableBufferCopy- If set totrue, the client built from this builder will not deep-copy responseByteBuffers.- Returns:
- The updated
NettyAsyncHttpClientBuilderobject.
-
connectTimeout
public NettyAsyncHttpClientBuilder connectTimeout(Duration connectTimeout)
Sets the connection timeout for a request to be sent.The connection timeout begins once the request attempts to connect to the remote host and finishes once the connection is resolved.
If
connectTimeoutis null eitherConfiguration.PROPERTY_AZURE_REQUEST_CONNECT_TIMEOUTor a 10-second timeout will be used, if it is aDurationless than or equal to zero then no timeout will be applied. When applying the timeout the greatest of one millisecond and the value ofconnectTimeoutwill be used.By default the connection timeout is 10 seconds.
- Parameters:
connectTimeout- Connect timeout duration.- Returns:
- The updated
NettyAsyncHttpClientBuilderobject.
-
writeTimeout
public NettyAsyncHttpClientBuilder writeTimeout(Duration writeTimeout)
Sets the writing timeout for a request to be sent.The writing timeout does not apply to the entire request but to the request being sent over the wire. For example a request body which emits
108KBbuffers will trigger10write operations, the last write tracker will update when each operation completes and the outbound buffer will be periodically checked to determine if it is still draining.If
writeTimeoutis null eitherConfiguration.PROPERTY_AZURE_REQUEST_WRITE_TIMEOUTor a 60-second timeout will be used, if it is aDurationless than or equal to zero then no write timeout will be applied. When applying the timeout the greatest of one millisecond and the value ofwriteTimeoutwill be used.- Parameters:
writeTimeout- Write operation timeout duration.- Returns:
- The updated
NettyAsyncHttpClientBuilderobject.
-
responseTimeout
public NettyAsyncHttpClientBuilder responseTimeout(Duration responseTimeout)
Sets the response timeout duration used when waiting for a server to reply.The response timeout begins once the request write completes and finishes once the first response read is triggered when the server response is received.
If
responseTimeoutis null eitherConfiguration.PROPERTY_AZURE_REQUEST_RESPONSE_TIMEOUTor a 60-second timeout will be used, if it is aDurationless than or equal to zero then no timeout will be applied to the response. When applying the timeout the greatest of one millisecond and the value ofresponseTimeoutwill be used.- Parameters:
responseTimeout- Response timeout duration.- Returns:
- The updated
NettyAsyncHttpClientBuilderobject.
-
readTimeout
public NettyAsyncHttpClientBuilder readTimeout(Duration readTimeout)
Sets the read timeout duration used when reading the server response.The read timeout begins once the first response read is triggered after the server response is received. This timeout triggers periodically but won't fire its operation if another read operation has completed between when the timeout is triggered and completes.
If
readTimeoutis null orConfiguration.PROPERTY_AZURE_REQUEST_READ_TIMEOUTor a 60-second timeout will be used, if it is aDurationless than or equal to zero then no timeout period will be applied to response read. When applying the timeout the greatest of one millisecond and the value ofreadTimeoutwill be used.- Parameters:
readTimeout- Read timeout duration.- Returns:
- The updated
NettyAsyncHttpClientBuilderobject.
-
-