co.com.bancolombia.commons.jms.http.replier.ReplyClient Maven / Gradle / Ivy
package co.com.bancolombia.commons.jms.http.replier;
import co.com.bancolombia.commons.jms.api.model.JmsMessage;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import lombok.extern.log4j.Log4j2;
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.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
import static io.netty.channel.ChannelOption.CONNECT_TIMEOUT_MILLIS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
@Log4j2
public class ReplyClient {
private final WebClient client;
public ReplyClient(WebClient.Builder builder, int timeout) {
this.client = buildClient(builder, timeout);
}
public Mono remoteReply(String endpoint, JmsMessage message) {
return client
.post()
.uri(endpoint)
.body(BodyInserters.fromValue(message))
.retrieve()
.toBodilessEntity()
.then();
}
public static WebClient buildClient(WebClient.Builder builder, int timeout) {
return builder
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.clientConnector(getClientHttpConnector(timeout))
.build();
}
public static ClientHttpConnector getClientHttpConnector(int timeout) {
return new ReactorClientHttpConnector(HttpClient.create()
.compress(true)
.keepAlive(true)
.option(CONNECT_TIMEOUT_MILLIS, timeout)
.doOnConnected(connection -> {
connection.addHandlerLast(new ReadTimeoutHandler(timeout, MILLISECONDS));
connection.addHandlerLast(new WriteTimeoutHandler(timeout, MILLISECONDS));
}));
}
}