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);
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
}
}
I'm working on a simple Java EE Application, using Glassfish.
Everything runs fine, my Entity and Session Beans are working.
I also created some JAX RS Resources to invoke the Session Beans, which also works fine.
Now I'm struggling with Bean Validation.
Let's have a look at a little snippet:
#GET
#Path( "{portaluser}" )
#NotNull
public PortaluserResponse load( #PathParam( "portaluser" ) #NotBlank #Email final String strEmail )
{ ... some implementation ... }
My Jersey Application, which of course extends ResourceConfig looks like this:
public JerseyApplication()
{
packages( PortaluserService.class.getPackage().getName() );
register( JacksonFeature.class );
register( ValidationConfig.class);
property( ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true );
}
In my pom.xml I included following dependency:
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.9</version>
</dependency>
If I invoke the REST Service with nonsense data, the validation doesn't kick in.
Why is that? I expect to get a validation error.
I found a jersey-Sample which covers the bean validation stuff. My REST-Resource works within that project.
The only difference is, that jersey-Sample doesn't run in Glassfish, but in a Jetty.
Can it be that jersey bean validation doesn't work when running in a Java EE container?
Would appreciate some hints.
I wanted to implement a file-upload function for my jersey based rest server.
when i set my pom.xml (using maven) to:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.0</version>
</dependency>
i get following error:
JAX-RS Jersey Error java.lang.NoSuchMethodError: org.glassfish.jersey.internal.util.ReflectionHelper.getContextClassLoaderPA()Ljava/security/PrivilegedAction;
without the "jersey-media-multipart"-dependency the rest server is working but i cant use the file-upload functions.
Following the important part of source code:
ResourceConfig resourceConfig = new ResourceConfig(RestWebServer.class);
//resourceConfig.register(MultiPartFeature.class);
URI endPoint = new URI(pathServer);
server = JdkHttpServerFactory.createHttpServer( endPoint, resourceConfig );
RestWebserver.java:
#Path("/fileupload")
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
#FormParam("file") InputStream uploadedInputStream,
#FormParam("file") FormDataContentDisposition fileDetail)
{
String uploadedFileLocation = "c://" + fileDetail.getFileName();
// save it
saveToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
Not a Jersey user so I am just guessing, but you probably have a jar mismatch.
Try replacing your second entry with this:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.4.1</version>
</dependency>
Basing my guess on the chapter 8, have you registered your client/server code?
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?