1 Reply Latest reply on Feb 12, 2015 12:32 PM by Arnold Maderthaner

    Problem with timer service

    Arnold Maderthaner Newbie

      Hi !

       

      I tried to use the timer service in wildfly 8.1.0 final but I get some unexpected results.

      Here my sample code:

       

      package my.class;

       

      import java.text.SimpleDateFormat;

      import java.util.Date;

       

      import javax.annotation.PostConstruct;

      import javax.annotation.PreDestroy;

      import javax.annotation.Resource;

      import javax.ejb.ScheduleExpression;

      import javax.ejb.Singleton;

      import javax.ejb.Startup;

      import javax.ejb.Timeout;

      import javax.ejb.Timer;

      import javax.ejb.TimerConfig;

      import javax.ejb.TimerService;

      import javax.interceptor.InvocationContext;

       

      import org.slf4j.Logger;

      import org.slf4j.LoggerFactory;

       

      @Singleton

      @Startup

      public class MyTimerBean {

        public static Logger log = LoggerFactory.getLogger(MyTimerBean.class);

       

        @Resource

        private TimerService timerService;

       

        @Timeout

        public void scheduler(Timer timer) {

        Date currentTime = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(

        "yyyy.MM.dd G 'at' HH:mm:ss z");

        log.info("TimeoutExample.scheduler() " + timer.getInfo()

        + simpleDateFormat.format(currentTime));

        }

       

        @PostConstruct

        public void initialize(InvocationContext ctx) {

        ScheduleExpression se = new ScheduleExpression();

        // Set schedule to every 3 seconds (starting at second 0 of every

        // minute).

        se.hour("*").minute("*").second("0/3");

        timerService.createCalendarTimer(se, new TimerConfig(

        "EJB timer service timeout at ", false));

        }

       

        @PreDestroy

        public void stop() {

        log.info("EJB Timer: Stop timers.");

        for (Timer timer : timerService.getTimers()) {

        System.out.println("Stopping timer: " + timer.getInfo());

        timer.cancel();

        }

        }

      }

       

      The issue is that the timer is called way too often (and sometimes I get as TimerConfig.getInfo() = null).

      Here the log output that I get:

       

      2015-02-12 14:00:18,008 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:18 UTC
      2015-02-12 14:00:18,015 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:18 UTC
      2015-02-12 14:00:18,791 INFO  my.class.MyTimerBean TimeoutExample.scheduler() info2015.02.12 AD at 14:00:18 UTC
      2015-02-12 14:00:18,796 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:18 UTC
      2015-02-12 14:00:18,802 INFO  my.class.MyTimerBean TimeoutExample.scheduler() info2015.02.12 AD at 14:00:18 UTC
      2015-02-12 14:00:18,807 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:18 UTC
      2015-02-12 14:00:19,007 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:19 UTC
      2015-02-12 14:00:19,014 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:19 UTC
      2015-02-12 14:00:20,010 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:20 UTC
      2015-02-12 14:00:20,015 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:20 UTC
      2015-02-12 14:00:21,003 INFO  my.class.MyTimerBean TimeoutExample.scheduler() EJB timer service timeout at 2015.02.12 AD at 14:00:21 UTC
      2015-02-12 14:00:21,011 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:21 UTC
      2015-02-12 14:00:21,017 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:21 UTC
      2015-02-12 14:00:22,008 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:22 UTC
      2015-02-12 14:00:22,015 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:22 UTC
      2015-02-12 14:00:23,009 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:23 UTC
      2015-02-12 14:00:23,016 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:23 UTC
      2015-02-12 14:00:24,006 INFO  my.class.MyTimerBean TimeoutExample.scheduler() EJB timer service timeout at 2015.02.12 AD at 14:00:24 UTC
      2015-02-12 14:00:24,012 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:24 UTC
      2015-02-12 14:00:24,018 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:24 UTC
      2015-02-12 14:00:25,010 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:25 UTC
      2015-02-12 14:00:25,016 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:25 UTC
      2015-02-12 14:00:26,008 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:26 UTC
      2015-02-12 14:00:26,014 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:26 UTC
      2015-02-12 14:00:27,002 INFO  my.class.MyTimerBean TimeoutExample.scheduler() EJB timer service timeout at 2015.02.12 AD at 14:00:27 UTC
      2015-02-12 14:00:27,009 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:27 UTC
      2015-02-12 14:00:27,015 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:27 UTC
      2015-02-12 14:00:28,008 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:28 UTC
      2015-02-12 14:00:28,013 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:28 UTC
      2015-02-12 14:00:28,794 INFO  my.class.MyTimerBean TimeoutExample.scheduler() info2015.02.12 AD at 14:00:28 UTC
      2015-02-12 14:00:28,799 INFO  my.class.MyTimerBean TimeoutExample.scheduler() info2015.02.12 AD at 14:00:28 UTC
      2015-02-12 14:00:28,805 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:28 UTC
      2015-02-12 14:00:28,810 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:28 UTC
      2015-02-12 14:00:29,008 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:29 UTC
      2015-02-12 14:00:29,014 INFO  my.class.MyTimerBean TimeoutExample.scheduler() null2015.02.12 AD at 14:00:29 UTC

       

      anyone can help on this issue ?