2 Replies Latest reply on May 24, 2007 11:22 PM by Gavin King

    Asynchronous programming pattern

    Simon Tian Newbie

      Whenever we make an asynchronous method call, a thread will be dispatched. It works well so far but we encountered serveral asynchronous call requirements such as doing calculation when rolling over a day, sending reminder message for a payment due, expiring a registration for certain condition. A scheduled task might be cancelled in the middle, say, a payment made after the reminder scheduled to send but before actually sending it.

      Firstly we just simple make a asynchronous call when needed, but it turns out there are too many threads running. We worry about the performance and resources even though we haven't experienced them yet. We tried to make only one asynchronous call with interval at every 5 minutes. The other scheduled tasks are registered with it, and the tasks will mature within 5 minutes will be dispatched with a one-off asynchronous call by this thread. The problem here is that the one-off asynchronous calls run immidiately and the expiration or duration totally get ignored. Thus the scheduled time might have discrepancy in 5 minutes.

      Does Seam support to make an asynchronous call from another asynchronous call? Or has anybody got another programming pattern to fulfill such kind of requirements. In case I incorrently programmed the asynchronous feature, I pasted the code below:

      //Making an asynchronous call to look after the other scheduled tasks
      
      @Name("scheduleDispatcherAction")
      @Scope(ScopeType.APPLICATION)
      @Synchronized
      @Startup
      public class ScheduleDispatcherAction implements ScheduleDispatcher, Serializable {
      
       @In(create = true)
       private ScheduleService scheduleServiceAction;
      
       @Create
       public void startService() {
       ...
       scheduleServiceAction.dispatchTasks(cal.getTime(), ScheduleService.INTERVAL_MILLIS);
      
       cal.add(Calendar.MINUTE, 1);
       ScheduledTask task = new ScheduledTask(new Long(1), cal.getTime(), 45000, "miscAction", "test1", null);
       ScheduleServiceAction.addTask(task);
       cal.add(Calendar.MINUTE, 1);
       task = new ScheduledTask(new Long(2), cal.getTime(), 0, "miscAction", "test2", null);
       ScheduleServiceAction.addTask(task);
       }
      


      //The method to dispatch the scheduled tasks
      
      @Name("scheduleServiceAction")
      @Scope(ScopeType.APPLICATION)
      @Synchronized
      public class ScheduleServiceAction implements ScheduleService, Serializable {
      
       @Asynchronous
       public void dispatchTasks(@Expiration Date startDate, @IntervalDuration long interval) {
       Iterator<ScheduledTask> iter = taskPool.iterator();
       while (iter.hasNext()) {
       ScheduledTask aTask = iter.next();
       long dueMillis = aTask.getStartDate().getTime() - System.currentTimeMillis();
       if (dueMillis > ScheduleService.INTERVAL_MILLIS) {
       break;
       } else if (dueMillis < 0) {
       dueMillis = 0;
       }
      
       Object action = Component.getInstance(aTask.getActionName(), ScopeType.APPLICATION);
       Method method = action.getClass().getMethod(aTask.getMethodName(), new Class[]{long.class, Object[].class});
       method.invoke(action, dueMillis, aTask.getParameters());
       // It's the same result if not using reflection here.
       ...
      


      //Task code
      
      @Name("miscAction")
      @Scope(ScopeType.APPLICATION)
      @Synchronized
      public class MiscAction implements MiscService, Serializable {
      
       @Asynchronous
       public void test1(@Duration long duration, Object[] paras) {;
       System.out.println("test1 started at " + (new Date()));
       }
      
       @Asynchronous
       public void test2(@Duration long duration, Object[] paras) {
       System.out.println("test2 started at " + (new Date()));
       }