/[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 1298 - (hide annotations) (download)
Mon Apr 18 12:28:14 2011 UTC (13 years, 1 month ago) by torben
File size: 1774 byte(s)
Enable timerejb code again - and describe the last few missing steps
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 torben 1298 * cd <glassfishv3>/glassfish/domains/domain1/lib/databases/ejbtimer
16 torben 1280 * /usr/lib/jvm/java-6-sun/db/bin/ij
17     * connect 'jdbc:derby:.';
18 torben 1298 * paste content from <glassfishv3>/glassfish/lib/install/databases/ejbtimer_derby.sql
19     * and content from <glassfishv3>/glassfish/lib/install/databases/upgrade/ejbtimer_upgrade_derby.sql (need the applicationID column)
20     * after this make sure that all the files are owned by the user your GFv3 instance is running as
21 torben 1280 * */
22    
23 torben 1298 @Stateless
24 torben 1280 public class TimedEjb implements TimedObject {
25 torben 1279
26 torben 1280 @Resource
27     private SessionContext sessionCtx;
28 torben 1281 int count;
29 torben 1279
30 torben 1280 public void startTimer() {
31 torben 1281
32 torben 1280 TimerService timerService = sessionCtx.getTimerService();
33    
34 torben 1281
35 torben 1280 //Timer timer = timerService.createTimer( (5 * 1000), null); //singleshot timer
36 torben 1281 timerService.createTimer(10*1000, 5000, null); //repeating timer
37 torben 1280
38     // otherwise use schedule expression to create more complex schedules
39     // in this case fire once every minute when the seconds is == 10
40 torben 1281 //ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*");
41     //System.out.println(se.toString());
42     //timerService.createCalendarTimer( se );
43 torben 1280
44 torben 1281 count = 0;
45 torben 1280 }
46    
47     @Override
48 torben 1279 public void ejbTimeout(Timer timer) {
49 torben 1281
50     count++;
51    
52     System.out.print("timeout .... " + count);
53    
54    
55     if (count >= 10) {
56     timer.cancel();
57     }
58 torben 1279 }
59    
60     }

  ViewVC Help
Powered by ViewVC 1.1.20