Groovy / grails:将参数传递给线程

时间:2014-06-03 11:40:10

标签: multithreading grails groovy

在grails控制器中启动一个新线程(task / Promise)。但是当我尝试在线程中获取它时,用户为null。

if(springSecurityService.currentUser){
                task {
                    // do something with the current user
                    // springSecurityService.currentUser is null here!!!!!!
                }.get()
            }

如何将用户作为参数传递给任务?

1 个答案:

答案 0 :(得分:6)

问题不在于“线程是在用户离开控制器之后启动的”,而是springSecurityService.currentUser是线程本地状态,当从不同线程调用时,它给出不同的值。但是如果你在局部变量中保存 springSecurityService.currentUser的这个线程的值,你应该能够访问闭包内的那个:

if(springSecurityService.currentUser){
   def theUser = springSecurityService.currentUser
   task {
       // do something with theUser
   }.get()
}