3 Replies Latest reply on Nov 25, 2014 9:23 AM by Marcel Šebek

    ManagedScheduledExecutorService unintuitive behavior

    Marcel Šebek Newbie

      Hi.

       

      I'm trying to use ManagedScheduledExecutorService in my application, and I'm observing strange things.

      It is very difficult to get rid of scheduled tasks on undeploy. If I do nothing, my tasks survive. If I call

      ScheduledFuture.cancel(), it cancels single invocation at best, but Trigger is called again to schedule a

      new invocation.

       

      I have two questions:

       

      1. Am I supposed to cancel all timers scheduled using MSES?
      2. Am I supposed to instruct all my Trigger instances to return null on further invocations of getNextRunTime()?

       

      If I do both things, I'm able to destroy my timers, but it is both unintuitive and not well documented (at least in javadoc

      I haven't found anything about it, I don't think I should read detailed specification to be able to use such an api correctly).

       

      Here is a sample code that doesn't do what one would expect:

      @Singleton
      @Startup
      public class TestBean {
          @Resource
          private ManagedScheduledExecutorService mses;
      
          private ScheduledFuture timer;
      
          @PostConstruct
          public void init() {
              timer = mses.schedule(() -> { System.out.println("timer"); }, new Trigger() {
      
                  @Override
                  public Date getNextRunTime(LastExecution le, Date date) {
                      return new Date(new Date().getTime() + 5000);
                  }
      
                  @Override
                  public boolean skipRun(LastExecution le, Date date) {
                      return false;
                  }
              });
          }
      
          @PreDestroy
          public void destroy() {
              System.out.println("destroy called");
              timer.cancel(true);
          }
      }