【发布时间】:2017-11-04 12:54:42
【问题描述】:
我正在使用 Grails 2.5.6。
我有一个以前连续执行的任务列表,需要引用我在会话中拥有的“lang”属性。
现在我从控制器调用一个创建线程的类,每个任务一个线程来加速执行。添加线程后,当代码尝试访问会话时开始出现此错误:
No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the o
riginally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outs
ide of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
我认为需要在我创建的线程中设置一些东西才能访问请求/会话,但找不到任何文档。
会话的访问方式是:
RequestContextHolder.currentRequestAttributes().session.lang
在添加线程之前这工作正常。
有什么想法可以让那行代码在新线程上工作吗?
顺便说一句,我尝试了两种创建线程的方法,经典的 Thread.start/treads*.join() 和使用 grails.async.Promises (http://docs.grails.org/2.5.6/guide/async.html)。在两者上我都会收到相同的错误消息。
【问题讨论】:
-
stackoverflow.com/questions/43250539/… 但根据下面可能需要克隆参数或 properties.collect{it} 所以你将它捕获为一个新列表并尝试将元素存储在一个新的平面地图中通过这个过程发送它 - 听起来快速或懒惰的方式有时会咬人 - 如果只有 session.lang 为什么不抓住它并让它 map.something=params.lang 或 session.lang 就是我的意思
标签: multithreading session grails