'HttpClient configuration
I was refactoring some old code the other day and came across a WebClient creation. (The code is written in Kotlin)
fun String.createClient(responseFilter: ExchangeFilterFunction = ExchangeFilterFunction.ofResponseProcessor { Mono.just(it) }): WebClient =
WebClient
.builder()
.codecs {
it.defaultCodecs().maxInMemorySize(52428800) // 50 Mb
it.defaultCodecs().jackson2JsonEncoder(jackson2JsonEncoder())
it.defaultCodecs().jackson2JsonDecoder(jackson2JsonDecoder())
}
.baseUrl(this)
.filter(responseFilter)
.clientConnector(
ReactorClientHttpConnector(
HttpClient.create()
.tcpConfiguration {
it.bootstrap { b ->
b.setCustomRequestLogging {
b
.group(onClient(DEFAULT_NATIVE))
.channel(EpollSocketChannel::class.java)
}
updateLogSupport(b, CustomLoggingHandler(HttpClient::class.java))
}
}
.apply {
if ([email protected]("https://", true))
secure { it.sslContext(SslContextBuilder.forClient().trustManager(INSTANCE).build()) }
}
)
)
.build()
fun <B : AbstractBootstrap<B, C>, C : Channel> AbstractBootstrap<B, C>.setCustomRequestLogging(init: HttpResources.() -> Unit) {
if (System.getProperty("os.name").toLowerCase() == "linux" && config().group() == null)
HttpResources.get().init()
}
Since tcpConfiguration and bootstrap are both marked deprecated I figured out I can replace it with
HttpClient.create().doOnRequest { _, connection -> connection.addHandlerFirst(CustomLoggingHandler(HttpClient::class.java)) }
but I'm not quite sure what's going on in this part
b.setCustomRequestLogging {
b
.group(onClient(DEFAULT_NATIVE))
.channel(EpollSocketChannel::class.java)
}
and how can I replace it using not deprecated api?
Solution 1:[1]
By default when you are running on Linux OS, Reactor Netty will configure for you Epoll transport. There is no need to configure by yourself neither the channel type nor the EventLoopGroup.
For the deprecated tcpConfiguration check the javadoc for HttpClient.html#tcpConfiguration where you can find a complete documentation for the replacements.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Violeta Georgieva |
