栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用“ crontab语法”进行EJB调度任务

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用“ crontab语法”进行EJB调度任务

请参阅EJB 3.1

@Schedule
API。我们为该规范选择的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中的新增功能。

@Schedule vs Scheduleexpression

  • @时间表
    • 静态配置
    • 可能有许多调度方法
    • 无法传递参数
    • 无法取消

以上都是在部署描述符中完成的,因此仅限于可以预先配置的内容。动态性更高的版本使用TimerService的以下签名:

TimerService.createCalendarTimer(javax.ejb.Scheduleexpression,javax.ejb.TimerConfig)

  • Scheduleexpression
    • 动态创建
    • 恰好一个@Timeout支持所有Scheduleexpression
    • 超时方法必须
      javax.ejb.Timer
      作为参数
    • 可以传递参数
    • 调用方将参数包装在TimerConfig.setInfo(Serializable)对象中
    • @Timeout方法通过Timer.getInfo()访问它们
    • 可以被调用者或@Timeout方法取消

还要注意,这里有一个拦截器

@AroundTimeout
注释,其功能与拦截器相同,
@AroundInvoke
并允许拦截器参与bean的计时器功能。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570359.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号