Created
April 14, 2024 09:16
-
-
Save vidhya03/5d21de835b5fc3a11d4df9bc2b6f6fcf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.netty.channel.ChannelOption; | |
import io.netty.handler.timeout.ReadTimeoutHandler; | |
import io.netty.handler.timeout.WriteTimeoutHandler; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.http.HttpHeaders; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.client.reactive.ClientHttpConnector; | |
import org.springframework.http.client.reactive.ReactorClientHttpConnector; | |
import org.springframework.web.reactive.config.WebFluxConfigurer; | |
import org.springframework.web.reactive.function.client.WebClient; | |
import reactor.netty.http.client.HttpClient; | |
@Configuration | |
public class WebFluxConfig implements WebFluxConfigurer { | |
private static final Logger logger = LoggerFactory.getLogger(WebFluxConfig.class); | |
@Autowired | |
private RestTimeoutConfiguration timeoutConfiguration; | |
@Bean | |
public WebClient getWebClient() { | |
HttpClient httpClient = HttpClient | |
.create() | |
.tcpConfiguration(client -> | |
client | |
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeoutConfiguration.getConnectTimeout()) | |
.doOnConnected(conn -> | |
conn | |
.addHandlerLast(new ReadTimeoutHandler(timeoutConfiguration.getReadTimeout())) | |
.addHandlerLast(new WriteTimeoutHandler(timeoutConfiguration.getWriteTimeout())) | |
) | |
); | |
ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient.wiretap(true)); | |
logger.info("WebClient initialized"); | |
return WebClient | |
.builder() | |
.clientConnector(connector) | |
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | |
.build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment