@GET
public String getList() {
return response[0];
}
cd javaee7-samples/jaxrs/async-client/Now we are ready to start testing. You can run all the tests in this sample by executing:
mvn test
Or you can run individual tests by executing one of the following:mvn test -Dtest=MyResourceTest
In this sample we’re going to explore how to communicate with a JAX-RS
service via an asynchronous invocation from the client.
First step; we need a service to invoke.
Let’s create a simple GET
method.
@GET
public String getList() {
return response[0];
}
For JAX-RS
to expose our service we need to provide an implementation of
the JAX-RS
Application
class to define our root path.
@ApplicationPath("webresources")
public class MyApplication extends Application {
public MyApplication();
}
Since JAX-RS
webservices are, well, web related and require a
web context, they are required to be deployed within a web archive
.
By default, JAX-RS
will perform autodiscovery of our services.
That means there is no need to add a web.xml
in this scenario.
Based on the definition of our @Deployment
method, we will be
creating and deploying the following archive structure.
/WEB-INF/
/WEB-INF/classes/
/WEB-INF/classes/org/
/WEB-INF/classes/org/javaee7/jaxrs/
/WEB-INF/classes/org/javaee7/jaxrs/asyncclient/
/WEB-INF/classes/org/javaee7/jaxrs/asyncclient/MyResource.class
/WEB-INF/classes/org/javaee7/jaxrs/asyncclient/MyApplication.class
@Deployment(testable = false)
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(MyApplication.class, MyResource.class);
}
Before we can invoke our service we need to setup the client.
@Before
public void setUpClass() throws MalformedURLException {
Client client = ClientBuilder.newClient();
target = client.target(URI.create(new URL(base, "webresources/fruits").toExternalForm()));
}
Now we are free to invoke our deployed service by using the JAX-RS
client library.
The asynchronous client library comes with multiple option on how
to invoke the methods. First let’s look at using the Future
option
with access to the complete Response
.
@Test
public void testPollingResponse() throws InterruptedException, ExecutionException {
Future<Response> r1 = target.request().async().get(); (1)
String response = r1.get().readEntity(String.class); (2)
assertEquals("apple", response); (3)
}
Build an asynchronous request handler for the Response
object
Read the entity from the body of the Response
Validate we got the expected value
Another possibility is to use the Future
option with access to only the Response
body.
@Test
public void testPollingString() throws InterruptedException, ExecutionException {
Future<String> r1 = target.request().async().get(String.class); (1)
String response = r1.get(); (2)
assertEquals("apple", response); (3)
}
Build an asynchronous request handler for the body of the Response
Read the entity directly from the Future
Validate we got the expected value
You can also register a InvocationCallback
and get a callback when the Request
is done.
@Test
public void testInvocationCallback() throws InterruptedException, ExecutionException {
target.request().async().get(new InvocationCallback<String>() { (1)
@Override
public void completed(String r) { (2)
assertEquals("apple", r);
}
@Override
public void failed(Throwable t) { (3)
fail(t.getMessage());
}
});
}
Build an asynchronous request callback for the body of the Response
Called when the Request
is completed and our entiy parsed
Called if the Request
failed to complete
There's a lot more about JavaEE to cover. If you're ready to learn more, check out the other available samples.
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/jaxrs/async-client/
Do the changes as you see fit and send a pull request!
Good Luck!