Search code examples
javatimerjbossejbwildfly

Why this simple scheduler does not work?


Am I doing anything wrong?

import java.util.Date;
import java.util.Timer;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.TimerService;

@Singleton
@LocalBean
@Startup
public class CIScheduler {

    @Resource
    private TimerService timerService;

    @PostConstruct
    public void init() {
        timerService.createTimer(1000, 5000, "myTimerInterval");
    }

    @Timeout
    public void execute(Timer timer) {
        System.out.println("Current Time : " + new Date());
    }
}

Stack: JDK 8, Wildfly 10

Error LOG:

09:31:31,781 WARN [org.jboss.as.ejb3] (EJB default - 9) WFLYEJB0161: Failed to reinstate timer 'config-api.config-api.CIScheduler' (id=f571b03a-efa 9-467d-a9de-b17d88bf54c7) from its persistent state 09:31:31,783 ERROR [org.jboss.as.ejb3] (EJB default - 9) WFLYEJB0022: Error duri ng retrying timeout for timer: [id=35f55f82-a982-43d8-bece-42cfc89debe8 timedObj ectId=config-api.config-api.CIScheduler auto-timer?:false persistent ?:true timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@1eef0fd init ialExpiration=Wed Jun 15 09:20:46 BRT 2016 intervalDuration(in milli sec)=5000 n extExpiration=Wed Jun 15 09:31:36 BRT 2016 timerState=RETRY_TIMEOUT info=myTimer Interval]: javax.ejb.EJBException: java.lang.IllegalArgumentException: ja va.lang.ClassCastException@1da01b2

Samples extracted from: https://examples.javacodegeeks.com/enterprise-java/ejb3/timer/ejb-timer-service-example/

UPDATE: I cleaned the data and tmp folder and the error log changed to:

09:49:42,178 INFO [org.jboss.as.ejb3] (EJB default - 2) WFLYEJB0021: Timer: [id =26b74afb-625d-4007-a786-1c0caa92a70b timedObjectId=configuracao-api.config-api.CIScheduler auto-timer?:false persistent?:true timerService=org.jboss.as.e jb3.timerservice.TimerServiceImpl@1b701e2 initialExpiration=Wed Jun 15 09:49:37 BRT 2016 intervalDuration(in milli sec)=5000 nextExpiration=Wed Jun 15 09:49:47 BRT 2016 timerState=IN_TIMEOUT info=myTimerInterval] will be retried 09:49:42,179 INFO [org.jboss.as.ejb3] (EJB default - 2) WFLYEJB0023: Retrying t imeout for timer: [id=26b74afb-625d-4007-a786-1c0caa92a70b timedObjectId=config-api.config-api.CIScheduler auto-timer?:false persistent?:true timerS ervice=org.jboss.as.ejb3.timerservice.TimerServiceImpl@1b701e2 initialExpiration =Wed Jun 15 09:49:37 BRT 2016 intervalDuration(in milli sec)=5000 nextExpiration =Wed Jun 15 09:49:47 BRT 2016 timerState=IN_TIMEOUT info=myTimerInterval] 09:49:42,181 ERROR [org.jboss.as.ejb3] (EJB default - 2) WFLYEJB0022: Error duri ng retrying timeout for timer: [id=26b74afb-625d-4007-a786-1c0caa92a70b timedObj ectId=config-api.config-api.CIScheduler auto-timer?:false persistent ?:true timerService=org.jboss.as.ejb3.timerservice.TimerServiceImpl@1b701e2 init ialExpiration=Wed Jun 15 09:49:37 BRT 2016 intervalDuration(in milli sec)=5000 n extExpiration=Wed Jun 15 09:49:47 BRT 2016 timerState=RETRY_TIMEOUT info=myTimer Interval]: javax.ejb.EJBException: java.lang.IllegalArgumentException: argument type mismatch at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInt erceptor.java:187) .......


Solution

  • The method annotated with @Timeout is only allowed to have an argument from type javax.ejb.Timer. You inadvertently imported Timer from the util package (java.util.Timer). If you fix the import, the timer should work like a charm :).