/*
 *  The MIT License
 *
 *   Copyright (c) 2016, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

package org.easybatch.core.job;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import static java.lang.Runtime.getRuntime;
import static java.util.concurrent.Executors.newFixedThreadPool;

/**
 * Main class to execute {@link Job}s.
 *
 * @author Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com)
 */
public class JobExecutor {

    private ExecutorService executorService;

    /**
     * Create a job executor. The number of workers will be set to the number of available processors.
     */
    public JobExecutor() {
        executorService = newFixedThreadPool(getRuntime().availableProcessors());
    }

    /**
     * Create a job executor.
     *
     * @param nbWorkers number of worker threads
     */
    public JobExecutor(int nbWorkers) {
        executorService = newFixedThreadPool(nbWorkers);
    }

    /**
     * Execute a job.
     *
     * @param job the job to execute
     * @return the job execution report
     */
    public JobReport execute(Job job) {
        try {
            return executorService.submit(job).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException("Unable to execute job " + job.getName());
        }
    }

    /**
     * Submit a job for execution.
     *
     * @param job to execute
     * @return the job report
     */
    public Future<JobReport> submit(Job job) {
        return executorService.submit(job);
    }

    /**
     * Shutdown the job executor.
     */
    public void shutdown() {
        executorService.shutdown();
    }

}

@@ -1239,16 +1239,97 @@
 e.job;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A
 import j
@@ -1368,16 +1368,16 @@
 eption;%0A
-
 import j
@@ -2216,16 +2216,30 @@
 te a job
+ synchronously
 .%0A     *
@@ -2303,18 +2303,8 @@
 job 
-execution 
 repo
@@ -2617,16 +2617,29 @@
 job for 
+asynchronous 
 executio
@@ -2754,32 +2754,32 @@
 bmit(Job job) %7B%0A
-
         return e
@@ -2805,32 +2805,503 @@
 it(job);%0A    %7D%0A%0A
+    /**%0A     * Submit jobs for execution.%0A     *%0A     * @param jobs to execute%0A     * @return the list of job reports%0A     */%0A    public List%3CFuture%3CJobReport%3E%3E submitAll(Job... jobs) %7B%0A        List%3CJob%3E jobList = new ArrayList%3C%3E();%0A        Collections.addAll(jobList, jobs);%0A        try %7B%0A            return executorService.invokeAll(jobList);%0A        %7D catch (InterruptedException e) %7B%0A            throw new RuntimeException(%22Unable to execute jobs%22);%0A        %7D%0A    %7D%0A%0A
     /**%0A     * S

add method to execute to set of jobs in parallel