Jax rs rest client - timeout configuration [duplicate] - rest

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);

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();

You can do this by creating a ClientConfig first and providing it as an argument when creating the new client.
import org.glassfish.jersey.client.ClientProperties;
ClientConfig configuration = new ClientConfig();
configuration.property(ClientProperties.CONNECT_TIMEOUT, 1000);
configuration.property(ClientProperties.READ_TIMEOUT, 1000);
Client client = ClientBuilder.newClient(configuration);

With Resteasy this can be accomplished by building your Client as such.
Client client = new ResteasyClientBuilder()
.establishConnectionTimeout(2, TimeUnit.SECONDS)
.socketTimeout(2, TimeUnit.SECONDS)
.build();
I have not seen a list of standard configuration properties you could set via ClientBuilder.newClient(Configuration configuration) which would be needed to make this portable.

First, you have to add relevant dependencies (here is for the WildFly 10.1):
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.14.Final</version>
<scope>provided</scope>
</dependency>
Next - create a normal Apache HttpClient and push it the RestEasy Enginge with overriding one method, which causes the problem:
// create here a normal Apache HttpClient with all parameters, that you need
HttpClient httpClient = createHttpClient(connectTimeout,
socketTimeout,
connectionRequestTimeout,
maxTotalHTTPConnections);
// Deprecated Apache classes cleanup https://issues.jboss.org/browse/RESTEASY-1357
// Client Framework not honoring connection timeouts Apache Client 4.3 https://issues.jboss.org/browse/RESTEASY-975
ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient) {
#Override
protected void loadHttpMethod(ClientInvocation request, HttpRequestBase httpMethod) throws Exception {
super.loadHttpMethod(request, httpMethod);
httpMethod.setParams(new BasicHttpParams());
}
};
return new ResteasyClientBuilder().httpEngine(engine).build();
Have a look at https://issues.jboss.org/browse/RESTEASY-975 Seems, that the problem was just resolved in the version 3.1.0.Final.

For people stuck with older JAX-RS 2.0 API and old Resteasy implementation, you may use this method:
Client client = new ResteasyClientBuilder()
.establishConnectionTimeout(3, TimeUnit.SECONDS)
.socketTimeout(5, TimeUnit.SECONDS).build();
Despite the name, socketTimeout stands for "read timeout", since by the docs, it stands for "The timeout for waiting for data".

If you are using Jersey 2.x Here it is the simple solution it's work for me
import com.jclient.JClient;
Client c = Client.create();
WebResource webResource = c.resource("requestUrl");
c.setConnectTimeout(yourMins*60*1000);

Related

Is it possible to write JUnit tests that are agnostic to your JAX-RS implementation?

I wrote a REST web service using JAX-RS that knows nothing about the specific JAX-RS implementation I chose. I happen to be using TomEE which means my JAX-RS implementation is ApacheCXF.
I'd like to write unit tests for the web service that also know nothing about the JAX-RS implementation. Is this possible? So far every example I've found involves using classes from a specific JAX-RS implementation (JAXRSClientFactory for ApacheCXF, Jersey Test Framework, etc).
I've started experimenting with tomee-embedded and am able to test my EJB's but it doesn't seem to startup the REST services.
My solution was to use Arquillian paired with an Embedded TomEE. Arquillian provides a ton of functionality but I'm only using it to start/stop the Embedded TomEE. Therefore, all I needed to do was add this to my pom.xml:
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>arquillian-tomee-embedded</artifactId>
<version>${tomee.version}</version>
<scope>test</scope>
</dependency>
Then I could write a JUnit test with a little extra Arquillian stuff and plain JAX-RS:
#RunWith(Arquillian.class)
public class MyServiceIT {
#ArquillianResource
private URL webappUrl;
#Deployment()
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(MyService.class)
.addAsWebInfResource("META-INF/persistence.xml") //Refers to src/main/resources/META-INF/persistence.xml
.addAsWebInfResource("test-resources.xml", "resources.xml") //Refers to src/test/resources/test-resources.xml
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}
#Test
public void randomTest() throws URISyntaxException {
//Get data from the web service.
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(webappUrl.toURI().resolve("myentity"));
Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
int status = response.getStatus();
List<MyEntity> myEntities = response.readEntity(new GenericType<List<MyEntity>>() {});
//Perform some tests on the data
}
}

Resteasy Bean Validation Not Working on Remote Server

I have a problem similar to the one described here.
I am using RESTEasy within a standalone Jetty application. When I start the application locally and call a service (e.g. localhost:16880/rest/user/login) bean validation works fine, i.e. I get validation errors like this:
[PARAMETER]
[UserService#login(arg0).appKey]
[app_key may not be null or empty]
[]
However, when I deploy my application to a remote host and call the same service (e.g. remotehost:16880/rest/user/login) bean validation is not invoked at all.
I am using the #ValidateRequest annotation for the service and #Valid annotation for the bean parameter.
My Resteasy version is 3.0.13.Final, though I have tried earlier versions as well. I have tried to write my custom validator, but that didn't work either.
I am puzzled why the validation works locally, but not on remote server. Any suggestions would be highly appreciated.
Since you are using Jetty as standalone server, you have to define RESTEasy validation providers where you define ServletContextHandler. Note that in standalone server there is no container to scan for #Provider classes and to activate them, so you must do it manually.
I expect that you create and start your server app something like:
//create a server listening at some port
Server server= new Server(port);
//add server handlers
HandlerList handlers= new HandlerList();
initHandlers(handlers);
server.setHandler(handlers);
//start the server
server.start();
In initHandlers you must have defined your RESTEasy support:
public void initHandlers(List<HandlerList> handlers) {
//define root context handler
ServletContextHandler servletContextHandler= new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
handlers.addHandler(servletContextHandler);
//define RESTEasy handler
ServletHolder restServlet= new ServletHolder(new HttpServlet30Dispatcher());
//since this is a standalone server, somewhere you have to define RESTful services and Singletons
restServlet.setInitParameter("javax.ws.rs.Application", "com.exampleapp.MyRestApplication");
restServlet.setInitParameter("resteasy.servlet.mapping.prefix", "rest");
servletContextHandler.addServlet(restServlet, "rest/*");
}
So what is left to do now is to add Validation provider as init parameter:
restServlet.setInitParameter("resteasy.providers", "org.jboss.resteasy.plugins.validation.ValidatorContextResolver,org.jboss.resteasy.api.validation.ResteasyViolationExceptionMapper");
On this link I tried to find the name of the validator providers: https://docs.jboss.org/resteasy/docs/3.0.4.Final/userguide/html/Validation.html
RESTEasy obtains a bean validation implemenation by looking in the available META-INF/services/javax.ws.rs.Providers files for an implementation of ContextResolver
So it does not say what, but says where. Now open the "resteasy-hibernatevalidator-provider-3...*.jar (from Eclipse -> Maven dependencies or manually unzip) and look into META-INF/services/javax.ws.rs.ext.Providers It says:
org.jboss.resteasy.plugins.validation.hibernate.ValidatorContextResolver
org.jboss.resteasy.api.validation.ResteasyViolationExceptionMapper
If you don't have this dependency, then add it to your pom file:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-hibernatevalidator-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
One more note: that at the same place where you described validation providers, you also add other providers, if you happen to need them (such as JacksonJaxbJson, etc).

Lightweight jax-rs client

Using below code sample from tutorial i can successfully make post to a jax-rs service on glassfish-4.
Client client = ClientFactory.newClient();
WebTarget root = client.target("http://localhost:8080/roast-house/api/coffeebeans");
Bean origin = new Bean("arabica", RoastType.DARK, "mexico");
final String mediaType = MediaType.APPLICATION_XML;
final Entity<Bean> entity = Entity.entity(origin, mediaType);
Response response = root.request().post(entity, Response.class);
response.close();
But it forces to bring a dependency that totals about 4.5mb (resteasy 3.0.5 was ~5mb)
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.4.1</version>
</dependency>
I have the feeling i'm using only a portion of the client API, is there any more lightweight clients out there, or how would i go about to construct the request using only standard libraries?

How to write a REST client, based on CXF, in TomEE?

I would like to use my REST client, developed with CXF, with TomEE/TomEE+ 1.0, but I have a little problem with JAXB JSON marshalling/unmarshalling (with the Jackson library).
I tried both Jersey Client 1.1.13 and CXF WebClient (the version included with Tomee+ 1.0), but, I have the same error at deploy time:
org.apache.openejb.OpenEJBException: No provider available for resource-ref 'null' of type 'javax.ws.rs.ext.Providers' for 'localhost/mywebapp.Comp'
I tried also to copy the 'jackson-jaxrs-json-provieder-2.0.4.jar' jar to the TomEE lib directory, but the error is the same.
I also tried to set the system property 'openejb.cxf.jax-rs.providers' to 'com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider,com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider', but nothing changed.
Here is a sample of the code I use to make a REST call with CXF:
final List<Object> providers = new ArrayList<Object>();
providers.add(new JacksonJaxbJsonProvider());
WebClient wc = WebClient.create(url, providers);
Anyway this code it's never executed, because the error is at deploy time.
My webapp (the version developed with Jersey Client) works on Glassfish 3.1.2.
Where is the problem?
Thank you,
bye,
Demis
Found and fixed this bug:
https://issues.apache.org/jira/browse/TOMEE-339
Try the latest TomEE snapshot.
I use jacskon (yes jars needs to be added and the provider to be set) and it works.
I found a good temporary solution to use the CXF rest client and the Jackson JSON marshalling with TomEE+ 1.0.0.
I moved these libraries from the webapp lib to the TomEE lib directory:
jackson-annotations-2.0.4.jar
jackson-jaxrs-json-provider-2.0.4.jar
jackson-module-jaxb-annotations-2.0.4.jar
jackson-core-2.0.4.jar
jackson-databind-2.0.4.jar
And this is my code to make a rest call:
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true);
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
final JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider(objectMapper, JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS);
provider.setAnnotationsToUse(JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS);
provider.setMapper(objectMapper);
final List<Object> providers = new ArrayList<Object>();
providers.add(provider);
WebClient wc = WebClient.create(_request.getUrl(), providers);
wc = wc.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
try {
res = (ElasticResponse) wc.invoke(_request.getHttpVerb(), _request.getMessage(), _request.getElasticResponseClass());
} catch (final ServerWebApplicationException _e) {
this._log.log(Level.FINE, "http response code > 400", _e);
}
I hope that with the next release of TomEE I do not need to add the Jackson's libraries to the container but only to the webapp.

Restlet - Connection Problems - 406 Not Acceptable - Plain Text

couldnt find somewhere else advice.
I am writing a Restlet JSE Client for a Jersey(!) Restful Service. I already wrote a Jersey client for that and it is working, so the jersey service is alright. Now I get problems in writing a restlet client:
My Service root adress is:
http://localhost:8080/com-project-core/rest, so I call:
ClientResource = service = new ClientResource("http://localhost:8080/com-project-core/rest");
My Basic Auth Credentiels are admin and xxx, so I call:
service.setChallengeResponse(ChallengeScheme.HTTP_BASIC, "admin", "xxx");
Now the problems:
ClientResource service = new ClientResource("http://localhost:8080/com-project-core/rest/ping");
calls up my service. After that I try
String myString = service.get(String.class);
System.out.println(myString);
I get a:
08.07.2012 17:41:48 org.restlet.engine.http.connector.HttpClientHelper start
INFO: Starting the default HTTP client
in my output. Not more! The Junit Test says:
Not Acceptable (406) - Not Acceptable
So he can find the resource but cannot produce #Produces("text/plain") ??
So when I remove #Produces("text/plain") on server side it works!!
For the resourcey my server side looks like this:
#Path("/ping")
#RolesAllowed({"admin", "user"})
public class ConnectedResourceBean implements ConnectedResourceIF {
#GET
#Produces("text/plain")
public String getPingMessage() throws NamingException {
return "Hello World";
}
}
For my pom in set this dependencies:
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet</artifactId>
<version>${restlet.version}</version>
</dependency>
<dependency>
<groupId>org.restlet.jse</groupId>
<artifactId>org.restlet.ext.xstream</artifactId>
<version>${restlet.version}</version>
</dependency>
As I said, its working with my jersey client.
No way: Restlet had problems with
#Produces("text/plain")
on jersey server side. Can someone explain me that fact?
Edit:
Made it work with
<properties>
<restlet.version>2.1-M3</restlet.version>
</properties>