是否可以在应用程序代码之外配置EJB 3.1 @Schedule?

如何配置计划间隔:

@Schedule(persistent=true, minute="*", second="*/5", hour="*")

在应用程序代码之外?

  1. 如何在ejb-jar.xml中配置它?
  2. 我可以在应用程序之外配置它(属性文件的种类)吗?

以下是部署描述符中的调度示例:

   MessageService  ejb.MessageService Stateless   0/18 * *   showMessage    

配置定时器的另一种方法是使用编程调度。

 @Singleton @Startup public class TimedBean{ @Resource private TimerService service; @PostConstruct public void init(){ ScheduleExpression exp=new ScheduleExpression(); exp.hour("*") .minute("*") .second("*/10"); service.createCalendarTimer(exp); } @Timeout public void timeOut(){ System.out.println(new Date()); System.out.println("time out"); } } 

根据EJB 3.1规范,可以通过注释或通过ejb-jar.xml部署描述符配置自动计时器。

18.2.2自动定时器创建

Timer Service支持基于bean类或部署描述符中的元数据自动创建计时器。 这允许bean开发人员在不依赖bean调用的情况下调度计时器,以编程方式调用其中一个Timer Service计时器创建方法。 由于应用程序部署,容器会创建自动创建的计时器。

我对部署描述符XLM模式的理解是你使用元素中的元素来定义它。

  

有关详细信息,请参阅timerType复杂类型的定义(特别是scheduletimeout-method元素)。

参考

  • EJB 3.1规范
    • 第18.2.2节“自动定时器创建”
    • 第19.5节“部署描述符XML模式”(p.580,p583-p584)
  1. ejb-jar.xml中

对我来说,ejb-jar.xml变体开始在TomEE上工作,我只在超时方法中传递javax.ejb.Timer参数:

  AppTimerService my.app.AppTimerService Singleton   */10 * *   timeout  javax.ejb.Timer    

 public class AppTimerService { public void timeout(Timer timer) { System.out.println("[in timeout method]"); } } 

谢谢https://blogs.oracle.com/arungupta/entry/totd_146_understanding_the_ejbpost。

  1. 属性文件变体

您可以读取.properties文件并以编程方式创建Timer

 ScheduleExpression schedule = new ScheduleExpression(); schedule.hour(hourProperty);//previously read property from .properties file schedule.minute(minuteProperty);//previously read property from .properties file Timer timer = timerService.createCalendarTimer(schedule); 

但我找不到可能在EJB中使用cron表达式。