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

3

23

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)?
Venal answered 16/10, 2010 at 20:26 Comment(0)
F
19

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

}
Folie answered 7/3, 2011 at 4:12 Comment(3)
suppose i have days e.g(mon,tue,....,friday) and working hrs(9-18) how can i apply to this method.Celio
this is not work correctly for: @Schedule(second = "30", minute = "*/5", hour = "*", persistent = false)Undergird
In JBoss EAP 7.0.0 this needs to be changed from <session-type>Stateless</session-type> to <session-type>Singleton</session-type>Hodgkins
N
9

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)
Neumeyer answered 16/10, 2010 at 23:25 Comment(0)
F
0
  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.

Fala answered 23/6, 2015 at 7:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.