diff --git a/src/main/java/com/alexnederlof/jasperreport/JasperReporter.java b/src/main/java/com/alexnederlof/jasperreport/JasperReporter.java index ed6fe63..df26f8e 100644 --- a/src/main/java/com/alexnederlof/jasperreport/JasperReporter.java +++ b/src/main/java/com/alexnederlof/jasperreport/JasperReporter.java @@ -22,8 +22,11 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.atomic.AtomicInteger; import net.sf.jasperreports.engine.DefaultJasperReportsContext; import net.sf.jasperreports.engine.JRException; @@ -409,10 +412,11 @@ private void createDestination(File destinationDirectory) throws MojoExecutionEx } private void executeTasks(List tasks) throws MojoExecutionException { + ExecutorService threadPool = newThreadPool(); try { long t1 = System.currentTimeMillis(); List> output = - Executors.newFixedThreadPool(numberOfThreads).invokeAll(tasks); + threadPool.invokeAll(tasks); long time = (System.currentTimeMillis() - t1); log.info("Generated " + output.size() + " jasper reports in " + (time / 1000.0) + " seconds"); checkForExceptions(output); @@ -429,6 +433,13 @@ private void executeTasks(List tasks) throws MojoExecutionException throw new MojoExecutionException("Error while compiling Jasper reports", e); } } + finally { + threadPool.shutdown(); + } + } + + private ExecutorService newThreadPool() { + return Executors.newFixedThreadPool(numberOfThreads, new JasperReporterThreadFactory()); } private void checkForExceptions(List> output) throws InterruptedException, ExecutionException { @@ -454,4 +465,20 @@ private boolean isSkip() { return skip; } + + /** + * Thread factory the compile threads. Sets the thread name and marks it as a daemon thread. + */ + private static final class JasperReporterThreadFactory implements ThreadFactory { + + private static final AtomicInteger THREAD_COUNTER = new AtomicInteger(); + + @Override + public Thread newThread(Runnable r) { + Thread thread = new Thread(r, "jasper-compiler-" + THREAD_COUNTER.incrementAndGet()); + thread.setDaemon(true); + return thread; + } + + } }