KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfox > test > ejb3 > timer > ExampleTimerBean


1 /*
2  * JFox - The most lightweight Java EE Application Server!
3  * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
4  *
5  * JFox is licenced and re-distributable under GNU LGPL.
6  */

7 package jfox.test.ejb3.timer;
8
9 import java.util.Date JavaDoc;
10 import javax.ejb.Remote JavaDoc;
11 import javax.ejb.SessionContext JavaDoc;
12 import javax.ejb.Stateless JavaDoc;
13 import javax.ejb.Timeout JavaDoc;
14 import javax.ejb.Timer JavaDoc;
15 import javax.ejb.TimedObject JavaDoc;
16 import javax.ejb.TimerService JavaDoc;
17 import javax.annotation.Resource;
18
19 @Stateless JavaDoc(name = "timer.ExampleTimerBean")
20 @Remote JavaDoc
21 public class ExampleTimerBean implements ExampleTimer, TimedObject JavaDoc {
22
23     @Resource
24     private SessionContext JavaDoc ctx;
25
26     @Resource
27     TimerService JavaDoc timerService;
28
29     /**
30      * 使用两ç§?æ–¹å¼?分别æ??交一个定时任务
31      */

32     public void scheduleTimer(long milliseconds) {
33         ctx.getTimerService().createTimer(new Date JavaDoc(new Date JavaDoc().getTime() + milliseconds), "Hello World");
34         timerService.createTimer(new Date JavaDoc(new Date JavaDoc().getTime() + milliseconds), "Hello World2");
35     }
36
37     /**
38      * 使用 @Timeout 标注
39      */

40     @Timeout JavaDoc
41     public void timeoutHandler(Timer JavaDoc timer) {
42         System.out.println("---------------------");
43         System.out.println("* Received Timer event: " + timer.getInfo());
44         System.out.println("---------------------");
45         timer.cancel();
46     }
47
48     /**
49      * 实现 TimedObject 定义的方法
50      */

51     public void ejbTimeout(Timer JavaDoc timer) {
52         System.out.println("---------------------");
53         System.out.println("* Received interface Timer event : " + timer.getInfo());
54         System.out.println("---------------------");
55         timer.cancel();
56     }
57 }
58
Popular Tags