1 |
package dk.thoerup.schedulesamples; |
package dk.thoerup.schedulesamples; |
2 |
|
|
3 |
|
import java.io.Serializable; |
4 |
|
|
5 |
import java.util.Calendar; |
import java.util.Calendar; |
6 |
import java.util.Date; |
import java.util.Date; |
7 |
|
|
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 |
|
|
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; |
|
int count; |
|
44 |
|
|
45 |
public void startTimer() { |
public void startTimer() { |
46 |
|
|
47 |
TimerService timerService = sessionCtx.getTimerService(); |
TimerService timerService = sessionCtx.getTimerService(); |
48 |
|
|
49 |
|
TimerInfo info = new TimerInfo(); //the info object is not necessary - but can be used to store data with the timer |
50 |
|
|
51 |
//Timer timer = timerService.createTimer( (5 * 1000), null); //singleshot timer |
//Timer timer = timerService.createTimer( (5 * 1000), info); //singleshot timer |
52 |
timerService.createTimer(10*1000, 5000, null); //repeating timer |
timerService.createTimer(10*1000, 5000, info ); //repeating timer |
53 |
|
|
54 |
// otherwise use schedule expression to create more complex schedules |
// otherwise use schedule expression to create more complex schedules |
55 |
// in this case fire once every minute when the seconds is == 10 |
// in this case fire once every minute when the seconds is == 10 |
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; |
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 |
count++; |
//@Timeout |
68 |
|
public void myEjbTimeout(Timer timer) { |
69 |
|
|
70 |
|
TimerInfo info = (TimerInfo) timer.getInfo(); |
71 |
|
|
72 |
System.out.print("timeout .... " + count); |
//any updates to the info object is not pushed back to the timer services |
73 |
|
|
74 |
|
System.out.print("timeout .... " + count + " / " + info.getStart() ); |
75 |
|
count++; |
76 |
|
|
77 |
if (count >= 10) { |
if (count >= 10) { |
78 |
timer.cancel(); |
timer.cancel(); |