【发布时间】:2016-08-04 18:10:15
【问题描述】:
我正在尝试创建一个运行一组任务的线程,如果它没有在特定时间(比如 100 秒)内完成,则会引发异常。目前我正在尝试通过将任务封装到runnable 对象中并使用ExecutorService 和Future 类来执行超时执行来做到这一点。但是,当我启动我的 Web 服务时,我得到了这个异常:
java.util.concurrent.ExecutionException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.localhostInterpolatorHealthCheck': 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.
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:202)
我在网上读到它并不像定义自己的线程并启动它们以在 Spring 上启动线程那么简单,所以我想知道如何做到这一点?我错过了什么?
【问题讨论】:
-
根据异常消息,您的任务正在使用具有请求范围的 spring bean,而您的计划线程执行不在请求范围内。附上任务代码或描述它在做什么
-
@AdamSkywalker 它正在创建一个扩展可运行的类,将该类封装在一个线程中,并使用
Future和ExecutorService来执行限时代码块。代码块运行一系列健康测试。新类没有支持它的 bean(我认为它不是必需的),这可能是我出错的地方吗? -
我没有足够的信息,我看到代码以某种方式使用了 spring bean。你有完整的堆栈跟踪吗?现在它只显示异常的顶部。
-
@AdamSkywalker 很长。我应该通过聊天或其他方式将其发送给您吗?
-
是的,你可以使用聊天
标签: java spring multithreading spring-mvc