package dk.thoerup.schedulesamples; import java.io.Serializable; 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 /glassfish/domains/domain1/lib/databases/ejbtimer * /usr/lib/jvm/java-6-sun/db/bin/ij * connect 'jdbc:derby:.'; * paste content from /glassfish/lib/install/databases/ejbtimer_derby.sql * and content from /glassfish/lib/install/databases/upgrade/ejbtimer_upgrade_derby.sql (need the applicationID column) * after this make sure that all the files are owned by the user your GFv3 instance is running as * */ @Stateless public class TimedEjb implements TimedObject { class TimerInfo implements Serializable { public int count; } @Resource private SessionContext sessionCtx; public void startTimer() { TimerService timerService = sessionCtx.getTimerService(); TimerInfo info = new TimerInfo(); //the info object is not necessary - but can be used to store data with the timer //Timer timer = timerService.createTimer( (5 * 1000), info); //singleshot timer timerService.createTimer(10*1000, 5000, info ); //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 ); } @Override public void ejbTimeout(Timer timer) { TimerInfo info = (TimerInfo) timer.getInfo(); info.count++; System.out.print("timeout .... " + info.count); if (info.count >= 10) { timer.cancel(); } } }