1 Reply Latest reply on Oct 27, 2008 1:43 PM by Gabriel Goïc

    Repeating asynchronous tasks fail silently

    Phil Haigh Novice

      Hi,


      I have been experiencing some issues with repeating asychronous tasks silently ceasing to repeat. Essentially it seems that if you have a method annotated Asynchronous and with parameters doStuff(@Duration Long waitBeforeStart, @IntervalDuration Long interval), if the method throws an uncaught exception, no further invocations of the method occur.


      I have created a simple test case.



      @In (required=false,create=true)
      private MaintenanceTasksI maintenanceTasks;
      
      @Observer("org.jboss.seam.postInitialization")
      public void scheduleRecurringTask()
      {
           maintenanceTasks.doStuff(5000L,2000L);
      }




      @Local
      public interface MaintenanceTasksI
      {
               @Destroy @Remove
               public abstract void destroy();
          
           @Asynchronous
           public void doStuff(@Duration Long waitBeforeStart, @IntervalDuration Long interval);
      }
      




      @Stateful
      @Name("maintenanceTasks")
      public class MaintenanceTasks implements MaintenanceTasksI
      {
          @Destroy @Remove
          public void destroy() {}
          
           @Logger
           private Log log;
           
           public void doStuff(@Duration Long waitBeforeStart, @IntervalDuration Long interval)
           {          
                int x = new Random().nextInt(10);
                log.info(x);
                if (x>8)
                {
                     throw new NullPointerException();
                }
           }
      }
      



      The output from which is below:



      2008-10-23 15:03:04,130 [INFO  MaintenanceTasks] 6
      2008-10-23 15:03:06,119 [INFO  MaintenanceTasks] 5
      2008-10-23 15:03:08,119 [INFO  MaintenanceTasks] 2
      2008-10-23 15:03:10,118 [INFO  MaintenanceTasks] 8
      2008-10-23 15:03:12,117 [INFO  MaintenanceTasks] 2
      2008-10-23 15:03:14,117 [INFO  MaintenanceTasks] 9
      



      As you can see, the task happily repeats from startup every 2 seconds, until it encounters a random number higher than 8, in which case the forced NPE causes the task to stop executing, and no further instances invoked. Nor is the NPE logged by the container.


      If this is expected behaviour, what is the best design pattern to defend against this happening?


      cheers


      phil