1 package ejava.examples.asyncmarket.ejb;
2
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6 import java.util.concurrent.Future;
7
8 import javax.ejb.AsyncResult;
9 import javax.ejb.Asynchronous;
10 import javax.ejb.Stateless;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15
16
17
18
19 @Stateless
20 public class AuctionMgmtActionEJB {
21 private static Log log = LogFactory.getLog(AuctionMgmtActionEJB.class);
22
23
24
25
26 public Date doWorkSync(long delay) {
27 DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
28 log.debug(String.format("sync method %d starting %d delay at %s", Thread.currentThread().getId(), delay, df.format(new Date())));
29 try { Thread.sleep(delay); }
30 catch (Exception ex) {
31 log.error("unexpected error during sleep:", ex);
32 }
33 Date now = new Date();
34 log.debug(String.format("sync method %d completed %d delay at %s", Thread.currentThread().getId(), delay, df.format(now)));
35
36 return now;
37 }
38
39
40
41
42 @Asynchronous
43 public Future<Date> doWorkAsync(long delay) {
44 DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
45 log.debug(String.format("async method %d starting %d delay at %s", Thread.currentThread().getId(), delay, df.format(new Date())));
46 try { Thread.sleep(delay); }
47 catch (Exception ex) {
48 log.error("unexpected error during sleep:", ex);
49 }
50 Date now = new Date();
51 log.debug(String.format("async method %d completed %d delay at %s", Thread.currentThread().getId(), delay, df.format(now)));
52
53 return new AsyncResult<Date>(now);
54 }
55
56 }