请参阅EJB 3.1
@ScheduleAPI。我们为该规范选择的API比cron更接近Quartz语法-两者之间的微小差异。
这是一个注释示例:
package org.superbiz.corn;import javax.ejb.Lock;import javax.ejb.LockType;import javax.ejb.Schedule;import javax.ejb.Schedules;import javax.ejb.Singleton;import java.util.concurrent.atomic.AtomicInteger;@Singleton@Lock(LockType.READ) // allows timers to execute in parallelpublic class FarmerBrown { private final AtomicInteger checks = new AtomicInteger(); @Schedules({ @Schedule(month = "5", dayOfMonth = "20-Last", minute = "0", hour = "8"), @Schedule(month = "6", dayOfMonth = "1-10", minute = "0", hour = "8") }) private void plantTheCorn() { // Dig out the planter!!! } @Schedules({ @Schedule(month = "9", dayOfMonth = "20-Last", minute = "0", hour = "8"), @Schedule(month = "10", dayOfMonth = "1-10", minute = "0", hour = "8") }) private void harvestTheCorn() { // Dig out the combine!!! } @Schedule(second = "*", minute = "*", hour = "*") private void checkonTheDaughters() { checks.incrementAndGet(); } public int getChecks() { return checks.get(); }}
完整的源代码在这里
您可以通过Scheduleexpression类以编程方式执行相同的 *** 作,该类只是上述批注的可构造版本。如果计划是在代码中完成的,则上面的示例如下所示:
package org.superbiz.corn;import javax.annotation.PostConstruct;import javax.annotation.Resource;import javax.ejb.Lock;import javax.ejb.LockType;import javax.ejb.Scheduleexpression;import javax.ejb.Singleton;import javax.ejb.Startup;import javax.ejb.Timeout;import javax.ejb.Timer;import javax.ejb.TimerConfig;import javax.ejb.TimerService;import java.util.concurrent.atomic.AtomicInteger;@Singleton@Lock(LockType.READ) // allows timers to execute in parallel@Startuppublic class FarmerBrown { private final AtomicInteger checks = new AtomicInteger(); @Resource private TimerService timerService; @PostConstruct private void construct() { final TimerConfig plantTheCorn = new TimerConfig("plantTheCorn", false); timerService.createCalendarTimer(new Scheduleexpression().month(5).dayOfMonth("20-Last").minute(0).hour(8), plantTheCorn); timerService.createCalendarTimer(new Scheduleexpression().month(6).dayOfMonth("1-10").minute(0).hour(8), plantTheCorn); final TimerConfig harvestTheCorn = new TimerConfig("harvestTheCorn", false); timerService.createCalendarTimer(new Scheduleexpression().month(9).dayOfMonth("20-Last").minute(0).hour(8), harvestTheCorn); timerService.createCalendarTimer(new Scheduleexpression().month(10).dayOfMonth("1-10").minute(0).hour(8), harvestTheCorn); final TimerConfig checkonTheDaughters = new TimerConfig("checkOnTheDaughters", false); timerService.createCalendarTimer(new Scheduleexpression().second("*").minute("*").hour("*"), checkOnTheDaughters); } @Timeout public void timeout(Timer timer) { if ("plantTheCorn".equals(timer.getInfo())) { plantTheCorn(); } else if ("harvestTheCorn".equals(timer.getInfo())) { harvestTheCorn(); } else if ("checkOnTheDaughters".equals(timer.getInfo())) { checkonTheDaughters(); } } private void plantTheCorn() { // Dig out the planter!!! } private void harvestTheCorn() { // Dig out the combine!!! } private void checkonTheDaughters() { checks.incrementAndGet(); } public int getChecks() { return checks.get(); }}
此示例的来源在这里
附带说明,两个示例都可以在纯IDE中运行,并且具有使用Embeddable
EJBContainer
API的测试用例,这也是EJB 3.1中的新增功能。
- @时间表
- 静态配置
- 可能有许多调度方法
- 无法传递参数
- 无法取消
以上都是在部署描述符中完成的,因此仅限于可以预先配置的内容。动态性更高的版本使用TimerService的以下签名:
TimerService.createCalendarTimer(javax.ejb.Scheduleexpression,javax.ejb.TimerConfig)
- Scheduleexpression
还要注意,这里有一个拦截器
@AroundTimeout注释,其功能与拦截器相同,
@AroundInvoke并允许拦截器参与bean的计时器功能。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)