Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timeout to JAX-RS client with CXF

I am working on a Rest Client and I am using CXF with JAX-RS.

The problem that I have is that I cannot find any way to override the default timeout values of the client.

A simple client:

Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/MyApp");
target = target.path("jsp/Test.jsp");
Response response = target.request().get();

I have read that there are two timeout properties in CXF called ReceiveTimeout and ConnectionTimeout but I have not managed to find a way to set them in my client.

I have tried client.property("ReceiveTimeout", 5000); but it doesn't work.

I have seen examples of using an xml configuration file to configure the client but I prefer not to take that path if it is possible.

Any ideas?

like image 958
NikosDim Avatar asked Feb 04 '15 10:02

NikosDim


People also ask

Is Apache CXF a JAX-RS?

JAX-RS: Java API for RESTful Web Services is a Java programming language API that provides support in creating web services according to the Representational State Transfer (REST) architectural style. CXF supports JAX-RS 2.1 (JSR-370), 2.0 (JSR-339) and 1.1 (JSR-311). CXF 3.2.

Is CXF client thread safe?

According to the JAX-WS spec, the client proxies are NOT thread safe. To write portable code, you should treat them as non-thread safe and synchronize access or use a pool of instances or similar. CXF answer: CXF proxies are thread safe for MANY use cases.

What is Apache CXF used for?

Apache CXF™ is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.

What is JAXRSClientFactory?

JAXRSClientFactory is a utility class which wraps JAXRSClientFactoryBean. JAXRSClientFactory offers a number of utility methods but JAXRSClientFactoryBean can also be used directly if desired.


2 Answers

HTTPConduit conduit = WebClient.getConfig(webClient).getHttpConduit();
conduit.getClient().setConnectionTimeout(1000 * 3);
conduit.getClient().setReceiveTimeout(1000 * 3);
like image 70
laughing buddha Avatar answered Sep 28 '22 15:09

laughing buddha


You can find the correct properties in org.apache.cxf.jaxrs.client.spec.ClientImpl: "http.connection.timeout" and "http.receive.timeout"

So just use them as property when building the client:

ClientBuilder.newClient().property("http.receive.timeout", 1000);

With JAX-RS 2.1 (supported from CXF 3.2) you can use these standard methods in ClientBuilder:

connectTimeout(long timeout, TimeUnit unit);
readTimeout(long timeout, TimeUnit unit);

See also: https://github.com/eclipse-ee4j/jaxrs-api/issues/467

like image 25
Dennis Kieselhorst Avatar answered Sep 28 '22 15:09

Dennis Kieselhorst