美文网首页
起新线程调用web接口方法报错:No thread-bound

起新线程调用web接口方法报错:No thread-bound

作者: Bigglesworth | 来源:发表于2022-08-31 14:32 被阅读0次

报错场景

我想在controller里,起新线程调用同类下的另一个web接口方法,直接使用

new Thread(()->{
 this.funcB();
}).start();

因为funcB也会用到session里的信息,所以会报错拿不到session。

解决方法

在起新线程前,添加子线程共享session即可

 ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
 RequestContextHolder.setRequestAttributes(servletRequestAttributes,true);//设置子线程共享
 new Thread(()->{
            this.funcB();
        }).start();

相关文章

网友评论

      本文标题:起新线程调用web接口方法报错:No thread-bound

      本文链接:https://www.haomeiwen.com/subject/fchsnrtx.html