2012-06-18 61 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放在我的服務類中,我很好! –