1 |
torben |
1279 |
package dk.thoerup.schedulesamples; |
2 |
|
|
|
3 |
torben |
1300 |
import java.io.Serializable; |
4 |
|
|
|
5 |
torben |
1280 |
import java.util.Calendar; |
6 |
|
|
import java.util.Date; |
7 |
|
|
|
8 |
|
|
import javax.annotation.Resource; |
9 |
|
|
import javax.ejb.ScheduleExpression; |
10 |
|
|
import javax.ejb.SessionContext; |
11 |
torben |
1279 |
import javax.ejb.Stateless; |
12 |
|
|
import javax.ejb.TimedObject; |
13 |
torben |
1301 |
import javax.ejb.Timeout; |
14 |
torben |
1279 |
import javax.ejb.Timer; |
15 |
torben |
1280 |
import javax.ejb.TimerService; |
16 |
torben |
1279 |
|
17 |
torben |
1280 |
/* GFv3: before using a timed bean make sure the ejb timer service is correctly configured with a datasource and the timer table is created |
18 |
torben |
1298 |
* cd <glassfishv3>/glassfish/domains/domain1/lib/databases/ejbtimer |
19 |
torben |
1280 |
* /usr/lib/jvm/java-6-sun/db/bin/ij |
20 |
|
|
* connect 'jdbc:derby:.'; |
21 |
torben |
1298 |
* paste content from <glassfishv3>/glassfish/lib/install/databases/ejbtimer_derby.sql |
22 |
|
|
* and content from <glassfishv3>/glassfish/lib/install/databases/upgrade/ejbtimer_upgrade_derby.sql (need the applicationID column) |
23 |
|
|
* after this make sure that all the files are owned by the user your GFv3 instance is running as |
24 |
torben |
1280 |
* */ |
25 |
|
|
|
26 |
torben |
1298 |
@Stateless |
27 |
torben |
1301 |
public class TimedEjb /*implements TimedObject*/ { |
28 |
torben |
1279 |
|
29 |
torben |
1301 |
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 |
torben |
1299 |
} |
39 |
|
|
|
40 |
torben |
1301 |
private int count; //storing shared data like this is not recommended !!! |
41 |
|
|
|
42 |
torben |
1280 |
@Resource |
43 |
|
|
private SessionContext sessionCtx; |
44 |
torben |
1279 |
|
45 |
torben |
1280 |
public void startTimer() { |
46 |
torben |
1281 |
|
47 |
torben |
1280 |
TimerService timerService = sessionCtx.getTimerService(); |
48 |
|
|
|
49 |
torben |
1299 |
TimerInfo info = new TimerInfo(); //the info object is not necessary - but can be used to store data with the timer |
50 |
torben |
1281 |
|
51 |
torben |
1299 |
//Timer timer = timerService.createTimer( (5 * 1000), info); //singleshot timer |
52 |
|
|
timerService.createTimer(10*1000, 5000, info ); //repeating timer |
53 |
torben |
1280 |
|
54 |
|
|
// otherwise use schedule expression to create more complex schedules |
55 |
|
|
// in this case fire once every minute when the seconds is == 10 |
56 |
torben |
1281 |
//ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*"); |
57 |
|
|
//System.out.println(se.toString()); |
58 |
|
|
//timerService.createCalendarTimer( se ); |
59 |
torben |
1301 |
|
60 |
|
|
count = 0; |
61 |
torben |
1280 |
|
62 |
|
|
} |
63 |
|
|
|
64 |
torben |
1301 |
//@Override if we used TimedObject interface |
65 |
|
|
//public void ejbTimeout(Timer timer) { |
66 |
torben |
1299 |
|
67 |
torben |
1707 |
//@Timeout |
68 |
torben |
1301 |
public void myEjbTimeout(Timer timer) { |
69 |
|
|
|
70 |
torben |
1299 |
TimerInfo info = (TimerInfo) timer.getInfo(); |
71 |
torben |
1281 |
|
72 |
torben |
1301 |
//any updates to the info object is not pushed back to the timer services |
73 |
torben |
1281 |
|
74 |
torben |
1301 |
System.out.print("timeout .... " + count + " / " + info.getStart() ); |
75 |
|
|
count++; |
76 |
torben |
1281 |
|
77 |
torben |
1301 |
if (count >= 10) { |
78 |
torben |
1281 |
timer.cancel(); |
79 |
|
|
} |
80 |
torben |
1279 |
} |
81 |
|
|
|
82 |
|
|
} |