javax.ws.rs.client.Client set timeout

0

I have a web application where at a certain point I make requests in endpoint using JAX-RS RestEasy as follows:

// Outros códigos acima
@Inject
private Client client;
...

WebTarget webTarget = client.target(URL).path(ENDPOINT);
Response response = webTarget.request().post// continua

Until then I can normally do POST, however, I need to consider setting the HTTP timeout for this operation. Searching I noticed that the staff uses the Jersey to perform the following configuration:

client.property(ClientProperties.CONNECT_TIMEOUT, 1000);
client.property(ClientProperties.READ_TIMEOUT,    1000);

I do not use this dependency in the project, and only by testing did I try to use and by running the application on Wildfly a series of exceptions and conflicts arise due to conflicts with other required dependencies.

One important note is that the client object is injected via CDI .

Well, I wonder how I could set the timeout without using this dependency on Jersey?

    
asked by anonymous 07.02.2018 / 21:05

1 answer

0

I have checked that it is not possible to actually set timeout values when this type of object is injected.

As a workaround I have modified the http client for Apache org.apache.httpcomponents and produce instances as follows:

RequestConfig config = RequestConfig.custom()
        .setConnectTimeout(timeout * 1000)
        .setConnectionRequestTimeout(timeout * 1000)
        .setSocketTimeout(timeout * 1000)
        .build();

CloseableHttpClient httpClient = HttpClientBuilder.create()
        .setDefaultRequestConfig(config).build();
    
08.02.2018 / 20:06