Error on async job

前端 未结 4 1280
轻奢々
轻奢々 2021-02-20 16:39

I\'m trying to create an async task that will not block the request. The user make the request, the task will start, and the controller will render \"Job is running...\", this i

4条回答
  •  梦谈多话
    2021-02-20 16:48

    In my case, just returning a promise worked.

    MyService.groovy

    import static grails.async.Promises.*
    def getAsync(){
        Promise p = task {
        //Long running task
                println 'John doe started digging a hole here.'
                Thread.sleep(2000)
                println 'John doe working......'
                return 'Kudos John Doe!'
            }
            p.onError { Throwable err ->
                println "Poor John"+err
            }
            p.onComplete { result ->
                println "Congrats." +result
            }
    
            println 'John Doe is doing something here.'
            return p
    }
    

提交回复
热议问题