手機網站開發(fā)注意友情鏈接怎么購買
問題描述
springboot項目中,需要使用到異步調用某個方法,此時 第一個想到的就是 @Async 注解,但是 發(fā)現 方法執(zhí)行報錯了,具體報錯如下:
java.lang.NullPointerExceptionat com.ruoyi.common.utils.ServletUtils.getRequest(ServletUtils.java:56)at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:782)at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:717)at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:443)at com.ruoyi.web.ecs.service.impl.EcsCollectOperateServiceImpl.collect(EcsCollectOperateServiceImpl.java:42)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:90)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)at java.lang.reflect.Method.invoke(Method.java:508)at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)at java.util.concurrent.FutureTask.run(FutureTask.java:277)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1160)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)at java.lang.Thread.run(Thread.java:825)
上面日志有點多,其實核心就是這一部分日志:
java.lang.NullPointerExceptionat com.ruoyi.common.utils.ServletUtils.getRequest(ServletUtils.java:56)
這塊邏輯就是,使用spring底層提供的獲取上下文信息的方法。
所以說明 獲取不到上下文信息,結果導致報錯
解決辦法
- 對自定義的配置類 進行改造,原來的配置類是這樣的:
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;@Configuration
@EnableAsync
public class AsyncConfiguration {private static final Logger logger = LoggerFactory.getLogger(AsyncConfiguration.class);@Bean(name = "taskExecutor")public Executor getAsyncExecutor() {ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();// 核心線程數:線程池創(chuàng)建時候初始化的線程數taskExecutor.setCorePoolSize(10);// 最大線程數:線程池最大的線程數,只有在緩沖隊列滿了之后才會申請超過核心線程數的線程taskExecutor.setMaxPoolSize(20);// 緩沖隊列:用來緩沖執(zhí)行任務的隊列taskExecutor.setQueueCapacity(50);// 允許線程的空閑時間60秒:當超過了核心線程之外的線程在空閑時間到達之后會被銷毀taskExecutor.setKeepAliveSeconds(60);// 線程池名的前綴:設置好了之后可以方便我們定位處理任務所在的線程池taskExecutor.setThreadNamePrefix("HiTask-");// 緩沖隊列滿了之后的拒絕策略:由調用線程處理(一般是主線程)//taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());taskExecutor.initialize();return taskExecutor;}class MyAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {@Overridepublic void handleUncaughtException(Throwable throwable, Method method, Object... objects) {logger.error("MethodName={},Throwable={}",method.getName(),throwable.toString());}}
}
- 改造之后:
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;@Configuration
@EnableAsync
public class AsyncConfiguration {private static final Logger logger = LoggerFactory.getLogger(AsyncConfiguration.class);@Bean(name = "taskExecutor")public Executor getAsyncExecutor() {ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();// 核心線程數:線程池創(chuàng)建時候初始化的線程數taskExecutor.setCorePoolSize(10);// 最大線程數:線程池最大的線程數,只有在緩沖隊列滿了之后才會申請超過核心線程數的線程taskExecutor.setMaxPoolSize(20);// 緩沖隊列:用來緩沖執(zhí)行任務的隊列taskExecutor.setQueueCapacity(50);// 允許線程的空閑時間60秒:當超過了核心線程之外的線程在空閑時間到達之后會被銷毀taskExecutor.setKeepAliveSeconds(60);// 線程池名的前綴:設置好了之后可以方便我們定位處理任務所在的線程池taskExecutor.setThreadNamePrefix("HiTask-");// 緩沖隊列滿了之后的拒絕策略:由調用線程處理(一般是主線程)//taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());taskExecutor.initialize();//解決使用@Async注解,獲取不到上下文信息的問題taskExecutor.setTaskDecorator(runnable -> {RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();return ()->{try {// 我們set 進去 ,其實是一個ThreadLocal維護的.RequestContextHolder.setRequestAttributes(requestAttributes);runnable.run();} finally {// 最后記得釋放內存RequestContextHolder.resetRequestAttributes();}};});return taskExecutor;}class MyAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {@Overridepublic void handleUncaughtException(Throwable throwable, Method method, Object... objects) {logger.error("MethodName={},Throwable={}",method.getName(),throwable.toString());}}
}
總結
解決使用@Async注解,獲取不到上下文信息的問題,只需要增加這一段代碼即可
taskExecutor.setTaskDecorator(runnable -> {RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();return ()->{try {// 我們set 進去 ,其實是一個ThreadLocal維護的.RequestContextHolder.setRequestAttributes(requestAttributes);runnable.run();} finally {// 最后記得釋放內存RequestContextHolder.resetRequestAttributes();}};});
額外補充一點
如果使用 @Async注解,發(fā)現沒有生效,那有可能 你沒有加 @EnableAsync 注解。
@EnableAsync注解表示 開啟異步任務,可以寫在springboot的啟動類上,也可以寫在 配置類上