Downloading files concurrently
This recipe is about downloading files concurrently from the network. As for most recipes in this chapter, we will use the GPars framework to leverage the concurrent features required by the parallel downloading.
Getting ready
This recipe reuses the same build infrastructure created in the Processing collections concurrently recipe.
How to do it...
The download logic is completely encapsulated in a Groovy class.
Add a new
FileDownloader
class to thesrc/main/groovy/org/groovy/cookbook
folder:package org.groovy.cookbook import static groovyx.gpars.GParsPool.* import static com.google.common.collect.Lists.* class FileDownloader { static final int POOL_SIZE = 25 static pool FileDownloader() { pool = createPool(POOL_SIZE) } private void downloadFile(String remoteUrl, String localUrl) { new File("$localUrl").withOutputStream { out -> new URL(remoteUrl).withInputStream { from -> out << from } } } private void parallelDownload...