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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3950636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:06:10  来源:igfitidea点击:

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

javajava-ee-6ejb-3.1schedule

提问by Moran

How can I configure a schedule intervals:

如何配置计划间隔:

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

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

outside of the application code?

在应用程序代码之外?

  1. How can I configure it in ejb-jar.xml?
  2. Can I configure it outside the application (kind of properties file)?
  1. 如何在 ejb-jar.xml 中配置它?
  2. 我可以在应用程序之外配置它吗(一种属性文件)?

回答by victor herrera

Here is an example of a scheduling in the deployment descriptor:

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

    <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>

Another way of configuring timers is with a programmatic scheduling.

另一种配置定时器的方法是程序化调度。

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

}

回答by Pascal Thivent

According to the EJB 3.1 specification, automatic timers can be configured through annotations or through the ejb-jar.xmldeployment descriptor.

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

18.2.2 Automatic Timer Creation

The Timer Service supports the automatic creation of a timer based on metadata in the bean class or deployment descriptor. This allows the bean developer to schedule a timer without relying on a bean invocation to programmatically invoke one of the Timer Service timer creation methods. Automatically created timers are created by the container as a result of application deployment.

18.2.2 自动定时器创建

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

And my understanding of the deployment descriptor XLM schema is that you define it using a <timer>element inside a <session>element.

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

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

See the definition of the timerTypecomplex type for the details (in particular the scheduleand timeout-methodelements).

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

References

参考

  • EJB 3.1 Specification
    • Section 18.2.2 "Automatic Timer Creation"
    • Section 19.5 "Deployment Descriptor XML Schema" (p. 580, p583-p584)
  • EJB 3.1 规范
    • 第 18.2.2 节“自动创建定时器”
    • 第 19.5 节“部署描述符 XML 架构”(第 580 页、p583-p584)

回答by Dedkov Vadim

  1. ejb-jar.xml
  1. ejb-jar.xml

For me, ejb-jar.xml variant started to work on TomEE only I pass javax.ejb.Timer parameter in timeout method:

对我来说,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]");
    }
}

Thanks https://blogs.oracle.com/arungupta/entry/totd_146_understanding_the_ejbpost.

感谢https://blogs.oracle.com/arungupta/entry/totd_146_understanding_the_ejb帖子。

  1. Properties file variant
  1. 属性文件变体

You can read .properties file and programmatically create Timer

您可以读取 .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);

But I can't find may we use cron expressions in EJB.

但是我找不到我们是否可以在 EJB 中使用 cron 表达式。