Calendar-based EJB timer expressions

The example in the previous section has one disadvantage: the startTimer() method in the session bean must be invoked from a client in order to start the timer. This restriction makes it difficult to have the timer start as soon as the bean is deployed.

Java EE 6 introduced calendar-based EJB timer expressions. Calendar-based expressions allow one or more methods in our session beans to be executed at a certain date and time. For example, we could configure one of our methods to be executed every night at 8:10 pm, which is exactly what our next example does:

package com.ensode.javaeebook.calendarbasedtimer; 
 
import java.util.logging.Logger; 
import javax.ejb.Stateless; 
import javax.ejb.LocalBean; 
import javax.ejb.Schedule; 
 
@Stateless 
@LocalBean 
public class CalendarBasedTimerEjbExampleBean { 
 
  private static Logger logger = Logger.getLogger( 
      CalendarBasedTimerEjbExampleBean.class.getName()); 
 
  @Schedule(hour = "20", minute = "10") 
  public void logMessage() { 
    logger.info("This message was triggered at:" 
        + System.currentTimeMillis()); 
  } 
} 

As we can see in the preceding example, we set up the time when the method will be executed via the javax.ejb.Schedule annotation. In this particular example, we set up our method to be executed at 8:10 pm by setting the hour attribute of the @Schedule annotation to "20", and its minute attribute to "10" (since the value of the hour attribute is 24 hour-based, hour 20 is equivalent to 8:00 pm).

The @Schedule annotation has several other attributes that allow a lot of flexibility in specifying when the method should be executed. We could, for instance, have a method executed on the third Friday of every month, or on the last day of the month, and so on and so forth.

The following table lists all the attributes in the @Schedule annotation that allow us to control when the annotated method will be executed:

Attribute

Description

Example values

Default value

dayOfMonth

The day of the month

"3": The third day of the month
"Last": The last day of the month

"-2": Two days before the end of the month

"1st Tue": The first Tuesday of the month

"*"

dayOfWeek

The day of the week

"3": Every Wednesday

"Thu": Every Thursday

"*"

hour

Hour of the day (24 hour-based)

"14": 2:00 pm

"0"

minute

Minute of the hour

"10": Ten minutes after the hour

"0"

month

Month of the year

"2": February

"March": March

"*"

second

Second of the minute

"5": Five seconds after the minute

"0"

timezone

Timezone ID

"America/New York"

""

year

Four digit year

"2010"

"*"

In addition to single values, most attributes accept the asterisk (*) as a wild card, meaning that the annotated method will be executed every unit of time (every day, hour, and so on).

Additionally, we can specify more than one value by separating the values with commas; for example, if we needed a method to be executed every Tuesday and Thursday, we could annotate the method as @Schedule(dayOfWeek="Tue, Thu").

We can also specify a range of values, with the first value and last value separated by a dash (-); to execute a method from Monday through Friday, we could use @Schedule(dayOfWeek="Mon-Fri").

Additionally, we could specify that we need the method to be executed every n units of time (for example, every day, every 2 hours, every 10 minutes, and so on). To do something like this, we could use @Schedule(hour="*/12"), which would execute the method every 12 hours.

As we can see, the @Schedule annotation provides a lot of flexibility when it comes to specifying when we need our methods executed. Plus, it provides the advantage of not needing a client call to activate the scheduling. Additionally, it has the advantage of using a cron-like syntax; therefore, developers familiar with this Unix tool will feel right at home using this annotation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.85.221