/[projects]/miscJava/Test3/src/dk/thoerup/schedulesamples/TimedEjb.java
ViewVC logotype

Annotation of /miscJava/Test3/src/dk/thoerup/schedulesamples/TimedEjb.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1281 - (hide annotations) (download)
Thu Apr 7 21:26:36 2011 UTC (13 years, 1 month ago) by torben
File size: 1567 byte(s)
a started timer can also be cancelled again
1 torben 1279 package dk.thoerup.schedulesamples;
2    
3 torben 1280 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 torben 1279 import javax.ejb.Stateless;
10     import javax.ejb.TimedObject;
11     import javax.ejb.Timer;
12 torben 1280 import javax.ejb.TimerService;
13 torben 1279
14 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
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 torben 1279 @Stateless
23 torben 1280 public class TimedEjb implements TimedObject {
24 torben 1279
25 torben 1280 @Resource
26     private SessionContext sessionCtx;
27 torben 1281 int count;
28 torben 1279
29 torben 1280 public void startTimer() {
30 torben 1281
31 torben 1280 TimerService timerService = sessionCtx.getTimerService();
32    
33 torben 1281
34 torben 1280 //Timer timer = timerService.createTimer( (5 * 1000), null); //singleshot timer
35 torben 1281 timerService.createTimer(10*1000, 5000, null); //repeating timer
36 torben 1280
37     // otherwise use schedule expression to create more complex schedules
38     // in this case fire once every minute when the seconds is == 10
39 torben 1281 //ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*");
40     //System.out.println(se.toString());
41     //timerService.createCalendarTimer( se );
42 torben 1280
43 torben 1281 count = 0;
44 torben 1280 }
45    
46     @Override
47 torben 1279 public void ejbTimeout(Timer timer) {
48 torben 1281
49     count++;
50    
51     System.out.print("timeout .... " + count);
52    
53    
54     if (count >= 10) {
55     timer.cancel();
56     }
57 torben 1279 }
58    
59     }

  ViewVC Help
Powered by ViewVC 1.1.20