【发布时间】:2017-07-10 11:13:54
【问题描述】:
我有下一个流程要在我的 Spring MVC Web 应用程序中实现:
- 用户在页面上插入一些值,即向服务器发送 POST
- 基于此值,控制器启动一个长时间运行的后台任务,即非常繁重(大约一小时左右),因此绝对需要异步。
- 在此任务期间,需要读取/写入数据库(在我的例子中是 Neo4j)
我是怎么做到的:
- 我们在“会话”范围内有 bean:
@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public Session getSession() throws Exception {
return super.getSession();
}
- 在我的控制器中,我有一些数据库存储库:
@Autowired
SomeDBRepo repo;
- 我有一个方法,由@Async 注释
@Async
doSomeAsync() {
repo.findAll();
}
我收到此错误:
Error creating bean with name 'scopedTarget.getSession': Scope 'session' 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.
我已经阅读了一些关于这个问题的文章,但对我没有任何帮助。这个问题怎么解决?
【问题讨论】:
-
您是否将@EnableAsync 添加到配置类中?
-
我认为answer 会帮助你...
-
是的,我添加了@EnableAsync
-
为什么在长时间运行的任务中需要范围会话?不能只是一个单例吗?
-
您使用的是哪个版本的 SDN?不再需要声明 Session bean。请使用 4.2.4 并参考此文档进行配置:docs.spring.io/spring-data/data-neo4j/docs/4.2.4.RELEASE 您应该会发现 @Async 请求工作正常。
标签: java spring spring-mvc spring-data spring-data-neo4j