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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1300 - (show 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 package dk.thoerup.schedulesamples;
2
3 import java.io.Serializable;
4
5 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 import javax.ejb.Stateless;
12 import javax.ejb.TimedObject;
13 import javax.ejb.Timer;
14 import javax.ejb.TimerService;
15
16 /* 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 * cd <glassfishv3>/glassfish/domains/domain1/lib/databases/ejbtimer
18 * /usr/lib/jvm/java-6-sun/db/bin/ij
19 * connect 'jdbc:derby:.';
20 * 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 * */
24
25 @Stateless
26 public class TimedEjb implements TimedObject {
27
28 class TimerInfo implements Serializable {
29 public int count;
30 }
31
32 @Resource
33 private SessionContext sessionCtx;
34
35 public void startTimer() {
36
37 TimerService timerService = sessionCtx.getTimerService();
38
39 TimerInfo info = new TimerInfo(); //the info object is not necessary - but can be used to store data with the timer
40
41 //Timer timer = timerService.createTimer( (5 * 1000), info); //singleshot timer
42 timerService.createTimer(10*1000, 5000, info ); //repeating timer
43
44 // otherwise use schedule expression to create more complex schedules
45 // in this case fire once every minute when the seconds is == 10
46 //ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*");
47 //System.out.println(se.toString());
48 //timerService.createCalendarTimer( se );
49
50 }
51
52 @Override
53 public void ejbTimeout(Timer timer) {
54
55 TimerInfo info = (TimerInfo) timer.getInfo();
56
57 info.count++;
58
59 System.out.print("timeout .... " + info.count);
60
61
62 if (info.count >= 10) {
63 timer.cancel();
64 }
65 }
66
67 }

  ViewVC Help
Powered by ViewVC 1.1.20