--- miscJava/Test3/src/dk/thoerup/schedulesamples/TimedEjb.java 2011/04/07 20:20:04 1279 +++ miscJava/Test3/src/dk/thoerup/schedulesamples/TimedEjb.java 2011/04/07 21:17:29 1280 @@ -1,16 +1,49 @@ 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*/ { +public class TimedEjb implements TimedObject { + + @Resource + private SessionContext sessionCtx; + public void startTimer() { + TimerService timerService = sessionCtx.getTimerService(); + + Calendar now = Calendar.getInstance(); + //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 ); + } + + @Override public void ejbTimeout(Timer timer) { System.out.print("timeout ...."); - } }