2012-06-18 67 views
1

是否可以在grails引导类中异步使用服务? 我尝试做以下Grails的-2.0.4和Grails的执行人 - 插件,但只出现第一个日志消息:grails异步引导

class BootStrap { 

def myService 

def init = { servletContext -> 

    log.info("Bootstrapping") 

    runAsync { 
     log.info("Doing myService async ") 
     myService.doSomething() 
    } 

} 

没有错误消息,只是没有从第二日志输出声明。 非常感谢!

+0

如何启动一个线程?我记得我在一个项目中做过这个,但我不记得我是否使用了类服务? –

+0

是啊!好主意:我试过如下:高清TH = {Thread.start \t \t log.info( “Autowarming印痕异步”) \t \t myService.doSomething() \t \t} –

+0

它的工作原理或不? –

回答

2

删除runAsync关闭 - 它是不正确的地方。您可以使用封样productiondevelopment这里针对不同的环境:

class BootStrap { 

def myService 

def init = { servletContext -> 
    log.info("Bootstrapping") 
    development { 
     log.info("Doing myService async ") 
     myService.doSomething() 
    } 
} 

class MyService { 
    def doSomething() { 
     runAsync { 
      // executed asynchronously 
     } 
    } 
} 
+0

@WolfWetzel我已经更新了我的答案。 'runAsyc'在Bootstrap中不起作用,因为它没有被扩展。只有域,控制器和服务被扩展。检查我的解决方案,不要开始你自己的线程。 –

+0

非常感谢 - 您的建议正在发挥作用。我把runAsync-Closure放在我的服务类中,我很好! –