
com.gc.iotools.stream.base.ExecutorServiceFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of easystream Show documentation
Show all versions of easystream Show documentation
EasyStream is a small set of utilities for dealing with
streams (InputStreams
and OutputStreams).
The aim is to ease the use of
pipes when they're required.
Main features are:
* "Convert" an
OutputStream to an InputStream.
* Count the number of bytes read or
wrote to a given stream.
* While reading the data from an InputStream
copy it to a supplied
OutputStream.
* Read the content of an InputStream
multiple times or seek to a
definite position
The newest version!
package com.gc.iotools.stream.base;
/*
* Copyright (c) 2008, 2015 Gabriele Contini. This source code is released
* under the BSD License.
*/
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* This class is responsible of instantiating the right executor given an
* ExecutionModel.
*
* @author Gabriele Contini
* @since 1.0.2
* @version $Id: ExecutorServiceFactory.java 576 2015-03-28 00:03:33Z gcontini $
*/
public final class ExecutorServiceFactory {
/**
* Executor that stops after a single execution. Used when a thread per
* instance strategy is requested.
*/
private static class OneShotThreadExecutor extends
AbstractExecutorService {
private final ExecutorService exec = Executors
.newSingleThreadExecutor();
@Override
public boolean awaitTermination(final long timeout,
final TimeUnit unit) throws InterruptedException {
return this.exec.awaitTermination(timeout, unit);
}
@Override
public void execute(final Runnable command) {
this.exec.execute(command);
shutdown();
}
@Override
public boolean isShutdown() {
return this.exec.isShutdown();
}
@Override
public boolean isTerminated() {
return this.exec.isTerminated();
}
@Override
public void shutdown() {
this.exec.shutdown();
}
@Override
public List shutdownNow() {
return this.exec.shutdownNow();
}
@Override
public Future submit(final Callable task) {
final Future result = this.exec.submit(task);
shutdown();
return result;
}
}
/*
* Default should be 0 otherwise there are problems stopping application
* servers.
*/
private static ExecutorService executor = new ThreadPoolExecutor(0, 20,
5, TimeUnit.SECONDS, new ArrayBlockingQueue(500));
private static final ExecutorService SINGLE_EXECUTOR = Executors
.newSingleThreadExecutor();
/**
* Getter for the field executor
.
*
* @param tmodel a {@link com.gc.iotools.stream.base.ExecutionModel} object.
* @return a {@link java.util.concurrent.ExecutorService} object.
*/
public static ExecutorService getExecutor(final ExecutionModel tmodel) {
final ExecutorService result;
switch (tmodel) {
case THREAD_PER_INSTANCE:
result = new OneShotThreadExecutor();
break;
case STATIC_THREAD_POOL:
result = ExecutorServiceFactory.executor;
break;
case SINGLE_THREAD:
result = ExecutorServiceFactory.SINGLE_EXECUTOR;
break;
default:
throw new UnsupportedOperationException("ExecutionModel ["
+ tmodel + "] not supported");
}
return result;
}
/**
*
* Call this method to initialize the ExecutorService
that is
* used in STATIC_THREAD_POOL
execution mode.
*
*
* @see ExecutionModel#STATIC_THREAD_POOL
* @see #setDefaultThreadPoolExecutor(ExecutorService)
*/
public static void init() {
setDefaultThreadPoolExecutor(new ThreadPoolExecutor(0, 20, 5,
TimeUnit.SECONDS, new ArrayBlockingQueue(500)));
}
/**
*
* Sets the default ExecutorService returned when this class is invoked
* with {@link ExecutionModel#STATIC_THREAD_POOL}.
*
*
* It can also be used to initialize the class (for instance for use into
* a web application).
*
*
* @param executor
* ExecutorService for the STATIC_THREAD_POOL model.
*/
public static void setDefaultThreadPoolExecutor(
final ExecutorService executor) {
ExecutorServiceFactory.executor = executor;
}
/**
*
* Call this method to finalize the execution queue.
*
*
* It is mandatory when you use this library in a container, otherwise the
* container doesn't terminate gracefully (for instance you can call it in
* a web application context listener
* ContextListener.shutdown()
).
*
*/
public static void shutDown() {
executor.shutdown();
}
/**
* Users should not instantiate this class directly.
*/
private ExecutorServiceFactory() {
// don't instantiate
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy