/[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 1298 by torben, Mon Apr 18 12:28:14 2011 UTC revision 1299 by torben, Mon Apr 18 14:05:29 2011 UTC
# Line 23  import javax.ejb.TimerService; Line 23  import javax.ejb.TimerService;
23  @Stateless  @Stateless
24  public class TimedEjb implements TimedObject {  public class TimedEjb implements TimedObject {
25    
26            class TimerInfo implements Serializable {
27                    public int count;
28            }
29    
30          @Resource          @Resource
31          private SessionContext sessionCtx;          private SessionContext sessionCtx;
         int count;  
32                    
33          public void startTimer() {          public void startTimer() {
34                                    
35                  TimerService timerService = sessionCtx.getTimerService();                  TimerService timerService = sessionCtx.getTimerService();
36                                    
37                    TimerInfo info = new TimerInfo(); //the info object is not necessary - but can be used to store data with the timer
38    
39                  //Timer timer = timerService.createTimer( (5 * 1000), null); //singleshot timer                  //Timer timer = timerService.createTimer( (5 * 1000), info); //singleshot timer
40                  timerService.createTimer(10*1000, 5000, null); //repeating timer                  timerService.createTimer(10*1000, 5000, info ); //repeating timer
41                                    
42                  // otherwise use schedule expression to create more complex schedules                  // otherwise use schedule expression to create more complex schedules
43                  // in this case fire once every minute when the seconds is == 10                  // in this case fire once every minute when the seconds is == 10
# Line 41  public class TimedEjb implements TimedOb Line 45  public class TimedEjb implements TimedOb
45                  //System.out.println(se.toString());                  //System.out.println(se.toString());
46                  //timerService.createCalendarTimer( se );                  //timerService.createCalendarTimer( se );
47                                    
                 count = 0;  
48          }          }
49                    
50          @Override          @Override
51          public void ejbTimeout(Timer timer) {          public void ejbTimeout(Timer timer) {
52    
53                    TimerInfo info = (TimerInfo) timer.getInfo();
54                                    
55                  count++;                  info.count++;
56                                    
57                  System.out.print("timeout .... " + count);                  System.out.print("timeout .... " + info.count);
58                                    
59                                    
60                  if (count >= 10) {                  if (info.count >= 10) {
61                          timer.cancel();                          timer.cancel();
62                  }                  }
63          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.20