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

穿精又带淫゛_ 提交于 2019-11-30 04:54:34

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

}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!