Hao Tan Hao Tan - 3 months ago 28
Groovy Question

Groovy XmlSlurper with Parallel Operation

I am parsing some rss xml feed and need to expand some urls in the description field.

Now my code is written as

items.collect {
it.description = FullText.expand(it.description)
return it
}


In this case, the urls inside are requested one by one, making its process very slow.

So I want to do something like

items.collectParallel {
it.description = FullText.expand(it.description)
return it
}


But instead I get the error messageļ¼š

groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.NodeChildren.collectParallel() is applicable for argument types

Answer

The items.collectParallel block needs to be surrounded by a GParsPool.withPool block to have the collectParallel and other GPars methods be available, like:

import static groovyx.gpars.GParsPool.withPool

// ...

withPool {
    items.collectParallel {
        it.description = FullText.expand(it.description)
        return it
    }
}
Comments