如何在异步任务执行程序中启用请求范围

在我的应用程序中,我有一些异步Web服务。服务器接受请求,返回OK响应并使用AsyncTaskExecutor启动处理请求。我的问题是如何在此处启用请求范围,因为在此处理中我需要获取注释的类:


@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)

现在我得到例外:


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.requestContextImpl': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

因为它运行SimpleAsyncTaskExecutor而不是运行DispatcherServlet


我的异步处理请求


taskExecutor.execute(new Runnable() {


    @Override

    public void run() {

        asyncRequest(request);

    }

});

taskExecutor的位置是:


<bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />


猛跑小猪
浏览 2357回答 3
3回答

小唯快跑啊

由于原始父请求处理线程可能已将响应提交到客户端并且所有请求对象都已销毁,因此无法在子异步线程中获取请求范围对象。处理此类方案的一种方法是使用自定义范围,如SimpleThreadScope。SimpleThreadScope的一个问题是子线程不会继承父范围变量,因为它在内部使用简单的ThreadLocal。要克服该实现自定义范围,它与SimpleThreadScope完全相似,但在内部使用InheritableThreadLocal。有关更多信息,请参阅 Spring MVC:如何在生成的线程中使用请求范围的bean?
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java