Thursday, March 22, 2007

 

Worker Object Creation Pattern

這是拿AspectJ in Action8.1 The worker object creation pattern中程式碼修改過的進化版本
我加了Annotation與Java 5新的Concurrent API



public @interface AsynchronousExecution {
}
=============================================================================
public class CachePreFetcher {
   @AsynchronousExecution
   static void fetch() {
      System.out.println("Fetching in thread " + Thread.currentThread());
   }

   @AsynchronousExecution
   static String fetchValue() {
      System.out
            .println("Fetching Value in thread " + Thread.currentThread());
      return "fetchValue";
   }
}
=============================================================================
public class ProjectSaver {
   @AsynchronousExecution
   static void backupSave() {
      System.out.println("Saving backup copy in thread "
            + Thread.currentThread());
   }
}
=============================================================================
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public abstract aspect AsynchronousExecutionAspect {
   public abstract pointcut asyncOperations();

   public abstract pointcut asyncValueOperations();

   private static final ExecutorService exec = Executors.newCachedThreadPool();

   public static void shutDownExecutorService() {
      try {
         exec.awaitTermination(10, TimeUnit.MILLISECONDS);
      } catch (InterruptedException e) {
         System.err.println(e);
      }
      exec.shutdown();
   }

   void around(): asyncOperations(){
      Runnable worker = new Runnable() {
         public void run() {
            proceed();
         }
      };
      exec.execute(worker);
   }

   Object around() : asyncValueOperations(){
      Callable<Object> worker = new Callable<Object>() {
         public Object call() {
            return proceed();
         }
      };
      Future<Object> future = exec.submit(worker);
      Object result = null;
      try {
         result = future.get();
      } catch (Exception e) {
         System.err.println(e);
      }
      return result;
   }
}
=============================================================================
public aspect SystemAsynchronousExecutionAspect extends
      AsynchronousExecutionAspect {

   public pointcut asyncOperations():
      call(@AsynchronousExecution void *.*(..));

   public pointcut asyncValueOperations():
      call(@AsynchronousExecution * *.*(..));

}
=============================================================================
public class Test {
   public static void main(String[] args) {
      CachePreFetcher.fetch();
      String value = CachePreFetcher.fetchValue();
      System.out.println("CachePreFetcher.fetchValue() returns " + value);
      ProjectSaver.backupSave();

      AsynchronousExecutionAspect.shutDownExecutorService();
   }
}

Labels: , , ,


Comments: Post a Comment



<< Home