01: /*
02: * JFox - The most lightweight Java EE Application Server!
03: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
04: *
05: * JFox is licenced and re-distributable under GNU LGPL.
06: */
07: package jfox.test.ejb3.timer;
08:
09: import java.util.Date;
10: import javax.ejb.Remote;
11: import javax.ejb.SessionContext;
12: import javax.ejb.Stateless;
13: import javax.ejb.Timeout;
14: import javax.ejb.Timer;
15: import javax.ejb.TimedObject;
16: import javax.ejb.TimerService;
17: import javax.annotation.Resource;
18:
19: @Stateless(name="timer.ExampleTimerBean")
20: @Remote
21: public class ExampleTimerBean implements ExampleTimer, TimedObject {
22:
23: @Resource
24: private SessionContext ctx;
25:
26: @Resource
27: TimerService timerService;
28:
29: /**
30: * 使用两ç§?æ–¹å¼?分别æ??交一个定时任务
31: */
32: public void scheduleTimer(long milliseconds) {
33: ctx.getTimerService().createTimer(
34: new Date(new Date().getTime() + milliseconds),
35: "Hello World");
36: timerService.createTimer(new Date(new Date().getTime()
37: + milliseconds), "Hello World2");
38: }
39:
40: /**
41: * 使用 @Timeout æ ‡æ³¨
42: */
43: @Timeout
44: public void timeoutHandler(Timer timer) {
45: System.out.println("---------------------");
46: System.out
47: .println("* Received Timer event: " + timer.getInfo());
48: System.out.println("---------------------");
49: timer.cancel();
50: }
51:
52: /**
53: * 实现 TimedObject 定义的方法
54: */
55: public void ejbTimeout(Timer timer) {
56: System.out.println("---------------------");
57: System.out.println("* Received interface Timer event : "
58: + timer.getInfo());
59: System.out.println("---------------------");
60: timer.cancel();
61: }
62: }
|