Spring的两种任务调度Scheduled和Async
2017-05-15 09:47
429 查看
Spring提供了两种后台任务的方法,分别是:
调度任务,@Schedule异步任务,@Async
当然,使用这两个是有条件的,需要在spring应用的上下文中声明
<task:annotation-driven/>当然,如果我们是基于java配置的,需要在配置哪里加多
EnableScheduling和
@EnableAsync就像下面这样
1 2 3 4 5 6 | @EnableScheduling @EnableAsync public class WebAppConfig { .... } |
@Schedule
先看下@Schedule怎么调用再说
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public final static long ONE_DAY = 24 * 60 * 60 * 1000; public final static long ONE_HOUR = 60 * 60 * 1000; @Scheduled(fixedRate = ONE_DAY) public void scheduledTask() { System.out.println(" 我是一个每隔一天就会执行一次的调度任务"); } @Scheduled(fixedDelay = ONE_HOURS) public void scheduleTask2() { System.out.println(" 我是一个执行完后,隔一小时就会执行的任务"); } @Scheduled(initialDelay=1000, fixedRate=5000) public void doSomething() { // something that should execute periodically } @Scheduled(cron = "0 0/1 * * * ? ") public void ScheduledTask3() { System.out.println(" 我是一个每隔一分钟就就会执行的任务"); } |
关于最后一个,在指定时间执行的任务,里面使用的是
Cron表达式,同时我们看到了两个不一样的面孔
fixedDelay&
fixedRate,前者
fixedDelay表示在指定间隔运行程序,例如这个程序在今晚九点运行程序,跑完这个方法后的一个小时,就会再执行一次,而后者
fixedDelay者是指,这个函数每隔一段时间就会被调用(我们这里设置的是一天),不管再次调度的时候,这个方法是在运行还是结束了。而前者就要求是函数运行结束后开始计时的,这就是两者区别。
这个还有一个initialDelay的参数,是第一次调用前需要等待的时间,这里表示被调用后的,推迟一秒再执行,这适合一些特殊的情况。
我们在
serviceImpl类写这些调度任务时候,也需要在这些我们定义的
serviceInterface的借口中写多这个接口,要不然会爆
but not found in any interface(s) for bean JDK proxy.Either pull the method up to an interface or
@Async
有时候我们会调用一些特殊的任务,任务会比较耗时,重要的是,我们不管他返回的后果。这时候我们就需要用这类的异步任务啦,调用后就让他去跑,不堵塞主线程,我们继续干别的。代码像下面这样:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public void AsyncTask(){ @Async public void doSomeHeavyBackgroundTask(int sleepTime) { try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } } @Async public Future<String> doSomeHeavyBackgroundTask() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } public void printLog() { System.out.println(" i print a log ,time=" + System.currentTimeMillis()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = AsycnTaskConfig.class) //要声明@EnableASync public class AsyncTaskTest { @Autowired AsyncTask asyncTask; @Test public void AsyncTaskTest() throws InterruptedException { if (asyncTask != null) { asyncTask.doSomeHeavyBackgroundTask(4000); asyncTask.printLog(); Thread.sleep(5000); } } } |
@Async去掉就可以了,另外如果你想要返回个结果的,这需要加多个Future<>,关于这个Future,完全可以写多几篇文章介绍,顺便把FutureTask介绍了。如果想修改Spring boot的默认线程池配置,可以实现AsyncConfigurer.
需要注意的:
相对于
@scheduled,这个可以有参数和返回个结果,因为这个是我们调用的,而调度的任务是spring调用的。
异步方法不能内部调用,只能像上面那样,外部调用,否则就会变成阻塞主线程的同步任务啦!这个坑我居然跳下去了!例如下面这样的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public void AsyncTask(){ public void fakeAsyncTaskTest(){ doSomeHeavyBackgroundTask(4000); printLog(); //你会发现,当你像这样内部调用的时候,居然是同步执行的,不是异步的!! } @Async public void doSomeHeavyBackgroundTask(int sleepTime) { try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } } public void printLog() { System.out.println(" i print a log "); } } |
关于异常处理,难免在这个异步执行过程中有异常发生,对于这个问题,spring提供的解决方案如下,实现 AsyncUncaughtExceptionHandler接口。
1 2 3 4 5 6 7 8 9 10 11 | public class MyAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler { @Override public void handleUncaughtException(Throwable ex, Method method, Object... params) { // handle exception } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | @Configuration @EnableAsync public class AsyncConfig implements AsyncConfigurer { @Bean public AsyncTask asyncBean() { return new AsyncTask(); } @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(7); executor.setMaxPoolSize(42); executor.setQueueCapacity(11); executor.setThreadNamePrefix("MyExecutor-"); executor.initialize(); return executor; } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return new MyAsyncUncaughtExceptionHandler(); } } |
Quartz登场
处理这两个外,还有一个和spring整合的第三方库叫Quartz看了下官网的使用简介,也是挺逗的,现在都习惯用maven,gradle之类来关系这些依赖了,他还叫人下载,也是不知为何,详情点击->
http://quartz-scheduler.org/documentation/quartz-2.2.x/quick-start
估计有可能是因为没再维护了的原因吧,看了下,最新版2.2居然是Sep, 2013更新的…
居然是停更的,不过Quartz作为一个企业级应用的任务调度框架,还是一个可以的候选项目的。
这里不铺开讲,有兴趣就去官网看下吧。整体用起来感觉是没有spring自己的后台任务方便,不过也可以接受,只需要简单的配置就可以使用了。
http://www.cnblogs.com/slimer/p/6401394.html
相关文章推荐
- Spring之——两种任务调度Scheduled和Async
- Spring的两种任务调度Scheduled和Async
- Spring @Scheduled @Async联合实现调度任务(2017.11.28更新)
- Spring任务调度<task:scheduled-tasks>【含cron参数详解】
- 06 Spring 异步执行,任务调度(@Schedule、@Async)
- Spring任务调度scheduled
- Spring任务调度<task:scheduled-tasks>【含cron参数详解】 (转载)
- Spring任务调度<task:scheduled-tasks>【含cron参数详解】
- 06 Spring 异步执行,任务调度(@Schedule、@Async)
- 关于Spring任务调度<task:scheduled-tasks>的cron参数详解
- Spring任务调度<task:scheduled-tasks>【含cron参数详解】
- 06 Spring 异步执行,任务调度(@Schedule、@Async)
- 06 Spring 异步执行,任务调度(@Schedule、@Async)
- Spring任务调度<task:scheduled-tasks>【含cron参数详解】
- 06 Spring 异步执行,任务调度(@Schedule、@Async)
- 06 Spring 异步执行,任务调度(@Schedule、@Async)
- 06 Spring 异步执行,任务调度(@Schedule、@Async)
- 06 Spring 异步执行,任务调度(@Schedule、@Async)
- Spring任务定时调度<task:scheduled-tasks>cron参数详解
- Spring任务调度两种配置方式