Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous Programming in Grails

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}"
    }
like image 968
Syed Mohammad Ali Avatar asked Nov 02 '22 00:11

Syed Mohammad Ali


1 Answers

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

like image 55
agusluc Avatar answered Nov 11 '22 16:11

agusluc