1.功能说明
Spring提供了Async注解来实现方法的异步调用。
即当调用Async标识的方法时,调用线程不会等待被调用方法执行完成即返回继续执行以下操作,而被调用的方法则会启动一个独立线程来执行此方法。
这种异步执行的方式通常用于处理接口中不需要返回给用户的数据处理。比如当注册的时候,只需要将用户信息返回用户,而关于信息的保存操作可以使用异步执行。
Spring提供了Scheduled注解来实现定时任务的功能。
在异步方法和定时任务功能中都是开发这自己定义需要执行的方法,然后交给Spring容器管理线程,并执行相应的方法。在使用异步方法和定时任务的时候需要特别注意的是线程池的配置以及任务中异常的处理。下面对这两个功能进行简单介绍。
2.实现
2.1Async异步方法的实现
2.1.1Spring配置文件
定义一个线程池,然后引入。线程在空闲后会根据存活时间配置进行关闭。
在applicationContext.xml同目录下创建文件threadPool.xml内容如下,然后在applicationContext.xml中引入threadPool.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<!-- 开启异步,并引入线程池 -->
<task:annotation-driven executor="threadPool" />
<!-- 定义线程池 -->
<bean id="threadPool"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<!-- 核心线程数,默认为1 -->
<property name="corePoolSize" value="5" />
<!-- 最大线程数,默认为Integer.MAX_VALUE -->
<property name="maxPoolSize" value="20" />
<!-- 队列最大长度,一般需要设置值>=notifyScheduledMainExecutor.maxNum;默认为Integer.MAX_VALUE -->
<property name="queueCapacity" value="500" />
<!-- 线程池维护线程所允许的空闲时间,默认为60s -->
<property name="keepAliveSeconds" value="30" />
<!-- 完成任务自动关闭 , 默认为false-->
<property name="waitForTasksToCompleteOnShutdown" value="true" />
<!-- 核心线程超时退出,默认为false -->
<property name="allowCoreThreadTimeOut" value="true" />
<!-- 线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy;默认为后者 -->
<property name="rejectedExecutionHandler">
<!-- AbortPolicy:直接抛出java.util.concurrent.RejectedExecutionException异常 -->
<!-- CallerRunsPolicy:主线程直接执行该任务,执行完之后尝试添加下一个任务到线程池中,可以有效降低向线程池内添加任务的速度 -->
<!-- DiscardOldestPolicy:抛弃旧的任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
<!-- DiscardPolicy:抛弃当前任务、暂不支持;会导致被丢弃的任务无法再次被执行 -->
<bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
</property>
</bean>
</beans>
2.1.2SpringBoot则是添加@EnableAsync注解
@EnableAsync
@SpringBootApplication
@ServletComponentScan
@MapperScan("com.cmc.schedule.model.mapper") //配置扫描mapper接口的地址
public class NLPApplication extends SpringBootServletInitializer {
//不使用springboot内嵌tomcat启动方式
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(NLPApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(NLPApplication.class, args);
}
//默认使用fastjson解析
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}
2.1.3这一步Spring和SpringBoot都是一样的,创建一个异步方法的类
package com.cmc.tst;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
* @Component 注解必须要有,否则无法将此bean注入
* 当然也可以使用其他的注解,只要可以装配就行
*
* @author chenmc
* @date 2017年9月4日 下午3:38:29
*/
@Component
public class MyAsync {
/**
* @Async 表明这是一个异步方法,也就是说当调用这个方法时,
* spring会创建一条线程来执行这个方法。
* 注意:不能使用static来修饰此方法,否则@Async无效
*
* @author chenmc
* @date 2017年9月4日 下午3:34:24
*/
@Async
public void asyncMethod(){
System.out.println(Thread.currentThread().getName());
}
}
2.1.4测试调用异步方法,我这里使用的是JUnit4测试
@Autowired
MyAsync async;
@Test
public void test() {
System.out.println(Thread.currentThread().getName() + "start");
//MyAsync async = new MyAsync(); //自己new出来的对象@Async将无效,必须要spring注入的
async.asyncMethod();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "end");
}
这里总结一下@Async注解无效的可能点
一、异步方法使用static修饰
二、异步类没有使用@Component注解(或其他注解)导致spring无法扫描到异步类
三、测试异步方法不能与异步方法在同一个类中
四、测试类中需要使用@Autowired或@Resource等注解自动注入,不能自己手动new对象
五、如果使用SpringBoot框架必须在启动类中增加@EnableAsync注解
2.2 Scheduled定时任务实现
定时任务标识注解Scheduled,定义如下:
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
定时任务
String cron() default "";
String zone() default "";
//上次执行结束到下次执行开始
long fixedDelay() default -1;
String fixedDelayString() default "";
上次执行开始到本次执行开始
long fixedRate() default -1;
fixedRate或者fixedDelay的时候第一次的延迟时间
long initialDelay() default -1;
}
3 对于线程池的选择顺序
3.1 Async对于线程池的选择顺序
Async线程池的选择顺序如下图所示:
Spring在执行async标识的异步方法的时候首先会在Spring的上下文中搜索类型为TaskExecutor或者名称为“taskExecutor”的bean,当可以找到的时候,就将任务提交到此线程池中执行。当不存在以上线程池的时候,Spring会手动创建一个SimpleAsyncTaskExecutor执行异步任务。
另外当标识async注解的时候指定了,value值,Spring会使用指定的线程池执行。比如以下:
@Async(value = "asyncTaskThreadPool")
这个时候Spring会去上下文中找名字为asyncTaskThreadPool的bean,并执行异步任务,找不到,会抛出异常。
3.2 Scheduled对于线程池的选择顺序
Scheduled对于线程池的选择顺序如下图所示:
当Spring执行定时任务的时候,首先会在上下文中找类型为TaskScheduler或者名称为taskScheduler的bean,找不到的时候会手动创建一个线程执行此task。