计划任务服务(windows 7旗舰版电脑计划任务服务的设置方法)

时刻小站 174

分布式调度任务服务概述

Redisson的分布式调度任务服务实现了java.util.concurrent.ScheduledExecutorService接口,支持在不同的独立节点里执行基于java.util.concurrent.Callable接口或java.lang.Runnable接口的任务。Redisson独立节点按顺序运行Redis列队里的任务。调度任务是一种需要在未来某个指定时间运行一次或多次的特殊任务。

设定任务计划

Redisson独立节点不要求任务的类在类路径里。他们会自动被Redisson独立节点的ClassLoader加载。因此每次执行一个新任务时,不需要重启Redisson独立节点。

采用Callable任务的范例:

publicclassCallableTaskimplementsCallable<Long>{@RInjectprivateRedissonClient redissonClient;@OverridepublicLongcall()throwsException{ RMap map = redissonClient.getMap("myMap"); Long result =0;for(Integer value : map.values()) { result += value; }returnresult; } }

在创建ExecutorService时可以配置以下参数:

ExecutorOptions options = ExecutorOptions.defaults()// 指定重新尝试执行任务的时间间隔。// ExecutorService的工作节点将等待10分钟后重新尝试执行任务//// 设定为0则不进行重试//// 默认值为5分钟options.taskRetryInterval(10, TimeUnit.MINUTES);RScheduledExecutorService executorService = redisson.getExecutorService("myExecutor"); ScheduledFuturefuture= executorService.schedule(newCallableTask(),10, TimeUnit.MINUTES); Long result =future.get();

使用Lambda任务的范例:

RExecutorService executorService = redisson.getExecutorService("myExecutor", options); ScheduledFuture future = executorService.schedule((Callable & Serializable)()->{ System.out.println("task has been executed!"); },10, TimeUnit.MINUTES); Long result = future.get();

采用Runnable任务的范例:

publicclassRunnableTaskimplementsRunnable{@RInjectprivateRedissonClient redissonClient;privatelongparam;publicRunnableTask(){ }publicRunnableTask(longparam){this.param= param; }@Overridepublicvoidrun(){ RAtomicLong atomic = redissonClient.getAtomicLong("myAtomic"); atomic.addAndGet(param); } } RScheduledExecutorService executorService = redisson.getExecutorService("myExecutor"); ScheduledFuture future1 = executorService.schedule(newRunnableTask(123),10, TimeUnit.HOURS);// ...ScheduledFuture future2 = executorService.scheduleAtFixedRate(newRunnableTask(123),10,25, TimeUnit.HOURS);// ...ScheduledFuture future3 = executorService.scheduleWithFixedDelay(newRunnableTask(123),5,10, TimeUnit.HOURS);

通过CRON表达式设定任务计划

在分布式调度任务中,可以通过CRON表达式来为任务设定一个更复杂的计划。表达式与Quartz的CRON格式完全兼容。

例如:

RScheduledExecutorService executorService = redisson.getExecutorService("myExecutor"); executorService.schedule(newRunnableTask(), CronSchedule.of("10 0/5 * * * ?"));// ...executorService.schedule(newRunnableTask(), CronSchedule.dailyAtHourAndMinute(10,5));// ...executorService.schedule(newRunnableTask(), CronSchedule.weeklyOnDayAndHourAndMinute(12,4, Calendar.MONDAY, Calendar.FRIDAY));

取消计划任务

分布式调度任务服务提供了两张取消任务的方式:通过调用ScheduledFuture.cancel()方法或调用RScheduledExecutorService.cancelScheduledTask方法。通过对Thread.currentThread().isInterrupted()方法的调用可以在已经处于运行状态的任务里实现任务中断:

publicclassRunnableTaskimplementsCallable {@RInjectprivateRedissonClient redissonClient; @OverridepublicLongcall()throws Exception{ RMapmap= redissonClient.getMap("myMap"); Long result =0;// map里包含了许多的元素for(Integer value :map.values()) {if(Thread.currentThread().isInterrupted()) {// 任务被取消了returnnull; } result += value; }returnresult; } } RScheduledExecutorService executorService = redisson.getExecutorService("myExecutor"); RScheduledFuturefuture= executorService.scheduleAsync(newRunnableTask(), CronSchedule.dailyAtHourAndMinute(10,5));// ...future.cancel(true);// 或String taskId =future.getTaskId();// ...executorService.cancelScheduledTask(taskId);

上一篇:

下一篇:

  同类阅读

分享