1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.as.quickstarts.ejb.asynchronous;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Future;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25
26 import javax.ejb.EJB;
27 import javax.ejb.Stateless;
28
29
30
31
32
33
34
35
36 @Stateless
37 public class ParallelAccessBean implements ParallelAccess {
38 private static final Logger LOGGER = Logger.getLogger(ParallelAccessBean.class.getName());
39
40 @EJB
41 private AsynchronousAccess asyncBean;
42 @EJB
43 private AnotherAsynchronousAccess anotherBean;
44
45 @Override
46 public Collection<String> invokeAsyncParallel() {
47 ArrayList<String> results = new ArrayList<String>();
48 Future<String> call1 = asyncBean.longerRunning(5000);
49 Future<String> call2 = asyncBean.longerRunning(3000);
50
51 try {
52 results.add(call1.get());
53 results.add(call2.get());
54 } catch (InterruptedException e) {
55 LOGGER.log(Level.SEVERE, "Asynchronous call was interrupted!", e);
56 throw new RuntimeException("Asynchronous call was interrupted!", e);
57 } catch (ExecutionException e) {
58 LOGGER.log(Level.SEVERE, "Asynchronous call failed!", e);
59 throw new RuntimeException("Asynchronous call failed!", e);
60 }
61 return results;
62 }
63
64 @Override
65 public void callInterfaceAnnotatedMethod() {
66
67 final Future<String> call1 = anotherBean.interfaceAsync(500);
68
69 try {
70
71 LOGGER.info("CALL result " + call1.get());
72 } catch (Exception e) {
73 throw new RuntimeException("Asynchronous (interface annotated) method can not be called!", e);
74 }
75 }
76 }