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.Date;
20 import java.util.concurrent.Future;
21 import java.util.logging.Logger;
22
23 import javax.ejb.AsyncResult;
24 import javax.ejb.Asynchronous;
25 import javax.ejb.Stateless;
26
27
28
29
30
31
32
33
34
35
36
37
38 @Stateless
39 public class AsynchronousAccessBean implements AsynchronousAccess, AnotherAsynchronousAccess {
40 private static final Logger LOGGER = Logger.getLogger(AsynchronousAccessBean.class.getName());
41
42 @Asynchronous
43 @Override
44 public void fireAndForget(long sleepTime) {
45 LOGGER.info("'fireAndForget' Will wait for " + sleepTime + "ms");
46 try {
47 Thread.sleep(sleepTime);
48 } catch (InterruptedException e) {
49 }
50 LOGGER.info("action 'fireAndForget' finished");
51 }
52
53 @Asynchronous
54 @Override
55 public Future<String> longerRunning(long sleepTime) {
56 LOGGER.info("Will wait for " + sleepTime + "ms");
57 try {
58 Thread.sleep(sleepTime);
59 } catch (InterruptedException e) {
60 }
61 LOGGER.info("returning the result");
62 return new AsyncResult<String>("returning at " + new Date() + ", duration was " + sleepTime + "ms");
63 }
64
65 @Asynchronous
66 @Override
67 public Future<String> failure() throws IllegalAccessException {
68 try {
69 Thread.sleep(400);
70 } catch (InterruptedException e) {
71 }
72 throw new IllegalAccessException("Asynchrounous fail demonstration");
73 }
74
75 @Override
76 public Future<String> interfaceAsync(long sleepTime) {
77 LOGGER.info("Will wait for " + sleepTime + "ms");
78 try {
79 Thread.sleep(sleepTime);
80 } catch (InterruptedException e) {
81 }
82 LOGGER.info("returning the result");
83 return new AsyncResult<String>("returning at " + new Date() + ", duration was " + sleepTime + "ms");
84 }
85 }