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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1297 by torben, Mon Apr 18 12:04:33 2011 UTC revision 1301 by torben, Mon Apr 18 14:39:01 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.schedulesamples;  package dk.thoerup.schedulesamples;
2    
3    import java.io.Serializable;
4    
5  import java.util.Calendar;  import java.util.Calendar;
6  import java.util.Date;  import java.util.Date;
7    
# Line 8  import javax.ejb.ScheduleExpression; Line 10  import javax.ejb.ScheduleExpression;
10  import javax.ejb.SessionContext;  import javax.ejb.SessionContext;
11  import javax.ejb.Stateless;  import javax.ejb.Stateless;
12  import javax.ejb.TimedObject;  import javax.ejb.TimedObject;
13    import javax.ejb.Timeout;
14  import javax.ejb.Timer;  import javax.ejb.Timer;
15  import javax.ejb.TimerService;  import javax.ejb.TimerService;
16    
17  /* GFv3: before using a timed bean make sure the ejb timer service is correctly configured with a datasource and the timer table is created  /* GFv3: before using a timed bean make sure the ejb timer service is correctly configured with a datasource and the timer table is created
18   * cd /inst/glassfishv3/glassfish/domains/domain1/lib/databases/ejbtimer   * cd <glassfishv3>/glassfish/domains/domain1/lib/databases/ejbtimer
19   * /usr/lib/jvm/java-6-sun/db/bin/ij   * /usr/lib/jvm/java-6-sun/db/bin/ij
20   * connect 'jdbc:derby:.';   * connect 'jdbc:derby:.';
21   * paste content from /home/torben/inst/glassfishv3/glassfish/lib/install/databases/ejbtimer_derby.sql   * paste content from <glassfishv3>/glassfish/lib/install/databases/ejbtimer_derby.sql
22   *   * and content from <glassfishv3>/glassfish/lib/install/databases/upgrade/ejbtimer_upgrade_derby.sql (need the applicationID column)
23     * after this make sure that all the files are owned by the user your GFv3 instance is running as
24   * */   * */
25    
26  //@Stateless  @Stateless
27  public class TimedEjb implements TimedObject {  public class TimedEjb /*implements TimedObject*/ {
28    
29            public static class TimerInfo implements Serializable {
30                    static final long serialVersionUID = 1L;
31    
32                    private long start = System.currentTimeMillis();
33    
34                    public long getStart() {
35                            return start;
36                    }
37    
38            }
39    
40            private int count; //storing shared data like this is not recommended !!!
41    
42          @Resource          @Resource
43          private SessionContext sessionCtx;          private SessionContext sessionCtx;
         int count;  
44                    
45          public void startTimer() {          public void startTimer() {
46                                    
47                  TimerService timerService = sessionCtx.getTimerService();                  TimerService timerService = sessionCtx.getTimerService();
48                                    
49                    TimerInfo info = new TimerInfo(); //the info object is not necessary - but can be used to store data with the timer
50    
51                  //Timer timer = timerService.createTimer( (5 * 1000), null); //singleshot timer                  //Timer timer = timerService.createTimer( (5 * 1000), info); //singleshot timer
52                  timerService.createTimer(10*1000, 5000, null); //repeating timer                  timerService.createTimer(10*1000, 5000, info ); //repeating timer
53                                    
54                  // otherwise use schedule expression to create more complex schedules                  // otherwise use schedule expression to create more complex schedules
55                  // in this case fire once every minute when the seconds is == 10                  // in this case fire once every minute when the seconds is == 10
56                  //ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*");                              //ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*");            
57                  //System.out.println(se.toString());                  //System.out.println(se.toString());
58                  //timerService.createCalendarTimer( se );                  //timerService.createCalendarTimer( se );
59                    
60                  count = 0;                  count = 0;
61                    
62          }          }
63                    
64          @Override          //@Override if we used TimedObject interface
65          public void ejbTimeout(Timer timer) {          //public void ejbTimeout(Timer timer) {
66                    
67                  count++;          @Timeout
68            public void myEjbTimeout(Timer timer) {
69    
70                    TimerInfo info = (TimerInfo) timer.getInfo();
71                                    
72                  System.out.print("timeout .... " + count);                  //any updates to the info object is not pushed back to the timer services
73                                    
74                    System.out.print("timeout .... " + count + " / " + info.getStart() );
75                    count++;
76                                    
77                  if (count >= 10) {                  if (count >= 10) {
78                          timer.cancel();                          timer.cancel();

Legend:
Removed from v.1297  
changed lines
  Added in v.1301

  ViewVC Help
Powered by ViewVC 1.1.20