Skip to content

Commit 4dc1bce

Browse files
committed
Add description to JAX-RS:Async-Client sample
Add AsciiDoc based JavaDoc to the Test Class to describe the sample; * How it works * How to use it * etc
1 parent dba0a2e commit 4dc1bce

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

jaxrs/async-client/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
<groupId>org.javaee7.jaxrs</groupId>
1212
<artifactId>async-client</artifactId>
13+
<name>Async Client</name>
14+
<description>Invoke a JAX-RS service via an asynchronous client</description>
1315
<version>1.0-SNAPSHOT</version>
1416
<packaging>war</packaging>
1517
</project>

jaxrs/async-client/src/test/java/org/javaee7/jaxrs/asyncclient/MyResourceTest.java

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,42 @@
3030
import org.junit.runner.RunWith;
3131

3232
/**
33-
* @author Arun Gupta
33+
* In this sample we're going to explore how to communicate with a +JAX-RS+
34+
* service via an asynchronous invocation from the client.
35+
*
36+
* First step; we need a service to invoke.
37+
*
38+
* Let's create a simple +GET+ method.
39+
*
40+
* include::MyResource#getList[]
41+
*
42+
* For +JAX-RS+ to expose our service we need to provide an implementation of
43+
* the +JAX-RS+ +Application+ class to define our root path.
44+
*
45+
* include::MyApplication[]
3446
*/
3547
@RunWith(Arquillian.class)
3648
public class MyResourceTest {
3749

50+
/**
51+
* Since +JAX-RS+ webservices are, well, web related and require a
52+
* web contex, they are required to be deployed within a +web archive+.
53+
* By default, +JAX-RS+ will perform autodiscovery of our services.
54+
* That means there is no need to add a +web.xml+ in this scenario.
55+
*
56+
* Based on the definition of our +@Deployment+ method, we will be
57+
* creating and deploying the following archive structure.
58+
* [source,file]
59+
* ----
60+
* /WEB-INF/
61+
* /WEB-INF/classes/
62+
* /WEB-INF/classes/org/
63+
* /WEB-INF/classes/org/javaee7/jaxrs/
64+
* /WEB-INF/classes/org/javaee7/jaxrs/asyncclient/
65+
* /WEB-INF/classes/org/javaee7/jaxrs/asyncclient/MyResource.class
66+
* /WEB-INF/classes/org/javaee7/jaxrs/asyncclient/MyApplication.class
67+
* ----
68+
*/
3869
@Deployment(testable = false)
3970
public static WebArchive createDeployment() {
4071
return ShrinkWrap.create(WebArchive.class)
@@ -53,39 +84,48 @@ public void setUpClass() throws MalformedURLException {
5384
}
5485

5586
/**
56-
* Test of getList method, of class MyResource.
87+
* Before we can invoke our service we need to setup the client.
88+
*
89+
* include::MyResourceTest#setUpClass[]
90+
*
91+
* Now we're free to invoke our deployed service by using the +JAX-RS+
92+
* client library.
93+
*
94+
* The asynchronous client library comes with multiple option on how
95+
* to invoke the methods. First let's look at using the +Future+ option
96+
* with access to the complete +Response+.
5797
*/
5898
@Test
5999
public void testPollingResponse() throws InterruptedException, ExecutionException {
60-
Future<Response> r1 = target.request().async().get();
61-
String response = r1.get().readEntity(String.class);
62-
assertEquals("apple", response);
100+
Future<Response> r1 = target.request().async().get(); // <1> Build an asynchronous request handler for the +Response+ object
101+
String response = r1.get().readEntity(String.class); // <2> Read the entity from the body of the +Response+
102+
assertEquals("apple", response); // <3> Validate we go the expected value
63103
}
64104

65105
/**
66-
* Test of getList method, of class MyResource.
106+
* Another possibility is to use the +Future+ option with access to only the +Response+ body.
67107
*/
68108
@Test
69109
public void testPollingString() throws InterruptedException, ExecutionException {
70-
Future<String> r1 = target.request().async().get(String.class);
71-
String response = r1.get();
72-
assertEquals("apple", response);
110+
Future<String> r1 = target.request().async().get(String.class); // <1> Build an asynchronous request handler for the body of the +Response+
111+
String response = r1.get(); // <2> Read the entity directly from the +Future+
112+
assertEquals("apple", response); // <3> Validate we go the expected value
73113
}
74114

75115
/**
76-
* Test of getList method, of class MyResource.
116+
* You can also register a +InvocationCallback+ and get a callback when the +Request+ is done.
77117
*/
78118
@Test
79119
public void testInvocationCallback() throws InterruptedException, ExecutionException {
80-
target.request().async().get(new InvocationCallback<String>() {
120+
target.request().async().get(new InvocationCallback<String>() { // <1> Build an asynchronous request callback for the body of the +Response+
81121

82122
@Override
83-
public void completed(String r) {
123+
public void completed(String r) { // <2> Called when the +Request+ is completed and our entiy parsed
84124
assertEquals("apple", r);
85125
}
86126

87127
@Override
88-
public void failed(Throwable t) {
128+
public void failed(Throwable t) { // <3> Called if the +Request+ failed to complete
89129
fail(t.getMessage());
90130
}
91131

0 commit comments

Comments
 (0)