Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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)?
like image 778
Moran Avatar asked Oct 16 '10 20:10

Moran


People also ask

What is EJB timer?

The EJB timer service provides a reliable and transactional notification service for timed events. Timer notifications may be scheduled to occur at a specific time, after a specific elapsed duration, or at specific recurring intervals. You can define callback methods on your EJB to receive these time-based events.

Which of the following are valid ways to create non persistent timers?

Nonpersistent programmatic timers are created by calling TimerConfig. setPersistent(false) and passing the TimerConfig object to one of the timer-creation methods.


2 Answers

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");     }  } 
like image 68
victor herrera Avatar answered Sep 19 '22 05:09

victor herrera


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)
like image 39
Pascal Thivent Avatar answered Sep 22 '22 05:09

Pascal Thivent