在Grails的异步编程 [英] Asynchronous Programming in Grails

查看:883
本文介绍了在Grails的异步编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的Grails应用程序的Servlet 3.0异步呈现。而我得到了下面的错误。

  |错误2014年4月29日11:10:24,125 [男演员线程28]错误gpars.LoggingPoolFactory  - 异步执行错误:空
消息:空
    线|方法
- >> 61 | doCall在org.grails.async.factory.gpars.GparsPromise $ _onComplete_closure1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 62 |运行groovyx.gpars.dataflow.DataCallback $ 1
| 1145 | runWorker在java.util.concurrent.ThreadPoolExecutor中
| 615 |运行java.util.concurrent.ThreadPoolExecutor中的$工人
^ 724 |跑 。 。 。在java.lang.Thread中

请帮我解决这个问题。

下面是code

  //承诺1
    无极P1 = {任务
        的println ID +P1任务是乳宁
        睡眠(4000)
        的println ID +P1线程醒了
        返回p1Completed
    }    //无极2
    无极P2 = {任务
        的println ID +P2任务是乳宁
        睡眠(4000)
        的println ID +P2线程醒了
        返回p2Completed
    }
    p1.onComplete {结果 - >
        的println ID +无极完成返回P1+结果
    }
    p1.onError {Throwable的错误 - >
        的println ID +P1时出错$ {} err.message
    }
    p2.onComplete {结果 - >
        的println ID +无极完成返回P2+结果
    }
    p2.onError {Throwable的错误 - >
        的println ID +P2时出错$ {} err.message
    }


解决方案

您需要在您的code的后下方添加此行:

  p1.get()
p2.get()

直到两个承诺完成这将阻止该请求。如果你想运行的异步任务,并最终没有同步等待响应的请求,则需要使用java执行人框架。检查这个问题:在异步工作错误

I am using Servlet 3.0 Asynchronous Rendering in my Grails application. And I am getting the following Error.

| Error 2014-04-29 11:10:24,125 [Actor Thread 28] ERROR gpars.LoggingPoolFactory  - Async execution error: null
Message: null
    Line | Method
->>   61 | doCall    in org.grails.async.factory.gpars.GparsPromise$_onComplete_closure1
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|     62 | run       in groovyx.gpars.dataflow.DataCallback$1
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    724 | run . . . in java.lang.Thread

Please help me to solve this issue.

Here is the Code

    //Promise 1
    Promise p1 = task {
        println id+" p1 Task is Runing"
        sleep(4000) 
        println id+" p1 Thread Woke Up"
        return "p1Completed"
    }

    //Promise 2
    Promise p2 = task {
        println id+" p2 Task is Runing"    
        sleep(4000)
        println id+" p2 Thread Woke Up"
        return "p2Completed"
    }
    p1.onComplete { result ->
        println id+" Promise p1 Completed returned "+result  
    }
    p1.onError { Throwable err ->
        println id+" p1 An error occured ${err.message}"
    }
    p2.onComplete { result ->
        println id+" Promise p2 Completed returned "+result
    }
    p2.onError { Throwable err ->
        println id+" p2 An error occured ${err.message}"
    }

解决方案

You need to add this lines after at the bottom of your code:

p1.get()
p2.get()

This will block the request until both promises are complete. If you want to run the async tasks and end the request without synchronously wait for the response, you will need to use the java executor framework. Check this question: Error on async job

这篇关于在Grails的异步编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆