Enterprise Java Development@TOPIC@
@Timeout callback method annotated with @Schedule
@Timeout
@Schedule(second="*/10", minute ="*", hour="*",
dayOfMonth="*", month="*", year="*", persistent=false)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void execute(Timer timer) {
logger.info("timer fired: {}", timer);
try {
checkAuction();
}
catch (Exception ex) {
logger.error("error checking auction", ex);
}
}
Server-side output
23:08:30 [AuctionMgmtEJB] (EJB default-6) **** AuctionMgmtEJB init() ***
23:08:30 [AuctionMgmtEJB] (EJB default-6) timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@d72fc25
23:08:30 [AuctionMgmtEJB] (EJB default-6) checkAuctionInterval=10000
23:08:30 [AuctionMgmtEJB] (EJB default-6) timer fired: [id=02cbc8b5-0ddd-414e-b81d-59c7800371fd
timedObjectId=asyncMarketEAR.asyncMarketEJB.AuctionMgmtEJB auto-timer?:true persistent?:false
timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@d72fc25
initialExpiration=null intervalDuration(in milli sec)=0
nextExpiration=Tue Dec 04 23:08:40 EST 2018 timerState=IN_TIMEOUT info=null]
23:08:30 [AuctionMgmtEJB] (EJB default-6) checking auctions
23:08:30 [AuctionMgmtEJB] (EJB default-6) processed 0 active items
23:08:40 [AuctionMgmtEJB] (EJB default-7) **** AuctionMgmtEJB init() ***
23:08:40 [AuctionMgmtEJB] (EJB default-7) timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@d72fc25
23:08:40 [AuctionMgmtEJB] (EJB default-7) checkAuctionInterval=10000
23:08:40 [AuctionMgmtEJB] (EJB default-7) timer fired: [id=02cbc8b5-0ddd-414e-b81d-59c7800371fd
timedObjectId=asyncMarketEAR.asyncMarketEJB.AuctionMgmtEJB auto-timer?:true persistent?:false
timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@d72fc25
initialExpiration=null intervalDuration(in milli sec)=0
nextExpiration=Tue Dec 04 23:08:50 EST 2018 timerState=IN_TIMEOUT info=null]
23:08:40 [AuctionMgmtEJB] (EJB default-7) checking auctions
23:08:40 [AuctionMgmtEJB] (EJB default-7) processed 0 active items
23:08:50 [AuctionMgmtEJB] (EJB default-9) **** AuctionMgmtEJB init() ***
23:08:50 [AuctionMgmtEJB] (EJB default-9) timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@d72fc25
23:08:50 [AuctionMgmtEJB] (EJB default-9) checkAuctionInterval=10000
23:08:50 [AuctionMgmtEJB] (EJB default-9) timer fired: [id=02cbc8b5-0ddd-414e-b81d-59c7800371fd
timedObjectId=asyncMarketEAR.asyncMarketEJB.AuctionMgmtEJB auto-timer?:true persistent?:false
timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@d72fc25
initialExpiration=null intervalDuration(in milli sec)=0
nextExpiration=Tue Dec 04 23:09:00 EST 2018 timerState=IN_TIMEOUT info=null]
23:08:50 [AuctionMgmtEJB] (EJB default-9) checking auctions
23:08:50 [AuctionMgmtEJB] (EJB default-9) processed 0 active items
Client builds Calendar using ScheduleExpression
@EJB(beanInterface=AuctionMgmtLocal.class)
private AuctionMgmt auctionMgmt;
...
ScheduleExpression schedule = new ScheduleExpression();
schedule.second("*/10");
schedule.minute("*");
schedule.hour("*");
schedule.dayOfMonth("*");
schedule.month("*");
schedule.year("*");
auctionMgmt.initTimers(schedule);
Server-side EJB method to initialize EJB Timer
public void initTimers(ScheduleExpression schedule) {
cancelTimers();
logger.debug("initializing timers, schedule={}", schedule);
timerService.createCalendarTimer(schedule, new TimerConfig("checkAuctionTimer", false));
}
Initialize Timers Cancels all other Timers - including declarative Timers
public void cancelTimers() {
logger.debug("canceling timers");
for (Timer timer : (Collection<Timer>)timerService.getTimers()) {
timer.cancel();
}
}
Server-side EJB method called back
@Timeout
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void execute(Timer timer) {
logger.info("timer fired: {}", timer);
try {
checkAuction();
}
catch (Exception ex) {
logger.error("error checking auction", ex);
}
}
Server-side output
23:20:00 [AuctionMgmtEJB] (EJB default-8) **** AuctionMgmtEJB init() ***
23:20:00 [AuctionMgmtEJB] (EJB default-8) timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@3ae3cbd8
23:20:00 [AuctionMgmtEJB] (EJB default-8) checkAuctionInterval=10000
23:20:00 [AuctionMgmtEJB] (EJB default-8) timer fired: [id=a68ba008-1020-49a6-a755-68bbaac4fae3
timedObjectId=asyncMarketEAR.asyncMarketEJB.AuctionMgmtEJB auto-timer?:false persistent?:false
timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@3ae3cbd8
initialExpiration=null intervalDuration(in milli sec)=0
nextExpiration=Tue Dec 04 23:20:10 EST 2018 timerState=IN_TIMEOUT info=checkAuctionTimer]
23:20:00 [AuctionMgmtEJB] (EJB default-8) checking auctions
23:20:00 [AuctionMgmtEJB] (EJB default-8) processed 0 active items
23:20:10 [AuctionMgmtEJB] (EJB default-6) **** AuctionMgmtEJB init() ***
23:20:10 [AuctionMgmtEJB] (EJB default-6) timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@3ae3cbd8
23:20:10 [AuctionMgmtEJB] (EJB default-6) checkAuctionInterval=10000
23:20:10 [AuctionMgmtEJB] (EJB default-6) timer fired: [id=a68ba008-1020-49a6-a755-68bbaac4fae3
timedObjectId=asyncMarketEAR.asyncMarketEJB.AuctionMgmtEJB auto-timer?:false persistent?:false
timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@3ae3cbd8
initialExpiration=null intervalDuration(in milli sec)=0
nextExpiration=Tue Dec 04 23:20:20 EST 2018 timerState=IN_TIMEOUT info=checkAuctionTimer]
23:20:10 [AuctionMgmtEJB] (EJB default-6) checking auctions
23:20:10 [AuctionMgmtEJB] (EJB default-6) processed 0 active items
23:20:20 [AuctionMgmtEJB] (EJB default-7) **** AuctionMgmtEJB init() ***
23:20:20 [AuctionMgmtEJB] (EJB default-7) timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@3ae3cbd8
23:20:20 [AuctionMgmtEJB] (EJB default-7) checkAuctionInterval=10000
23:20:20 [AuctionMgmtEJB] (EJB default-7) timer fired: [id=a68ba008-1020-49a6-a755-68bbaac4fae3
timedObjectId=asyncMarketEAR.asyncMarketEJB.AuctionMgmtEJB auto-timer?:false persistent?:false
timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@3ae3cbd8 initialExpiration=null
intervalDuration(in milli sec)=0 nextExpiration=Tue Dec 04 23:20:30 EST 2018 timerState=IN_TIMEOUT info=checkAuctionTimer]
23:20:20 [AuctionMgmtEJB] (EJB default-7) checking auctions
23:20:20 [AuctionMgmtEJB] (EJB default-7) processed 0 active items