欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot 异步线程间传递上下文方式

程序员文章站 2022-08-06 17:53:27
目录异步线程间传递上下文需求实现启用异步功能配置异步配置任务装饰器启用多线程安全上下文无法在线程间共享问题问题解决方案原理结果异步线程间传递上下文需求springboot项目中,经常使用@async来...

异步线程间传递上下文

需求

springboot项目中,经常使用@async来开启一个子线程来完成异步操作。主线程中的用户信息需要传递给子线程

实现

启用异步功能

在启动类里加上@enableasync注解

@enableasync
@springbootapplication
public class application {}

配置异步

新建一个配置类,实现asyncconfigurer接口,并重写getasyncexecutor方法

@configuration
public class asyncconfig implements asyncconfigurer {
    @override
    public executor getasyncexecutor() {
        threadpooltaskexecutor executor = new threadpooltaskexecutor();
        executor.setcorepoolsize(10);
        executor.setmaxpoolsize(50);
        executor.setthreadnameprefix("async-pool-");
        // 这一步是关键,异步task装饰器
        executor.settaskdecorator(new mycontextdecorator());
        executor.initialize();
        return executor;
    }
}

配置任务装饰器

新建一个异步任务装饰器,实现taskdecorator接口,并重写decorate方法

public class mycontextdecorator implements taskdecorator {
    @override
    @nonnull
    public runnable decorate(@nonnull runnable runnable) {
  // 获取主线程中的请求信息(我们的用户信息也放在里面)
       requestattributes attributes = requestcontextholder.getrequestattributes();
        return () -> {
            try {
               // 将主线程的请求信息,设置到子线程中
               requestcontextholder.setrequestattributes(attributes);
              // 执行子线程,这一步不要忘了
                runnable.run();
            } finally {
             // 线程结束,清空这些信息,否则可能造成内存泄漏
                requestcontextholder.resetrequestattributes();
            }
        };
    }

补充下:requestcontextholder内部是基于threadlocal实现的,因此在使用set get时,都是和当前线程绑定的。当然,使用者的用户信息不一定放在了requestcontextholder里面,读者可以自行扩展。

到此,通过@async开启的子线程,就可以正常拿到父线程中的request信息了。

启用多线程安全上下文无法在线程间共享问题

问题

项目中多线程添加数据,mybatisplus元数据填充功能,填充创建人时,数据是来自 spring security securitycontextholder.getcontext.getauthentication,同步操作时,能正常获取,而异步执行时空指针异常。

解决方案

配置安全上下文全局策略securitycontextholder.setstrategyname(securitycontextholder.mode_inheritablethreadlocal)

原理

spring security 安全上下文默认策略为mode_threadlocal,threadlocal机制来保存每个使用者的安全上下文。

这意味着,只要针对某个使用者的逻辑执行都是在同一个线程中进行,即使不在各个方法之间以参数的形式传递其安全上下文,各个方法也能通过securitycontextholder工具获取到该安全上下文。

只要在处理完当前使用者的请求之后注意清除threadlocal中的安全上下文,这种使用threadlocal的方式是很安全的。

  • mode_global: jvm中所有的线程使用同一个安全上下文
  • mode_inheritablethreadlocal:有些应用会有自己的线程创建,并且希望这些新建线程也能使用创建者的安全上下文。这种效果,可以通过将securitycontextholder配置成mode_inheritablethreadlocal策略达到。

结果

在配置文件中添加:

@postconstruct
public void init(){
    securitycontextholder.setstrategyname(securitycontextholder.mode_inheritablethreadlocal);
}
 

@postconstruct注解好多人以为是spring提供的。其实是java自己的注解。

java中该注解的说明:@postconstruct该注解被用来修饰一个非静态的void()方法。被@postconstruct修饰的方法会在服务器加载servlet的时候运行,并且只会被服务器执行一次。postconstruct在构造函数之后执行,init()方法之前执行。

通常我们会是在spring框架中使用到@postconstruct注解 该注解的方法在整个bean初始化中的执行顺序:

constructor(构造方法) -> @autowired(依赖注入) -> @postconstruct(注释的方法)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。