1 異步@Async詳解
1.1 引言
在java
中異步線程很重要,比如在業(yè)務(wù)流處理時,需要通知硬件設(shè)備,發(fā)短信通知用戶,或者需要上傳一些圖片資源到其他服務(wù)器這種耗時的操作,在主線程里處理會阻塞整理流程,而且我們也不需要等待處理結(jié)果之后再進行下一步操作,這時候就可以使用異步線程進行處理,這樣主線程不會因為這些耗時的操作而阻塞,保證主線程的流程可以正常進行。
最近在項目中使用了很多線程的操作,在這做個記錄
1.2 異步說明和原理
使用地方說明:
- 在方法上使用該
@Async
注解,申明該方法是一個異步任務(wù); - 在類上面使用該
@Async
注解,申明該類中的所有方法都是異步任務(wù); - 使用此注解的方法的類對象,必須是
spring
管理下的bean
對象; - 要想使用異步任務(wù),需要在主類上開啟異步配置,即,配置上
@EnableAsync
注解;
@Async
的原理概括:@Async
的原理是通過 Spring AOP
動態(tài)代理 的方式來實現(xiàn)的。Spring
容器啟動初始化bean
時,判斷類中是否使用了@Async
注解,如果使用了則為其創(chuàng)建切入點和切入點處理器,根據(jù)切入點創(chuàng)建代理,在線程調(diào)用@Async
注解標注的方法時,會調(diào)用代理,執(zhí)行切入點處理器invoke
方法,將方法的執(zhí)行提交給線程池中的另外一個線程來處理,從而實現(xiàn)了異步執(zhí)行。
所以,需要注意的一個錯誤用法是,如果a方法調(diào)用它同類中的標注@Async
的b
方法,是不會異步執(zhí)行的,因為從a方法進入調(diào)用的都是該類對象本身,不會進入代理類。
因此,相同類中的方法調(diào)用帶@Async
的方法是無法異步的,這種情況仍然是同步。
1.3 @Async使用
在Spring
中啟用@Async
:
-
@Async
注解在使用時,如果不指定線程池的名稱,則使用Spring
默認的線程池,Spring
默認的線程池為SimpleAsyncTaskExecutor
。 - 方法上一旦標記了這個
@Async
注解,當其它線程調(diào)用這個方法時,就會開啟一個新的子線程去異步處理該業(yè)務(wù)邏輯。
1.3.1 啟動類中增加@EnableAsync
以Spring boot
為例,啟動類中增加@EnableAsync
:
@EnableAsync
@SpringBootApplication
public class ManageApplication {
//...
}
1.3.2 方法上加@Async注解
@Component
public class MyAsyncTask {
@Async
public void asyncCpsItemImportTask(Long platformId, String jsonList){
//...具體業(yè)務(wù)邏輯
}
}
1.4 @Async異步線程池
1.4.1 默認線程池
上面的配置會啟用默認的線程池/執(zhí)行器,異步執(zhí)行指定的方法。
Spring
默認的線程池的默認配置:
默認核心線程數(shù):8,
最大線程數(shù):Integet.MAX_VALUE,
隊列使用LinkedBlockingQueue,
容量是:Integet.MAX_VALUE,
空閑線程保留時間:60s,
線程池拒絕策略:AbortPolicy
缺點
:從最大線程數(shù)的配置上,相信看到問題:并發(fā)情況下,會無限創(chuàng)建線程
默認線程池的上述缺陷如何解決:答案是,自定義配置參數(shù)就可以了
1.4.3 在配置文件中配置
spring:
task:
execution:
pool:
max-size: 6
core-size: 3
keep-alive: 3s
queue-capacity: 1000
thread-name-prefix: name
1.4.3 自定義線程池
在業(yè)務(wù)場景中,有時需要使用自己定義的執(zhí)行器來跑異步的業(yè)務(wù)邏輯,那該怎么辦呢?答案是,自定義線程池。
1.4.3.1 編寫配置類
@Configuration
@Data
public class ExecutorConfig{
//核心線程
private int corePoolSize;
//最大線程
private int maxPoolSize;
//隊列容量
private int queueCapacity;
//保持時間
private int keepAliveSeconds;
//名稱前綴
private String preFix;
@Bean("MyExecutor")
public Executor myExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAliveSeconds);
executor.setThreadNamePrefix(preFix);
executor.setRejectedExecutionHandler( new ThreadPoolExecutor.AbortPolicy());
executor.initialize();
return executor;
}
}
1.4.3.2 使用自定義線程池
在方法上的@Async
注解處指定線程池名字:
@Component
public class MyAsyncTask {
@Async("MyExecutor") //使用自定義的線程池(執(zhí)行器)
public void asyncCpsItemImportTask(Long platformId, String jsonList){
//...具體業(yè)務(wù)邏輯
}
}
1.4.4 Spring中的線程池(執(zhí)行器)
Spring
用TaskExecutor
和TaskScheduler
接口提供了異步執(zhí)行
和調(diào)度任務(wù)
的抽象。
Spring
的TaskExecutor
和java.util.concurrent.Executor
接口時一樣的,這個接口只有一個方法execute(Runnable task)
。
Spring
已經(jīng)內(nèi)置了許多TaskExecutor
的實現(xiàn),沒有必要自己去實現(xiàn):
-
SimpleAsyncTaskExecutor
: 這種實現(xiàn)不會重用任何線程,每次調(diào)用都會創(chuàng)建一個新的線程。 -
SyncTaskExecutor
: 這種實現(xiàn)不會異步的執(zhí)行,相反,每次調(diào)用都在發(fā)起調(diào)用的線程中執(zhí)行。它的主要用處是在不需要多線程的時候,比如簡單的測試用例; -
ConcurrentTaskExecutor
:這個實現(xiàn)是對Java 5 java.util.concurrent.Executor
類的包裝。有另一個ThreadPoolTaskExecutor
類更為好用,它暴露了Executor
的配置參數(shù)作為bean
屬性。
點擊了解Spring線程池ThreadPoolTaskExecutor講解 -
SimpleThreadPoolTaskExecutor
: 這個實現(xiàn)實際上是Quartz
的SimpleThreadPool
類的子類,它會監(jiān)聽Spring
的生命周期回調(diào)。當有線程池,需要在Quartz
和非Quartz
組件中共用時,這是它的典型用處。 -
ThreadPoolTaskExecutor
:這是最常用、最通用的一種實現(xiàn)。它包含了java.util.concurrent.ThreadPoolExecutor
的屬性,并且用TaskExecutor
進行包裝。
1.5 異步中的事務(wù)和返回
1.5.1 異步事務(wù)
在@Async
標注的方法,同時也使用@Transactional
進行標注;在其調(diào)用數(shù)據(jù)庫操作之時,將無法產(chǎn)生事務(wù)管理的控制,原因就在于其是基于異步處理
的操作。
那該如何給這些操作添加事務(wù)管理呢?
可以將需要事務(wù)管理操作的方法放置到異步方法內(nèi)部,在內(nèi)部被調(diào)用的方法上添加@Transactional
示例:
-
方法A
:使用了@Async/@Transactional
來標注,但是無法產(chǎn)生事務(wù)控制的目的。 -
方法B
:使用了@Async
來標注,B
中調(diào)用了C、D
,C/D
分別使用@Transactional
做了標注,則可實現(xiàn)事務(wù)控制的目的
1.5.2 異步返回
異步的業(yè)務(wù)邏輯處理場景 有兩種:一個是不需要返回結(jié)果,另一種是需要接收返回結(jié)果。不需要返回結(jié)果的比較簡單,就不多說了。
需要接收返回結(jié)果的示例如下:
@Async("MyExecutor")
public Future<Map<Long, List>> queryMap(List ids) {
List<> result = businessService.queryMap(ids);
..............
Map<Long, List> resultMap = Maps.newHashMap();
...
return new AsyncResult<>(resultMap);
}
調(diào)用異步方法的示例:
public Map<Long, List> asyncProcess(List<BindDeviceDO> bindDevices,List<BindStaffDO> bindStaffs, String dccId) {
Map<Long, List> finalMap =null;
// 返回值:
Future<Map<Long, List>> asyncResult = MyService.queryMap(ids);
try {
finalMap = asyncResult.get();
} catch (Exception e) {
...
}
return finalMap;
}
1.6 異步不能回調(diào)問題
使用了異步但是執(zhí)行異步的方法,原因是在方法上加了@Async
注解,之所以加這個注解是因為報錯:
There was an unexpected error (type=Internal Server Error, status=500).
Async support must be enabled on a servlet and for all filters involved in async request processing. This is done in Java code using the Servlet API or by adding<async-supported>true</async-supported>
to servlet and filter declarations in web.xml
異步測試時一直報這個錯誤,提示我在web.xml
開啟異步支持,但是我是SpringBoot
項目,于是開始網(wǎng)上查找錯誤
:加@Async
注解,會更加異步,不能獲取異步結(jié)果正確
:根本原因是容器注冊問題,在springboot
啟動類的注解@SpringBootApplication
旁邊添加了@ServletComponentScan
,才導致上面的報錯和不能回調(diào),有三種解決方法:文章來源:http://www.zghlxwxcb.cn/news/detail-430290.html
- 去掉注解
@ServletComponentScan
- 添加容器注冊(springboot項目)
@Bean
public ServletRegistrationBean dispatcherServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new DispatcherServlet(), "/");
registration.setAsyncSupported(true);
return registration;
}
@Bean
DispatcherServlet dispatcherServlet(){
return new DispatcherServlet();
}
在過濾器那里添加asyncSupported = true
的支持文章來源地址http://www.zghlxwxcb.cn/news/detail-430290.html
@WebFilter(urlPatterns="/*",asyncSupported = true)
- 修改
web.xml
(傳統(tǒng)xml項目)
需要在web.xml
文件中的servlet
定義中添加:"<async-supported>true</async-supported>"
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>dev</param-value>
</context-param>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
到了這里,關(guān)于Spring之異步任務(wù)@Async詳解分析的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!