How can I configure a schedule intervals:
@Schedule(persistent=true, minute="*", second="*/5", hour="*")
outside of the application code?
- How can I configure it in ejb-jar.xml?
- Can I configure it outside the application (kind of properties file)?
How can I configure a schedule intervals:
@Schedule(persistent=true, minute="*", second="*/5", hour="*")
outside of the application code?
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");
}
}
<session-type>Stateless</session-type>
to <session-type>Singleton</session-type>
–
Hodgkins 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).
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.
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.
© 2022 - 2024 — McMap. All rights reserved.