/[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 1280 by torben, Thu Apr 7 21:17:29 2011 UTC revision 1298 by torben, Mon Apr 18 12:28:14 2011 UTC
# Line 12  import javax.ejb.Timer; Line 12  import javax.ejb.Timer;
12  import javax.ejb.TimerService;  import javax.ejb.TimerService;
13    
14  /* 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
15   * cd /inst/glassfishv3/glassfish/domains/domain1/lib/databases/ejbtimer   * cd <glassfishv3>/glassfish/domains/domain1/lib/databases/ejbtimer
16   * /usr/lib/jvm/java-6-sun/db/bin/ij   * /usr/lib/jvm/java-6-sun/db/bin/ij
17   * connect 'jdbc:derby:.';   * connect 'jdbc:derby:.';
18   * 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
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   * */   * */
22    
23  @Stateless  @Stateless
# Line 24  public class TimedEjb implements TimedOb Line 25  public class TimedEjb implements TimedOb
25    
26          @Resource          @Resource
27          private SessionContext sessionCtx;          private SessionContext sessionCtx;
28            int count;
29                    
30          public void startTimer() {          public void startTimer() {
31                    
32                  TimerService timerService = sessionCtx.getTimerService();                  TimerService timerService = sessionCtx.getTimerService();
33                                    
34                  Calendar now = Calendar.getInstance();  
35                  //Timer timer = timerService.createTimer( (5 * 1000), null); //singleshot timer                  //Timer timer = timerService.createTimer( (5 * 1000), null); //singleshot timer
36                  //timerService.createTimer(10*1000, 5000, null); //repeating timer                  timerService.createTimer(10*1000, 5000, null); //repeating timer
37                                    
38                  // otherwise use schedule expression to create more complex schedules                  // otherwise use schedule expression to create more complex schedules
39                  // in this case fire once every minute when the seconds is == 10                  // in this case fire once every minute when the seconds is == 10
40                  ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*");                  //ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*");            
41                    //System.out.println(se.toString());
42                    //timerService.createCalendarTimer( se );
43                                    
44                  System.out.println(se.toString());                  count = 0;
                 timerService.createCalendarTimer( se );    
45          }          }
46                    
47          @Override          @Override
48          public void ejbTimeout(Timer timer) {          public void ejbTimeout(Timer timer) {
49                  System.out.print("timeout ....");                  
50                    count++;
51                    
52                    System.out.print("timeout .... " + count);
53                    
54                    
55                    if (count >= 10) {
56                            timer.cancel();
57                    }
58          }          }
59    
60  }  }

Legend:
Removed from v.1280  
changed lines
  Added in v.1298

  ViewVC Help
Powered by ViewVC 1.1.20