title: Schedule a task to run only once
body: <p>I'm working on a recovery monitor which waits for 5 minutes and fires an alert if system has not been recovered yet. The monitor needs to be started at start up and to fire alert only once. The source code looks like this:</p>
  
  <pre><code>@Stateless
  public class RecoveryMonitor {
      @Inject TimerService timerService;
      @Inject MyAlertService alertService;
      @Inject SystemRecovery systemRecovery;
  
      public void scheduleMonitor() {
          timerService.createSingleActionTimer(TimeUnit.MINUTES.toMillis(5),
              new TimerConfig);
      }
  
      @Timeout
      public void timeout() {
          if (!systemRecovery.isDone) {
              alertService.alert("System recovery failed");
          }
      }
  }
  </code></pre>
  
  <p>So, the problem here is how to schedule a task, i.e. invoke scheduleMonitor method. I cannot use @PostConstruct as it's not allowed to. I think about using @Schedule, but it executes a method periodically while I only to do it once. Any solutions and/or suggestions are welcome. Thanks.</p>
  
  <p>L</p>
  
  <p><strong>UPDATE</strong>: by making the class not Stateless anymore, e.g. make it a @Singleton, I am able to start scheduling using @PostConstruct. This is not a complete solution but it works for me:</p>
  
  <pre><code>@Singleton
  public class RecoveryMonitor {
      @Inject TimerService timerService;
      @Inject MyAlertService alertService;
      @Inject SystemRecovery systemRecovery;
  
      @PostConstruct
      public void scheduleMonitor() {
          timerService.createSingleActionTimer(TimeUnit.MINUTES.toMillis(5),
              new TimerConfig);
      }
  
      @Timeout
      public void timeout() {
          if (!systemRecovery.isDone) {
              alertService.alert("System recovery failed");
          }
      }
  }
  </code></pre>