10 |
import javax.ejb.SessionContext; |
import javax.ejb.SessionContext; |
11 |
import javax.ejb.Stateless; |
import javax.ejb.Stateless; |
12 |
import javax.ejb.TimedObject; |
import javax.ejb.TimedObject; |
13 |
|
import javax.ejb.Timeout; |
14 |
import javax.ejb.Timer; |
import javax.ejb.Timer; |
15 |
import javax.ejb.TimerService; |
import javax.ejb.TimerService; |
16 |
|
|
24 |
* */ |
* */ |
25 |
|
|
26 |
@Stateless |
@Stateless |
27 |
public class TimedEjb implements TimedObject { |
public class TimedEjb /*implements TimedObject*/ { |
28 |
|
|
29 |
|
public static class TimerInfo implements Serializable { |
30 |
|
static final long serialVersionUID = 1L; |
31 |
|
|
32 |
|
private long start = System.currentTimeMillis(); |
33 |
|
|
34 |
|
public long getStart() { |
35 |
|
return start; |
36 |
|
} |
37 |
|
|
|
class TimerInfo implements Serializable { |
|
|
public int count; |
|
38 |
} |
} |
39 |
|
|
40 |
|
private int count; //storing shared data like this is not recommended !!! |
41 |
|
|
42 |
@Resource |
@Resource |
43 |
private SessionContext sessionCtx; |
private SessionContext sessionCtx; |
44 |
|
|
56 |
//ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*"); |
//ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*"); |
57 |
//System.out.println(se.toString()); |
//System.out.println(se.toString()); |
58 |
//timerService.createCalendarTimer( se ); |
//timerService.createCalendarTimer( se ); |
59 |
|
|
60 |
|
count = 0; |
61 |
|
|
62 |
} |
} |
63 |
|
|
64 |
@Override |
//@Override if we used TimedObject interface |
65 |
public void ejbTimeout(Timer timer) { |
//public void ejbTimeout(Timer timer) { |
66 |
|
|
67 |
|
@Timeout |
68 |
|
public void myEjbTimeout(Timer timer) { |
69 |
|
|
70 |
TimerInfo info = (TimerInfo) timer.getInfo(); |
TimerInfo info = (TimerInfo) timer.getInfo(); |
71 |
|
|
72 |
info.count++; |
//any updates to the info object is not pushed back to the timer services |
|
|
|
|
System.out.print("timeout .... " + info.count); |
|
73 |
|
|
74 |
|
System.out.print("timeout .... " + count + " / " + info.getStart() ); |
75 |
|
count++; |
76 |
|
|
77 |
if (info.count >= 10) { |
if (count >= 10) { |
78 |
timer.cancel(); |
timer.cancel(); |
79 |
} |
} |
80 |
} |
} |