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

发布于 2024-09-28 06:26:46 字数 184 浏览 10 评论 0原文

如何

配置计划间隔: @Schedule(persistent=true, 分钟=“*”,秒=“*/5”,小时=“*”)

在应用程序代码之外

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

How can I configure a schedule intervals:

@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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_失温 2024-10-05 06:26:46

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

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

}

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

}
饮湿 2024-10-05 06:26:46

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

18.2.2 自动定时器创建

定时器服务支持
自动创建一个基于定时器
bean 类中的元数据或
部署描述符。这允许 Bean 开发人员安排计时器
不依赖 bean 调用
以编程方式调用其中之一
Timer Service定时器创建方法。
自动创建的计时器是
由容器创建的结果
应用程序部署。

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

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

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

参考资料

  • EJB 3.1 规范
    • 第 18.2.2 节“自动计时器创建”
    • 第 19.5 节“部署描述符 XML 架构”(第 580 页、p583-p584)

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

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.

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

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

See the definition of the timerType complex type for the details (in particular the schedule and timeout-method elements).

References

  • EJB 3.1 Specification
    • Section 18.2.2 "Automatic Timer Creation"
    • Section 19.5 "Deployment Descriptor XML Schema" (p. 580, p583-p584)
凑诗 2024-10-05 06:26:46
  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_understand_the_ejb 帖子。

  1. 属性文件变体

您可以读取 .properties 文件并以编程方式创建计时器,

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 表达式吗?

  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:

<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_ejb post.

  1. Properties file variant

You can read .properties file and programmatically create 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文