目的:解决用户请求后等待时间过长的问题,用户请求后,异步执行业务逻辑函数,提前返回信息给用户
技术:spring异步机制
使用方式:
1. 配置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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"
default-lazy-init="true">
<description>Spring Configuration</description>
<!-- 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = "0 0 2 * * ?")标注方法 -->
<!-- 定义异步任务线程池 -->
<bean id="executor" 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="60" />
<!-- 完成任务自动关闭 , 默认为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>
<task:scheduler id="scheduler" pool-size="10"/>
<task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
</beans>
2. 后端代码
异步方法
/**
* 异步执行函数
* @author congqian
* @version 2019-07-08
*/
@Service
public class DsAsyncFunctionService {
@Async(value = "executor") //异步注释
public void AsyncFunction(){
try {
System.out.print("开始异步函数");
Thread.sleep(3000); //延时3秒
System.out.print("结束异步函数");
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行异步方法
@Controller
@RequestMapping(value = "Async")
public class SoaDismantlingRecordController
{
@Autowired
private DsQuotationdataStatusService dsQuotationdataStatusService;
/**
* 异步调用测试
*/
@RequestMapping(value = {"AsyncTest"})
@ResponseBody
public AsyncTest()
{
try
{
dsAsyncFunctionService.AsyncFunction();
System.out.print("顺序执行");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
注意事项:1. 异步方法使用注解@Async ,返回值为void或者Future 2. 异步方法和调用异步方法不能写在同一个类,如果写在一个类中,则没有效果的