groovygpars

List multiprocessing in groovy


I have a list of URLs in groovy for which I would like to start a new thread for each. I would like to know the groovy way of doing this list multiprocessing. Suppose I have a list of urls I wish to fetch data from concurrently.

def urls = ["https://stackexchange.com","https://amazon.com","https://bing.com","https://google.com"]

Solution

  • import groovyx.gpars.GParsPool
    
    def urls = ["https://stackexchange.com","https://amazon.com","https://bing.com","https://google.com"]
    
    GParsPool.withPool( urls.size() ) { urls.eachParallel { url ->
    try {
            // test if url is reachable
            response = url.toURL().text
            println url + " connected OK"
        } 
    catch (Exception e) {  println e }
        }
    }