java 调度封装

来源:互联网 发布:网络部署架构图 编辑:程序博客网 时间:2024/05/29 16:14

http://blog.csdn.net/gaojava/article/details/8608348

[java] view plaincopy
  1. public class SchedulerService extends Service {  
  2.     private static final int DEFAULT_POOL_SIZE = 2 * Runtime.getRuntime().availableProcessors();  
  3.   
  4.     private int poolSize;  
  5.     private ScheduledExecutorService pool;  
  6.   
  7.     public int getPoolSize() {  
  8.         return poolSize;  
  9.     }  
  10.   
  11.     public void setPoolSize(int poolSize) {  
  12.         if (poolSize < 0) {  
  13.             logger.warning("pool size error,use default value:" + DEFAULT_POOL_SIZE);  
  14.             poolSize = DEFAULT_POOL_SIZE;  
  15.         }  
  16.         this.poolSize = poolSize;  
  17.     }  
  18.   
  19.     protected void initService() throws ServiceException {  
  20.         pool = Executors.newScheduledThreadPool(poolSize);  
  21.     }  
  22.   
  23.     protected void startService() throws ServiceException {  
  24.         //  
  25.     }  
  26.   
  27.     protected void stopService() throws ServiceException {  
  28.         pool.shutdown();  
  29.         try {  
  30.             if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {  
  31.                 pool.shutdownNow();  
  32.                 if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {  
  33.                     logger.severe("Pool did not terminate");  
  34.                 }  
  35.             }  
  36.         } catch (InterruptedException e) {  
  37.             pool.shutdownNow();  
  38.             Thread.currentThread().interrupt();  
  39.         }  
  40.     }  
  41.   
  42.     ScheduledExecutorService getPool() {  
  43.         return pool;  
  44.     }  
  45.   
  46.     public Scheduler scheduleWithFixedDelay(Runnable task, long delay, long period, TimeUnit util) {  
  47.         Scheduler scheduler = new Scheduler();  
  48.         scheduler.setDelay(delay);  
  49.         scheduler.setPeriod(period);  
  50.         scheduler.setWorker(task);  
  51.         scheduler.setService(this);  
  52.         return scheduler;  
  53.     }  
  54.   
  55.     public Scheduler schedule(Runnable task, long delay, TimeUnit util) {  
  56.         Scheduler scheduler = new Scheduler();  
  57.         scheduler.setDelay(delay);  
  58.         scheduler.setPeriod(-1);  
  59.         scheduler.setWorker(task);  
  60.         scheduler.setService(this);  
  61.         return scheduler;  
  62.     }  
  63. }  




[java] view plaincopy
  1. public class Scheduler {  
  2.     private SchedulerService service;  
  3.     private Runnable worker;  
  4.     private long period = -1;  
  5.     private long delay = 0;  
  6.     private ScheduledFuture<?> task;  
  7.   
  8.     void setService(SchedulerService service) {  
  9.         this.service = service;  
  10.     }  
  11.   
  12.     void setWorker(Runnable worker) {  
  13.         this.worker = worker;  
  14.     }  
  15.   
  16.     void setPeriod(long period) {  
  17.         this.period = period;  
  18.     }  
  19.   
  20.     void setDelay(long delay) {  
  21.         this.delay = delay;  
  22.     }  
  23.   
  24.     public SchedulerService getService() {  
  25.         return service;  
  26.     }  
  27.   
  28.     public Runnable getWorker() {  
  29.         return worker;  
  30.     }  
  31.   
  32.     public ScheduledFuture<?> getTask() {  
  33.         return task;  
  34.     }  
  35.   
  36.     public void cancel() {  
  37.         if (this.task != null  
  38.                 && (!this.task.isCancelled() || !this.task.isDone())) {  
  39.             this.task.cancel(true);  
  40.         }  
  41.     }  
  42.   
  43.     public long getDelay() {  
  44.         return delay;  
  45.     }  
  46.   
  47.     public long getPeriod() {  
  48.         return period;  
  49.     }  
  50.   
  51.     public boolean isRunning() {  
  52.         return task != null && !task.isCancelled() && !task.isDone();  
  53.     }  
  54.   
  55.     public synchronized void start() {  
  56.         if (!isRunning()) {  
  57.             if (service != null && worker != null) {  
  58.                 if (this.period > 0) {  
  59.                     task = service.getPool().scheduleWithFixedDelay(worker,  
  60.                             this.delay, this.period, TimeUnit.MILLISECONDS);  
  61.                 } else {  
  62.                     task = service.getPool().schedule(worker, this.delay,  
  63.                             TimeUnit.MILLISECONDS);  
  64.                 }  
  65.             }  
  66.         }  
  67.     }  
  68. }  


[java] view plaincopy
  1. private Scheduler addSchedulerJob(final ITimeTask task,long delay ,final IPluginActivator activator) {  
  2.     Scheduler timer = schedulerService.scheduleWithFixedDelay(new Runnable() {  
  3.         public void run() {  
  4.             try {  
  5.                 if (activator.getState() == IPluginActivator.RUNNING) {  
  6.                     task.excute();  
  7.                 }  
  8.             } catch (Throwable e) {  
  9.                 logger.log(Level.SEVERE, "", e);  
  10.             }  
  11.         }  
  12.     }, 0, delay, TimeUnit.MILLISECONDS);  
  13.     return timer;  
  14. }  

原创粉丝点击