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

  ViewVC Help
Powered by ViewVC 1.1.20