22

如何配置计划间隔:

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

在应用程序代码之外?

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

3 回答 3

18

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

    <session>
         <ejb-name>MessageService</ejb-name>
         <local-bean/>
         <ejb-class>ejb.MessageService</ejb-class>
         <session-type>Stateless</session-type>
         <timer>
            <schedule>
                <second>0/18</second>
                <minute>*</minute>
                <hour>*</hour>
            </schedule>
            <timeout-method>
                <method-name>showMessage</method-name>
            </timeout-method>
         </timer>
    </session>

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

@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");
    }

}
于 2011-03-07T04:12:47.500 回答
9

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

18.2.2 自动创建定时器

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

我对部署描述符 XLM 模式的理解是,您使用<timer>元素中的<session>元素来定义它。

<xsd:element name="timer"
             type="javaee:timerType"
             minOccurs="0"
             maxOccurs="unbounded"/>

有关详细信息(尤其是and元素) ,请参见timerType复杂类型的定义。scheduletimeout-method

参考

  • EJB 3.1 规范
    • 第 18.2.2 节“自动创建定时器”
    • 第 19.5 节“部署描述符 XML 模式”(第 580 页,第 583-p584 页)
于 2010-10-16T23:25:23.500 回答
0
  1. ejb-jar.xml

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

<session>
  <ejb-name>AppTimerService</ejb-name>
  <ejb-class>my.app.AppTimerService</ejb-class>
  <session-type>Singleton</session-type>
  <timer>
    <schedule>
      <second>*/10</second>
      <minute>*</minute>
      <hour>*</hour>
    </schedule>
    <timeout-method>
      <method-name>timeout</method-name>
      <method-params>
        <method-param>javax.ejb.Timer</method-param>
      </method-params>
   </timeout-method>
 </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_ejb帖子。

  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 表达式。

于 2015-06-23T07:16:06.250 回答