Enterprise Java Development@TOPIC@
Perform similar role of job schedulers
e.g., cron
Three types
Single Action Timer
Interval Timer
Calendar Timer
Timers are specific to an EJB
Each EJB may have one @Timeout method but many timers
import javax.ejb.Timeout;
@Timeout
public void execute(Timer timer) {
Timer can hold Serializable context information
public long sellProduct(String sellerId, AuctionItem item) throws ResourceNotFoundException {
...
timerService.createTimer(item.getEndDate(), new Long(item.getId()));
timerService.createSingleActionTimer(item.getEndDate(),
new TimerConfig(new Long(item.getId()), false));
If you need multiple @Timeout method behaviors - create multiple EJBs
Original EJB Timer Service part of EJB 2.1
EJB 2.x is pre-annotations, pre-JavaSE 5, interface-based
EJB 3.0 added annotation-based extensions
eliminated interface-based solution requirements
EJB 3.1 provided overhaul of the overall service
Added cron-like, calendar schedules
import javax.ejb.ScheduleExpression;
public void initTimers(ScheduleExpression schedule) {
...
}
Added declarative scheduling
import javax.ejb.Schedule;
import javax.ejb.Timeout;
@Timeout
@Schedule(second="*/10", minute ="*", hour="*", dayOfMonth="*", month="*", year="*", persistent=false)
public void execute(Timer timer) {
import javax.annotation.Resource;
import javax.ejb.TimerService;
@Resource
private TimerService timerService;
Starting point for all EJB Timer programmatic manipulation
Timer Creation
createCalendarTimer(ScheduleExpression schedule, ...)
createIntervalTimer(Date initialExpiration, long intervalDuration, ...)
createSingleActionTimer(Date expiration, ...)
Get Timers
getTimers() - get Timers associated with this EJB
getAllTimers() - get Timers associated with this module
EJB Timers are created as perisistent=true by default. This sounds reasonable until you begin refactoring your application and start seeing "EJB not found", etc. on follow-on redeploys or many more EJB Timers firing that you believe should be. For programmatic EJB Timers - always pass in the optional TimerConfig and set persistent to false. For the Schedule annotation, set the persistent attribute to false.
It is always desirable to be able to easily coldstart your application in development with a reboot of the application server or a redeploy of the application and not have to worry about artifacts of a previous implementation approach.
Call @Timeout method after a specified expiration
createSingleActionTimer(Date expiration, TimerConfig timerConfig)
createSingleActionTimer(long duration, TimerConfig timerConfig)
Call @Timeout method after a given point in time and then specified duration after that
createIntervalTimer(Date initialExpiration, long intervalDuration, TimerConfig timerConfig)
createIntervalTimer(long initialDuration, long intervalDuration, TimerConfig timerConfig)
Call @Timeout method based on the schedule expression
second
minute
hour
dayOfMonth
month
dayOfWeek
year
createCalendarTimer(ScheduleExpression schedule, TimerConfig timerConfig)