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

Moran picture Moran · Oct 16, 2010 · Viewed 11.6k times · Source

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

Answer

victor herrera picture victor herrera · Mar 7, 2011

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

}