繁体   English   中英

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

[英]Can the EJB 3.1 @Schedule be configured outside of the application code?

如何配置计划间隔:

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

在应用程序代码之外?

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

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

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

}

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

18.2.2自动定时器创建

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

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

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

有关详细信息,请参阅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参数:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM