javax.ws.rs.client.Client set timeout

Asked

Viewed 239 times

0

I have a web application where at a certain time I make requests on a 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 perform the POST, however, I need to consider setting the HTTP timeout for this operation. Researching I realized that the staff uses the Jersey to carry out the following configuration:

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

I don’t use this dependency in the project and, just for testing, I tried using and running the application in Wildfly a number of exceptions and conflicts arise due to conflicts with other necessary dependencies.

An important observation is that the object client is injected via ICD.

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

1 answer

0


I found that it is not really possible to define values for timeout when this type of object is injected.

As an alternative solution I changed the http client to 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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.