I have written simple REST web service client class which uses the JAX-RS 2.0 client API to make REST requests. I am trying to figure out how to set a request timeout for each invocation. Here is the code for a request:
Client client = ClientBuilder.newBuilder().build(); WebTarget resourceTarget = client.target(restServiceUrl) .path("{regsysID}/{appointmentID}/") .resolveTemplate("regsysID", regSysId) .resolveTemplate("appointmentID", apptId); Invocation invocation = resourceTarget.request(MediaType.APPLICATION_JSON).buildPut(null); String createSessionJson = invocation.invoke(String.class);
build(); We create a ClientBuilder instance by calling the static method ClientBuilder. newBuilder(). We then set a proprietary, JAX-RS implementation–specific configuration property that controls socket connection timeouts.
request timeout — a time period required to process an HTTP call: from sending a request to receiving a response. connection timeout — a time period in which a client should establish a connection with a server. socket timeout — a maximum time of inactivity between two data packets when exchanging data with a server.
The REST client timeouts when a REST call takes longer to respond than the specified time. When this happens, the OUT event fails. The default value of the REST client response timeout is 120 seconds. You can increase this time if an adapter that you use has longer than normal response times.
The JAX-RS client API is a Java based API used to access Web resources.
Note: this is a new method available on JAX-RS 2.1
This is a very old post but the below code will work for both jersey and resteasy.
ClientBuilder clientBuilder = ClientBuilder.newBuilder(); clientBuilder.connectTimeout(10, TimeUnit.SECONDS); clientBuilder.readTimeout(12, TimeUnit.SECONDS); Client client = clientBuilder.build();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With