Skip to content

Instantly share code, notes, and snippets.

@damgel
Forked from mageddo/RestEasy.java
Created October 10, 2021 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damgel/d3915e4d3b1a3fc20cd5571f327ed1c5 to your computer and use it in GitHub Desktop.
Save damgel/d3915e4d3b1a3fc20cd5571f327ed1c5 to your computer and use it in GitHub Desktop.
Set timeout per request using RestEasy
import com.mageddo.engoo.factory.CustomJacksonJsonProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.jboss.resteasy.client.jaxrs.ClientHttpEngine;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine;
import org.jboss.resteasy.client.jaxrs.internal.ClientInvocation;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.Configuration;
import java.time.Duration;
public class RestEasy {
public static final String SOCKET_TIMEOUT = "SOCKET_TIMEOUT";
public static final String CONNECT_TIMEOUT = "CONNECT_TIMEOUT";
public static final String CONNECTION_REQUEST_TIMEOUT = "CONNECTION_REQUEST_TIMEOUT";
public static Client newClient(){
return newClient(10);
}
public static Client newClient(int poolSize){
final PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
pool.setMaxTotal(poolSize);
final CloseableHttpClient httpClient = HttpClientBuilder
.create()
.setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultRequestConfig(
RequestConfig.custom()
.setConnectionRequestTimeout((int) Duration.ofSeconds(10).toMillis())
.setConnectTimeout((int) Duration.ofSeconds(10).toMillis())
.setSocketTimeout((int) Duration.ofSeconds(10).toMillis())
.setRedirectsEnabled(true)
.setRelativeRedirectsAllowed(true)
.build()
)
.setConnectionManager(pool)
.build();
return new ResteasyClientBuilder()
.httpEngine(withPerRequestTimeout(httpClient))
.register(new CustomJacksonJsonProvider())
.build();
}
static Integer parseIntegerOrNull(Configuration conf, String key) {
final Object property = conf.getProperty(key);
if(property == null){
return null;
}
return Integer.parseInt(String.valueOf(property));
}
/**
* Allow to set timeout per request
* @see #CONNECTION_REQUEST_TIMEOUT
* @see #CONNECT_TIMEOUT
* @see #SOCKET_TIMEOUT
*/
static ClientHttpEngine withPerRequestTimeout(HttpClient httpClient) {
final ApacheHttpClient43Engine engine = new ApacheHttpClient43Engine(httpClient) {
@Override
protected void loadHttpMethod(ClientInvocation request, HttpRequestBase httpMethod) throws Exception {
super.loadHttpMethod(request, httpMethod);
final RequestConfig.Builder reqConf = httpMethod.getConfig() != null ? RequestConfig.copy(httpMethod.getConfig()) : RequestConfig.custom();
final Configuration conf = request.getConfiguration();
final Integer connectionRequestTimeout = parseIntegerOrNull(conf, RestEasy.CONNECTION_REQUEST_TIMEOUT);
if (connectionRequestTimeout != null) {
reqConf.setConnectionRequestTimeout(connectionRequestTimeout);
}
final Integer connectTimeout = parseIntegerOrNull(conf, RestEasy.CONNECT_TIMEOUT);
if (connectTimeout != null) {
reqConf.setConnectTimeout(connectTimeout);
}
final Integer socketTimeout = parseIntegerOrNull(conf, RestEasy.SOCKET_TIMEOUT);
if (socketTimeout != null) {
reqConf.setSocketTimeout(socketTimeout);
}
httpMethod.setConfig(reqConf.build());
}
};
engine.setFollowRedirects(true);
return engine;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment