spring task

来源:互联网 发布:读心术的书籍排名知乎 编辑:程序博客网 时间:2024/05/29 15:26
spring task 大概就如下代码, 代码我没贴全,你自己参考吧,该用到的都在下面.可以完成你的功能 
Java代码  收藏代码
  1. /** logger日志. */  
  2.    public static final Logger LOGGER = Logger.getLogger(GTaskAssembler.class);  
  3.    private static final Map<String, GTask> TASKS = new HashMap<String, GTask>(12);  
  4.    private static final Map<String, ScheduledFuture<?>> SCHEDULED_FUTURE = new HashMap<String, ScheduledFuture<?>>(16);  
  5.    private final static int POOL_SIZE = 64;  
  6.      
  7.    private final ConcurrentTaskScheduler ct = new ConcurrentTaskScheduler(Executors.newScheduledThreadPool(POOL_SIZE));  
  8.      
  9.    /** 
  10.     * 启动一个计划任务. 
  11.     * @param task 当前进行的任务. 
  12.     */  
  13.    public void start(GTask task) {  
  14.        try {  
  15.            if (StringUtils.isEmpty(task.getTaskId())) {  
  16.                throw new GTaskException("the taskid must be not empty.");  
  17.            }  
  18.   
  19.            if (StringUtils.isEmpty(task.getTrigger())) {  
  20.                throw new GTaskException("任务的调度表达式不能为空.");  
  21.            }  
  22.   
  23.            ScheduledFuture<?> scheduledFuture = ct.schedule(task, new CronTrigger(task.getTrigger()));  
  24.            SCHEDULED_FUTURE.put(task.getTaskId(), scheduledFuture);  
  25.            TASKS.put(task.getTaskId(), task);  
  26.            LOGGER.info("the task with " + task.getTaskId() + "has bean already started.");  
  27.        } catch (Exception e) {  
  28.            LOGGER.info(null, e);  
  29.            throw new GTaskException(e);  
  30.        }  
  31.    }  
  32.   
  33.    /** 
  34.     * 停止一个计划任务. 
  35.     * @param taskId 任务编号. 
  36.     */  
  37.    public void stop(String taskId) {  
  38.        LOGGER.info("正在停止任务 " + taskId);  
  39.        if (StringUtils.isEmpty(taskId)) {  
  40.            throw new GTaskException("the taskid must be not empty.");  
  41.        }  
  42.   
  43.        try {  
  44.            ScheduledFuture<?> scheduledFuture = SCHEDULED_FUTURE.remove(taskId);  
  45.            if (scheduledFuture == null) {  
  46.                throw new GTaskException("the task with id " + taskId + " is not exists.");  
  47.            } else {  
  48.                if (!scheduledFuture.isCancelled()) {  
  49.                    /** false 表示当前任务若正在执行,则待其执行结束,再结束此任务. */  
  50.                    scheduledFuture.cancel(false);  
  51.                }  
  52.            }  
  53.        } catch (Exception e) {  
  54.            LOGGER.info(null, e);  
  55.            throw new GTaskException(e);  
  56.        }  
  57.    }  
  58.   
  59.    /** 
  60.     * 重新设置当前任务的执行频率. 
  61.     *  
  62.     * @param taskId 
  63.     *            任务编号. 
  64.     */  
  65.    public void resetTrigger(String taskId, String cronExpression) {  
  66.        LOGGER.info("正在修改当前任务 " + taskId + "执行频率.");  
  67.        if (StringUtils.isEmpty(taskId)) {  
  68.            throw new GTaskException("the taskid must be not empty.");  
  69.        }  
  70.   
  71.        if (StringUtils.isEmpty(cronExpression)) {  
  72.            throw new GTaskException("任务的调度表达式不能为空.");  
  73.        }  
  74.   
  75.        GTask task = TASKS.get(taskId);  
  76.        if (task != null) {  
  77.            if (cronExpression.equals(task.getTrigger())) {  
  78.                return;  
  79.            }  
  80.   
  81.            /** first, stop the task. */  
  82.            ScheduledFuture<?> scheduledFuture = SCHEDULED_FUTURE.remove(taskId);  
  83.            scheduledFuture.cancel(false);  
  84.   
  85.            /** second, reset the task with cronExpression. */  
  86.            task.setTrigger(cronExpression);  
  87.            /** third, restart the task. */  
  88.            scheduledFuture = ct.schedule(task, new CronTrigger(cronExpression));  
  89.            SCHEDULED_FUTURE.put(taskId, scheduledFuture);  
  90.        }  
  91.    }  
  92.   
  93.    /** 
  94.     * 仅执行一次. 
  95.     *  
  96.     * @param task 所要执行任务. 
  97.     */  
  98.    public void onlyOneTime(GTask task) {  
  99.        if (StringUtils.isEmpty(task.getTaskId())) {  
  100.            throw new GTaskException("the taskid must be not empty.");  
  101.        }  
  102.   
  103.        ct.execute(task, 2000);  
  104.    }  
  105.   
  106.    /** 
  107.     * 销毁线程池中的任务. 
  108.     */  
  109.    public void destrory() {  
  110.        LOGGER.info("正在终止自动任务的线程池资源.");  
  111.        ScheduledExecutorService scheduledExecutor = (ScheduledExecutorService) ct.getConcurrentExecutor();  
  112.        try {  
  113.            scheduledExecutor.shutdownNow();  
  114.        } catch (Exception e) {  
  115.            LOGGER.info("自动任务的线程池资源清理发生异常.", e);  
  116.        } finally {  
  117.            LOGGER.info("自动任务的线程池资源清理完成.");  
  118.        }  
  119.    }  
0 0