org.zodiac.autoconfigure.web.remote.WebRemoteWebClientAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.web.remote;
import java.util.concurrent.TimeUnit;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.http.client.reactive.ReactorResourceFactory;
import org.zodiac.autoconfigure.web.condition.ConditionalOnWebRemoteEnabled;
import org.zodiac.commons.util.lang.Numbers;
@SpringBootConfiguration
@ConditionalOnWebRemoteEnabled
@ConditionalOnWebApplication(type = Type.REACTIVE)
@ConditionalOnClass(value = {org.springframework.http.client.reactive.ClientHttpConnector.class, reactor.netty.http.client.HttpClient.class, io.netty.handler.timeout.IdleState.class})
public class WebRemoteWebClientAutoConfiguration {
private final WebRemoteProperties webRemoteProperties;
public WebRemoteWebClientAutoConfiguration(WebRemoteProperties webRemoteProperties) {
this.webRemoteProperties = webRemoteProperties;
}
@Bean
@ConditionalOnMissingBean
protected org.springframework.http.client.reactive.ClientHttpConnector clientHttpConnector(ReactorResourceFactory reactorFactory) {
// reactor.netty.tcp.TcpClient client = reactor.netty.tcp.TcpClient.create(reactorFactory.getConnectionProvider())
// .runOn(reactorFactory.getLoopResources())
// .option(io.netty.channel.ChannelOption.CONNECT_TIMEOUT_MILLIS,
// Numbers.safeLongToInt(TimeUnit.SECONDS.toMillis(webRemoteProperties.getConnectTimeout())))
// .doOnConnected(conn -> conn.addHandlerLast(new io.netty.handler.timeout.ReadTimeoutHandler(webRemoteProperties.getReadTimeout())));
reactor.netty.tcp.TcpClient client = reactor.netty.tcp.TcpClient.create(reactorFactory.getConnectionProvider())
.runOn(reactorFactory.getLoopResources())
.option(io.netty.channel.ChannelOption.CONNECT_TIMEOUT_MILLIS,
Numbers.safeLongToInt(webRemoteProperties.getConnectTimeoutMills()))
.doOnConnected(conn -> conn.addHandlerLast(new io.netty.handler.timeout.ReadTimeoutHandler(webRemoteProperties.getReadTimeoutMills(), TimeUnit.MILLISECONDS)));
return new ReactorClientHttpConnector(reactor.netty.http.client.HttpClient.from(client));
}
}