Implementing an EJB Timer Service is really simple. We talked about EJB3, which involves talking annotations and injection of services:
Code:
<span <span> style="direction: ltr; text-align: left" com.autentia.tutorial.ejb.scheduler class="google-src-text">
package, import java.util.Calendar; import java.util.Collection; import java.util.Iterator;
javax.annotation.Resource import, import javax.ejb.Stateless; javax.ejb.Timeout import, import javax.ejb.Timer;
javax.ejb.TimerService import, import org.apache. commons.logging.Log; import org.apache.commons.logging.LogFactory;
/ ** * Implementation IAlarmScheduler @ local service.
* / @ Stateless public class implements AlarmScheduler IAlarmScheduler (private static final Log log = LogFactory.getLog (AlarmScheduler.class);
/ ** Injection of TimerService * /
@ Resource TimerService TimerService / ** Time to completion: 23 hours * /
private static final int START_HOUR = 23,
/ ** Minutes Runtime: 0 minutes * / private final static int START_MINUTES = 0,
/ ** The time of completion: 00 * / private final static int START_SECONDS = 0,
/ ** Interval Runtime: 1440 = 24 hours * / private static final int INTERVAL_IN_MINUTES = 1440;
/ ** * Lift service * /
public void startUpTimer () (log.info ("startUpTimer - scheduler service alarm is activated."); ShutdownTimer () ; Calendar initialExpiration = Calendar.getInstance (); initialExpiration.set (Calendar.HOUR_OF_DAY, START_HOUR);
initialExpiration.set (Calendar.MINUTE, START_MINUTES); initialExpiration.set (Calendar.SECOND, START_SECONDS); long intervalDuration = new Integer (INTERVAL_IN_MINUTES ). longValue () * 60 * 1000; log.info ("startUpTimer - create new timer service at \" "+ initialExpiration.getTime () +" \ "with \" "+ intervalDuration +" \ "interval in milis." ) timerService.createTimer (initialExpiration.getTime (), intervalDuration, null);)
/ ** * For the service * / public void ShutdownTimer () (Collection </ span> com.autentia.tutorial.ejb.scheduler package, import java.util.Calendar; import java.util.Collection;
import java.util.Iterator;
import javax.annotation.Resource;
javax.ejb.Stateless import, import javax.ejb. Timeout; javax.ejb.Timer import, import javax.ejb.TimerService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/ ** * Implementation IAlarmScheduler @ local service. * / @ Stateless public class implements AlarmScheduler IAlarmScheduler (private static final Log log = LogFactory.getLog (AlarmScheduler.class);
/ ** Injection of TimerService * / @ Resource TimerService TimerService
/ ** Time to completion: 23 hours * / private START_HOUR final static int = 23;
/ ** Minutes Runtime: 0 minutes * / private static final int START_MINUTES = 0,
/ ** The time of completion: 00 * / private static final int START_SECONDS = 0,
/ ** Interval Runtime: 1440 = 24 hours * / private final static int INTERVAL_IN_MINUTES = 1440;
/ ** * Lift service * / public void startUpTimer () (log.info (startUpTimer - scheduler service alarm is activated. "); ShutdownTimer (); Calendar initialExpiration = Calendar.getInstance (); initialExpiration.set (Calendar.HOUR_OF_DAY, START_HOUR); initialExpiration.set (Calendar.MINUTE, START_MINUTES); initialExpiration.set (Calendar.SECOND, START_SECONDS); long intervalDuration = new Integer (INTERVAL_IN_MINUTES). longValue ( ) * 60 * 1000; log.info ("startUpTimer - create new timer service at \" "+ initialExpiration.getTime () +" \ "with \" "+ intervalDuration +" \ "interval in milis.") timerService.createTimer (initialExpiration.getTime (), intervalDuration, null))
/ ** * For the service * / public void ShutdownTimer () (Collection </ span> <Timer> <span> <span style = "direction: ltr; text- align: left "class =" google-src-text "> timers = timerService.getTimers () log.info (" ShutdownTimer - existing timers? "+ timers) if (timers! = null) (for (Iterator iterator = timers.iterator (); iterator.hasNext ()) (Timer t = (Timer) iterator.next (); t.cancel () log.info ("ShutdownTimer - timer \" "+ t +" \ "canceled. ");)))
/ ** * callback method is invoked at the end of the interval defined * / public void execute @ Timeout (Timer timer) (log.info (" executing - "+ timer.getInfo ()) / / TODO: Implement the logic of the alarm.)) </ span> timers = timerService.getTimers () log.info ("ShutdownTimer - existing timers?" + timers) if (timers! = null) (for (Iterator iterator = timers.iterator (); iterator.hasNext ()) (t = Timer (Timer) iterator.next (); t.cancel () log.info ("ShutdownTimer - timer \" "+ t +" \ "canceled." );))) / ** * Invoked That callback method at the end of the interval defined * /
public void execute @ Timeout (Timer timer) (log.info ("executing -" + timer.getInfo ())
/ / TODO : Implements the logic of the alarm .))</ span>
Bookmarks