How to use Thread-Pooling using Groovy
Grails Development is challenging and most Groovy and Grails projects require in-depth understanding of thread-pooling.
As a Groovy developer, I really like the thread-pooling API (in java.util.concurrent package), especially when you are hitting x number of API dealing with outside network to fetch information for a single request.
Example: A website dealing with some kind of ticket booking service and there are 4 different vendors (sources where you retrieve ticket status from).
Classes you need to import.
[groovy]
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.Callable
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.Future
[/groovy]
I am discussing it here in a very simple Groovish manner.
[groovy]
def myClosure = {num -> println "I Love Groovy ${num}"}
def threadPool = Executors.newFixedThreadPool(4)
try {
List<Future> futures = (1..10).collect{num->
threadPool.submit({->
myClosure num } as Callable);
}
// recommended to use following statement to ensure the execution of all tasks.
futures.each{it.get()}
}finally {
threadPool.shutdown()
}
[/groovy]
In the above example, thread pool size is 4, that means concurrent 4 tasks would be executed in a single go. And task is the code in ‘myClosure’.
Let’s say you rely on output from tasks. In such a case, you need to do the following:
[groovy]
Collection results = futures.collect{it.get()}
[/groovy]
yes! that’s very good question. My answer is simple – “this is the way you implement it”. 🙂
If I implement, I will do something like following (rough idea)..
def myClosure = {arg1 ->
def result = null
try{
result = ticketService.fetchStatus(arg1)
}catch(Throwable t){
// handle with your way.
}
}
Collection results = futures.collect{it.get()}.findAll{it}
it will give me only successful results.
Also, you can set timeout for tasks. It works very well, when you never know if third-party server is down. So you can set this time-out like 5 seconds or so.
I hope it makes sense???
For more knowledge, please see java.util.concurrent package.
-salil
So what happens when one of the tasks fail.Do you still get the results from the other tasks?