Spring的两种任务调度Scheduled和Async

Spring提供了两种后台任务的方法,分别是:

  1. 调度任务,@Schedule
  2. 异步任务,@Async

当然,使用这两个是有条件的,需要在spring应用的上下文中声明
<task:annotation-driven/>当然,如果我们是基于java配置的,需要在配置哪里加多EnableScheduling@EnableAsync 就像下面这样

1
2
3
4
5
6
@EnableScheduling
@EnableAsync
public class WebAppConfig {
....

除此之外,还是有第三方库可以调用的,例如Quartz.


@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(" 我是一个每隔一分钟就就会执行的任务");
}

需要注意的

  1. 关于最后一个,在指定时间执行的任务,里面使用的是Cron表达式,同时我们看到了两个不一样的面孔fixedDelayfixedRate,前者fixedDelay表示在指定间隔运行程序,例如这个程序在今晚九点运行程序,跑完这个方法后的一个小时,就会再执行一次,而后者fixedDelay者是指,这个函数每隔一段时间就会被调用(我们这里设置的是一天),不管再次调度的时候,这个方法是在运行还是结束了。而前者就要求是函数运行结束后开始计时的,这就是两者区别。

  2. 这个还有一个initialDelay的参数,是第一次调用前需要等待的时间,这里表示被调用后的,推迟一秒再执行,这适合一些特殊的情况。

  3. 我们在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.
需要注意的:

  1. 相对于@scheduled,这个可以有参数和返回个结果,因为这个是我们调用的,而调度的任务是spring调用的。
  2. 异步方法不能内部调用,只能像上面那样,外部调用,否则就会变成阻塞主线程的同步任务啦!这个坑我居然跳下去了!例如下面这样的。
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 ");
}
}
  1. 另外一点就是不要重复的扫描,这也会导致异步无效,具体的可以看这个stackoveflow的spring-async-not-working Issue。
  2. 关于异常处理,难免在这个异步执行过程中有异常发生,对于这个问题,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
}
}

写好我们的异常处理后,我们需要配置一下,告诉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
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

 

转载于:https://www.cnblogs.com/softidea/p/6855034.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541373.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

建立单链表 单链表的插入_单链列表插入

建立单链表 单链表的插入All possible cases: 所有可能的情况&#xff1a; Inserting at beginning 开始插入 Inserting at the ending 在末尾插入 Inserting at given position 在给定位置插入 Algorithms: 算法&#xff1a; 1)开始插入 (1) Inserting at the beginning) In…

mysql学习笔记(1-安装简介)

mysql的安装方式&#xff1a;(1)通过系统提供的默认版本(rpm包&#xff0c;稳定版&#xff0c;该版本满足了使用的需求&#xff0c;建议使用&#xff0c;os vendor)(2)mysql官方提供官方提供的通用rpm安装包通用二进制格式的程序包(直接下载文件&#xff0c;解压到指定目录&…

存储器间接寻址方式_8086中的数据存储器寻址模式

存储器间接寻址方式In this type of addressing mode, first the offset address is calculated, then the memory address is calculated and then the operand form that memory location is fetched. There are following modes which lie under the Data Addressing Mode: …

oracle asm 删除diskgroup,ASM磁盘组删除DISK操作

没想到这么简单的操作&#xff0c;由于不熟悉还碰到了两个小问题。[oracledbserver1 ~]$ sqlplus / as sysdbaSQL*Plus: Release 11.2.0.2.0 Production on Tue Aug 9 10:08:062011Copyright (c) 1982, 2010, Oracle.All rights reserved.Connected to:Oracle Database 11g Ent…

intellij idea 最常用的快捷键

F2&#xff0c; 可以快速的向下跳走 CtrlF7&#xff0c;可以查询当前元素在当前文件中的引用&#xff0c;然后按 F3 可以选择AltQ&#xff0c;可以看到当前方法的声明CtrlP&#xff0c;可以显示参数信息CtrlAltV&#xff0c;可以引入变量。例如&#xff1a;new String(); 自动导…

如何在Java中检查字符串是否为数字?

We will check whether string is a number or not – with the help of logic we will solve this problem, 我们将检查字符串是否为数字-借助逻辑&#xff0c;我们将解决此问题&#xff0c; In the first step, we will take a string variable named str and store any val…

oracle清理告警日志,Oracle 跟踪/告警/监听日志的清理脚本

[root ~]# cat del_oracle_log.sh#!/bin/bashsource /home/oracle/.bash_profilefunction audit_log(){ #---audit_log日志跟踪文件#audit_log$(strings $ORACLE_HOME/dbs/spfile$ORACLE_SID.ora|grep -i audit_file_dest|awk -F {print $NF}|sed "s///g")audit_lo…

python爬虫之scrapy框架

Scrapy是一个为了爬取网站数据&#xff0c;提取结构性数据而编写的应用框架。 其可以应用在数据挖掘&#xff0c;信息处理或存储历史数据等一系列的程序中。其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的&#xff0c; 也可以应用在获取API所返回的数据(例如 Amazon As…

oracle中的事务回滚,ORACLE 死事务的回滚

死事务出现在异常关闭数据库或者事务进程不正常结束&#xff0c;比如KILL -9&#xff0c;shutdown abort的情况下。当前数据库里的死事务可以通过查询内部表x$ktuxe来获得。select ADDR,KTUXEUSN,KTUXESLT,KTUXESQN,KTUXESIZ from x$ktuxe where KTUXECFLDEAD;ADDR …

大数据数据可视化设计原则_数据可视化设计的8顶帽子

大数据数据可视化设计原则8 hats of data visualization are basically the important persons and their roles that are basically required to carry out data visualization are as follows: 数据可视化有8个基本要素&#xff0c;而进行数据可视化所需的基本角色如下&#…

debian8.8安装谷歌浏览器

第一步&#xff1a;下载&#xff1a; wget https://dl.google.com/linux/direct/google-chrome-stable_current_i386.deb //32位 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb //64位第二步&#xff1a;安装dpkg -i google-chrome*.deb…

MB_LEN_MAX常数,带C ++示例

C MB_LEN_MAX宏常量 (C MB_LEN_MAX macro constant) MB_LEN_MAX constant is a macro constant which is defied in climits header, it is used to get the maximum number of bytes in a multibyte character, for any locale, it returns maximum number of bytes that a m…

php谷歌收录接口,php实现查询百度google收录情况(示例代码)

PHP$SEOdetail array();$domain !empty($_GET[q]) ? $_GET[q] : www.mycodes.net;baidudetail($domain);googledetail($domain);var_dump($SEOdetail);function baidudetail($domain) {$baidu_site http://www.baidu.com/baidu?wordsite%3A . $domain;$baidu_link http:/…

Linux学习第三步(Centos7安装mysql5.7数据库)

版本&#xff1a;mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar 前言&#xff1a;在linux下安装mysql不如windows下面那么简单&#xff0c;但是也不是很难。本文向大家讲解了如何在Centos7下如何安装mysql5.7版本,如果有什么问题和错误的地方&#xff0c;欢迎大家指出。 注释&…

linux oracle删除恢复数据恢复,Linux下Oracle误删除数据文件恢复操作

检查数据文件的位置如下&#xff1a;SQL> select name from v$datafile;NAME--------------------------------------------------------------------------------/u01/app/Oracle/oradata/marven/system01.dbf/u01/app/oracle/oradata/marven/undotbs1.dbf/u01/app/oracle/…

数据库如何处理数据库太大_网络数据库中的数据处理

数据库如何处理数据库太大Before learning the data manipulation in a network model, we are discussing data manipulation language, so what is the data manipulation language? 在学习网络模型中的数据操作之前&#xff0c;我们正在讨论数据操作语言&#xff0c;那么什…

oracle12537错误,ORA-12537:TNS:connection closed错误处理方法

1.ORA-12537:TNS:connection closed错误处理过程检查监听正常&#xff0c;Oracle服务也是正常启动的&#xff0c;但是登录不进去。2.解决方案1. cd $ORACLE_HOME/bin/ 进入bin目录2. ll oracle-rwxrwxrwx. 1 ora12 dba 323762222 6?. 14 19:12 oracle3.chmod 6571 oracle 更改…

操作系统中的死锁_操作系统中的死锁介绍

操作系统中的死锁1.1究竟什么是僵局&#xff1f; (1.1 What exactly is a deadlock?) In a multiprogramming environment, there may be several processes with a finite number of resources. A process may request another resource while still holding some of the oth…

《云数据管理:挑战与机遇》2.3 数据库系统

本节书摘来自华章出版社《云数据管理》一书中的第2章&#xff0c;第3节&#xff0c;作者迪卫艾肯特阿格拉沃尔&#xff0c;更多章节内容可以访问云栖社区“华章计算机”公众号查看本节中&#xff0c;我们将为数据库系统中的一些主要概念提供一个相当抽象、简洁和高层次的描述。…