All Downloads are FREE. Search and download functionalities are using the official Maven repository.

woko.async.JobManager Maven / Gradle / Ivy

The newest version!
package woko.async;

import woko.Closeable;
import woko.util.WLogger;

import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * Top-level component for managing async {@link Job}s.
 */
public class JobManager implements Closeable {

    public static final String KEY = "JobManager";

    private static final WLogger logger = WLogger.getLogger(JobManager.class);

    private final ExecutorService pool;
    private final Map runningJobs = new ConcurrentHashMap();

    /**
     * Create with a single-threaded executor.
     */
    public JobManager() {
        this(Executors.newSingleThreadExecutor());
    }

    /**
     * Create with passed ExecutorService.
     *
     * @param pool the ExecutorService to be used
     */
    public JobManager(ExecutorService pool) {
        logger.info("Created with ExecutorService : " + pool);
        this.pool = pool;
    }

    /**
     * Submit and execute a new Job, and notify all listeners.
     *
     * @param job       the Job to execute
     * @param listeners a list of the listeners to be notified during job execution
     */
    public void submit(final Job job, final List listeners) {
        logger.debug("Submitting Job : " + job + " with listeners : " + listeners);
        pool.submit(new Callable() {
            @Override
            public Object call() throws Exception {
                runningJobs.put(job.getUuid(), job);
                try {
                    job.execute(listeners);
                    return null;
                } finally {
                    runningJobs.remove(job.getUuid());
                }
            }
        });
    }

    /**
     * Shutdown the ExecutorService passed at construction time.
     */
    public void close() {
        logger.debug("Closing pool : " + pool);
        pool.shutdown(); // Disable new tasks from being submitted
        try {
            // Wait a while for existing tasks to terminate
            if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
                pool.shutdownNow(); // Cancel currently executing tasks
                // Wait a while for tasks to respond to being cancelled
                if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
                    throw new IllegalStateException("pool not shutdown, tasks still running ?");
                }
            }
        } catch (InterruptedException ie) {
            // (Re-)Cancel if current thread also interrupted
            pool.shutdownNow();
            // Preserve interrupt status
            Thread.currentThread().interrupt();
        }
    }

    /**
     * Return a running Job by its uuid. Returns null if
     * no job is currently running with passed uuid.
     *
     * @param uuid the uuid of the Job to retrieve
     * @return the Job if any, null otherwise
     */
    public Job getRunningJob(String uuid) {
        return runningJobs.get(uuid);
    }
}