Spring Boot 之异步执行方法

Spring Boot 之异步执行方法
强烈推介IDEA2021.1.3破解激活,IntelliJ IDEA 注册码,2021.1.3IDEA 激活码  

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说Spring Boot 之异步执行方法,希望能够帮助大家进步!!!

给方法加上 @Async 注解

package me.deweixu.aysncdemo.service;
public interface AsyncService {
 void asyncMethod(String arg);
}
package me.deweixu.aysncdemo.service.ipml;
import me.deweixu.aysncdemo.service.AsyncService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncServiceImpl implements AsyncService {
 @Async
 @Override
 public void asyncMethod(String arg) {
 System.out.println("arg:" + arg);
 System.out.println("=====" + Thread.currentThread().getName() + "=========");
 }
}

@EnableAsync

在启动类或者配置类加上 @EnableAsync 注解

package me.deweixu.aysncdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync
@SpringBootApplication
public class AysncDemoApplication {
 public static void main(String[] args) {
 SpringApplication.run(AysncDemoApplication.class, args);
 }
}

测试

package me.deweixu.aysncdemo;
import me.deweixu.aysncdemo.service.AsyncService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AysncDemoApplicationTests {
 @Autowired
 AsyncService asyncService;
 @Test
 public void testAsync() {
 System.out.println("=====" + Thread.currentThread().getName() + "=========");
 asyncService.asyncMethod("Async");
 }
}
=====main=========
2018-03-25 21:30:31.391 INFO 28742 --- [ main] .s.a.AnnotationAsyncExecutionInterceptor : No task executor bean found for async processing: no bean of type TaskExecutor and no bean named 'taskExecutor' either
arg:Async
=====SimpleAsyncTaskExecutor-1=========

 

从上面的结果看 asyncService.asyncMethod("Async") 确实异步执行了,它使用了一个新的线程。

指定 "Executor"

从上面执行的日志可以猜测到 Spring 默认使用 SimpleAsyncTaskExecutor 来异步执行任务的,可以搜索到这个类。 @Async 也可以指定自定义的 Executor。

在启动类中增加自定义的 Executor

package me.deweixu.aysncdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@EnableAsync
@SpringBootApplication
public class AysncDemoApplication {
 public static void main(String[] args) {
 SpringApplication.run(AysncDemoApplication.class, args);
 }
 @Bean(name = "threadPoolTaskExecutor")
 public Executor threadPoolTaskExecutor() {
 return new ThreadPoolTaskExecutor();
 }
}

指定 "Executor"

package me.deweixu.aysncdemo.service.ipml;
import me.deweixu.aysncdemo.service.AsyncService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncServiceImpl implements AsyncService {
 @Async("threadPoolTaskExecutor")
 @Override
 public void asyncMethod(String arg) {
 System.out.println("arg:" + arg);
 System.out.println("=====" + Thread.currentThread().getName() + "=========");
 }
}

这样在异步执行任务的时候就使用 threadPoolTaskExecutor

设置默认的

Executor

上面提到如果 @Async 不指定 Executor 就默认使用 SimpleAsyncTaskExecutor,其实默认的 Executor 是可以使用 AsyncConfigurer 接口来配置的

@Configuration
public class SpringAsyncConfig implements AsyncConfigurer {
 
 @Override
 public Executor getAsyncExecutor() {
 return new ThreadPoolTaskExecutor();
 }
 
}

异常捕获

在异步执行的方法中是可能出现异常的,我们可以在任务内部使用 try catch 来处理异常,当任务抛出异常时, Spring 也提供了捕获它的方法。

实现 AsyncUncaughtExceptionHandler 接口

public class CustomAsyncExceptionHandler
 implements AsyncUncaughtExceptionHandler {
 
 @Override
 public void handleUncaughtException(
 Throwable throwable, Method method, Object... obj) {
 
 System.out.println("Exception message - " + throwable.getMessage());
 System.out.println("Method name - " + method.getName());
 for (Object param : obj) {
 System.out.println("Parameter value - " + param);
 }
 }
 
}

实现 AsyncConfigurer 接口重写 getAsyncUncaughtExceptionHandler 方法
@Configuration
public class SpringAsyncConfig implements AsyncConfigurer {
 
 @Override
 public Executor getAsyncExecutor() {
 return new ThreadPoolTaskExecutor();
 }
 
 @Override
 public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
 return new CustomAsyncExceptionHandler();
 }
 
}

改写 asyncMethod 方法使它抛出异常

 @Async
 @Override
 public void asyncMethod(String arg) {
 System.out.println("arg:" + arg);
 System.out.println("=====" + Thread.currentThread().getName() + "=========");
 throw new NullPointerException();
 }

运行结果:

=====main=========
arg:Async
=====threadPoolTaskExecutor-1=========
Exception message - Async NullPointerException
Method name - asyncMethod
Parameter value - Async

正确捕获到了异常。

 

本文来源huayang183,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/18144

发表评论