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.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15
16
17
18
19 @Stateless
20 public class AuctionMgmtActionEJB {
21 private static Logger logger = LoggerFactory.getLogger(AuctionMgmtActionEJB.class);
22
23
24
25
26 public Date doWorkSync(long delay) {
27 DateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");
28 logger.debug("sync method {} starting {} delay at {}", Thread.currentThread().getId(), delay, df.format(new Date()));
29 try { Thread.sleep(delay); }
30 catch (Exception ex) {
31 logger.error("unexpected error during sleep", ex);
32 }
33 Date now = new Date();
34 logger.debug("sync method {} completed {} delay at {}", 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 logger.debug("async method {} starting {} delay at {}", Thread.currentThread().getId(), delay, df.format(new Date()));
46 try { Thread.sleep(delay); }
47 catch (Exception ex) {
48 logger.error("unexpected error during sleep", ex);
49 }
50 Date now = new Date();
51 logger.debug("async method {} completed {} delay at {}", Thread.currentThread().getId(), delay, df.format(now));
52
53 return new AsyncResult<Date>(now);
54 }
55 }