Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting request timeout for JAX-RS 2.0 Client API

Tags:

java

rest

jax-rs

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); 
like image 945
sarabdeep singh Avatar asked Mar 26 '14 20:03

sarabdeep singh


People also ask

Which JAX-RS Client API method is required to set a specific configuration property that can be used to control socket connection timeouts?

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.

What is connection request timeout?

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.

What is REST request timeout?

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.

What is JAX-RS Client API?

The JAX-RS client API is a Java based API used to access Web resources.


1 Answers

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(); 
like image 188
Pravat Avatar answered Sep 21 '22 00:09

Pravat