1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.as.quickstarts.ejb.asynchronous.client;
18
19 import java.util.Collection;
20 import java.util.Date;
21 import java.util.Hashtable;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.Future;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.TimeoutException;
26 import java.util.logging.Logger;
27
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31
32 import org.jboss.as.quickstarts.ejb.asynchronous.AsynchronousAccess;
33 import org.jboss.as.quickstarts.ejb.asynchronous.ParallelAccess;
34
35
36
37
38
39
40 public class AsynchronousClient {
41 private static final Logger LOGGER = Logger.getLogger(AsynchronousClient.class.getName());
42
43
44
45 private final AsynchronousAccess accessBean;
46
47
48
49 private final ParallelAccess parallelBean;
50
51
52
53
54
55
56
57 private AsynchronousClient() throws NamingException {
58 final Hashtable<String, String> jndiProperties = new Hashtable<String, String>();
59 jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
60 final Context context = new InitialContext(jndiProperties);
61 String lookupName = "ejb:/jboss-ejb-asynchronous-ejb/AsynchronousAccessBean!" + AsynchronousAccess.class.getName();
62 LOGGER.info("Lookup Bean >" + lookupName);
63 accessBean = (AsynchronousAccess) context.lookup(lookupName);
64 lookupName = "ejb:/jboss-ejb-asynchronous-ejb/ParallelAccessBean!" + ParallelAccess.class.getName();
65 LOGGER.info("Lookup Bean >" + lookupName);
66 parallelBean = (ParallelAccess) context.lookup(lookupName);
67 }
68
69
70
71
72
73
74 private void fireAndForget() throws InterruptedException {
75 long sleepMillis = 15000;
76 accessBean.fireAndForget(sleepMillis);
77 LOGGER.info(String.format("The server log should contain a message at (about) %s, indicating that the call to the asynchronous bean completed.", new Date(new Date().getTime() + sleepMillis)));
78
79
80 }
81
82
83
84
85
86
87
88 private void getResultAsync() throws InterruptedException, ExecutionException, TimeoutException {
89 Future<String> myResult = accessBean.longerRunning(200);
90
91
92 Thread.sleep(400);
93
94
95 LOGGER.info("Got the async result as expected => " + myResult.get(1, TimeUnit.MILLISECONDS));
96 }
97
98
99
100
101
102
103 private void waitForAsyncResult() throws InterruptedException, ExecutionException, TimeoutException {
104 Future<String> myResult = accessBean.longerRunning(1500);
105
106
107
108 LOGGER.info("Got the async result as expected after wait => " + myResult.get());
109 }
110
111
112
113
114 private void callAnEJBwithAsyncAccess() {
115 Collection<String> results = parallelBean.invokeAsyncParallel();
116 LOGGER.info("Results of the parallel (server) processing : " + results);
117 }
118
119
120
121
122 private void waitForAnotherAsyncResult2() throws InterruptedException, ExecutionException, TimeoutException {
123 parallelBean.callInterfaceAnnotatedMethod();
124 }
125
126
127
128
129 private void callAsyncWithFailure() throws InterruptedException {
130 Future<String> x;
131 try {
132 x = accessBean.failure();
133 } catch (IllegalAccessException e) {
134 throw new RuntimeException("Unexpected failure during start asynchronous execution!", e);
135 }
136 try {
137 x.get();
138 } catch (ExecutionException e) {
139
140 if (e.getCause() instanceof IllegalAccessException) {
141
142 LOGGER.info("Catch the expected Exception of the asynchronous execution!");
143 } else if (e.getCause().getCause() instanceof IllegalAccessException) {
144
145 LOGGER.info("Catch the covered Exception of the asynchronous execution, you may be using a release <= AS7.1.2 or EAP6.0.0!");
146 } else {
147 throw new RuntimeException("Unexpected ExecutionException during asynchronous call!", e);
148 }
149 }
150 }
151
152
153
154
155
156
157 public static void main(String[] args) throws Exception {
158 AsynchronousClient client = new AsynchronousClient();
159
160 client.fireAndForget();
161 client.getResultAsync();
162 client.waitForAsyncResult();
163
164 client.callAsyncWithFailure();
165
166 client.callAnEJBwithAsyncAccess();
167 client.waitForAnotherAsyncResult2();
168 }
169
170 }