1 |
package dk.thoerup.schedulesamples; |
package dk.thoerup.schedulesamples; |
2 |
|
|
3 |
|
import java.util.Calendar; |
4 |
|
import java.util.Date; |
5 |
|
|
6 |
|
import javax.annotation.Resource; |
7 |
|
import javax.ejb.ScheduleExpression; |
8 |
|
import javax.ejb.SessionContext; |
9 |
import javax.ejb.Stateless; |
import javax.ejb.Stateless; |
10 |
import javax.ejb.TimedObject; |
import javax.ejb.TimedObject; |
11 |
import javax.ejb.Timer; |
import javax.ejb.Timer; |
12 |
|
import javax.ejb.TimerService; |
13 |
|
|
14 |
|
/* GFv3: before using a timed bean make sure the ejb timer service is correctly configured with a datasource and the timer table is created |
15 |
|
* cd /inst/glassfishv3/glassfish/domains/domain1/lib/databases/ejbtimer |
16 |
|
* /usr/lib/jvm/java-6-sun/db/bin/ij |
17 |
|
* connect 'jdbc:derby:.'; |
18 |
|
* paste content from /home/torben/inst/glassfishv3/glassfish/lib/install/databases/ejbtimer_derby.sql |
19 |
|
* |
20 |
|
* */ |
21 |
|
|
22 |
@Stateless |
@Stateless |
23 |
public class TimedEjb /*implements TimedObject*/ { |
public class TimedEjb implements TimedObject { |
24 |
|
|
25 |
|
@Resource |
26 |
|
private SessionContext sessionCtx; |
27 |
|
|
28 |
|
|
29 |
|
public void startTimer() { |
30 |
|
TimerService timerService = sessionCtx.getTimerService(); |
31 |
|
|
32 |
|
Calendar now = Calendar.getInstance(); |
33 |
|
//Timer timer = timerService.createTimer( (5 * 1000), null); //singleshot timer |
34 |
|
//timerService.createTimer(10*1000, 5000, null); //repeating timer |
35 |
|
|
36 |
|
// otherwise use schedule expression to create more complex schedules |
37 |
|
// in this case fire once every minute when the seconds is == 10 |
38 |
|
ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*"); |
39 |
|
|
40 |
|
System.out.println(se.toString()); |
41 |
|
timerService.createCalendarTimer( se ); |
42 |
|
} |
43 |
|
|
44 |
|
@Override |
45 |
public void ejbTimeout(Timer timer) { |
public void ejbTimeout(Timer timer) { |
46 |
System.out.print("timeout ...."); |
System.out.print("timeout ...."); |
|
|
|
47 |
} |
} |
48 |
|
|
49 |
} |
} |