package dk.thoerup.schedulesamples; import java.util.Calendar; import java.util.Date; import javax.annotation.Resource; import javax.ejb.ScheduleExpression; import javax.ejb.SessionContext; import javax.ejb.Stateless; import javax.ejb.TimedObject; import javax.ejb.Timer; import javax.ejb.TimerService; /* GFv3: before using a timed bean make sure the ejb timer service is correctly configured with a datasource and the timer table is created * cd /inst/glassfishv3/glassfish/domains/domain1/lib/databases/ejbtimer * /usr/lib/jvm/java-6-sun/db/bin/ij * connect 'jdbc:derby:.'; * paste content from /home/torben/inst/glassfishv3/glassfish/lib/install/databases/ejbtimer_derby.sql * * */ @Stateless public class TimedEjb implements TimedObject { @Resource private SessionContext sessionCtx; int count; public void startTimer() { TimerService timerService = sessionCtx.getTimerService(); //Timer timer = timerService.createTimer( (5 * 1000), null); //singleshot timer timerService.createTimer(10*1000, 5000, null); //repeating timer // otherwise use schedule expression to create more complex schedules // in this case fire once every minute when the seconds is == 10 //ScheduleExpression se = new ScheduleExpression().second(10).minute("*").hour("*"); //System.out.println(se.toString()); //timerService.createCalendarTimer( se ); count = 0; } @Override public void ejbTimeout(Timer timer) { count++; System.out.print("timeout .... " + count); if (count >= 10) { timer.cancel(); } } }