Search in sources :

Example 1 with AbstractExecutorService

use of java.util.concurrent.AbstractExecutorService in project atmosphere by Atmosphere.

the class ExecutorsFactory method getAsyncOperationExecutor.

/**
     * Create an {@link ExecutorService} to be used for dispatching I/O events.
     *
     * @param config the {@link AtmosphereConfig}
     * @param name   a name to use if shared is false.
     * @return {@link ExecutorService}
     */
public static ExecutorService getAsyncOperationExecutor(final AtmosphereConfig config, final String name) {
    final boolean shared = config.framework().isShareExecutorServices();
    if (!shared || config.properties().get(ASYNC_WRITE_THREAD_POOL) == null) {
        int numberOfAsyncThread = DEFAULT_ASYNC_THREAD;
        String s = config.getInitParameter(ApplicationConfig.BROADCASTER_ASYNC_WRITE_THREADPOOL_MAXSIZE);
        if (s != null) {
            numberOfAsyncThread = Integer.parseInt(s);
        }
        if (shared && numberOfAsyncThread == 1) {
            logger.warn("Not enough numberOfAsyncThread for a shareable thread pool {}, " + "Setting it to a newCachedThreadPool", numberOfAsyncThread);
            numberOfAsyncThread = -1;
        }
        AbstractExecutorService asyncWriteService;
        boolean useForkJoinPool = config.getInitParameter(USE_FORJOINPOOL, true);
        logger.trace("Max number of AsyncOp {}", numberOfAsyncThread == -1 ? "Unlimited" : numberOfAsyncThread);
        String threadName = name + "-AsyncOp-";
        if (numberOfAsyncThread == -1) {
            asyncWriteService = !useForkJoinPool ? (ThreadPoolExecutor) Executors.newCachedThreadPool(new AtmosphereThreadFactory(shared, threadName)) : new ForkJoinPool(shared, threadName);
        } else {
            asyncWriteService = (ThreadPoolExecutor) Executors.newFixedThreadPool(numberOfAsyncThread, new AtmosphereThreadFactory(shared, threadName));
        }
        keepAliveThreads(asyncWriteService, config);
        if (shared) {
            config.properties().put(ASYNC_WRITE_THREAD_POOL, asyncWriteService);
        }
        return asyncWriteService;
    } else {
        return (ExecutorService) config.properties().get(ASYNC_WRITE_THREAD_POOL);
    }
}
Also used : AbstractExecutorService(java.util.concurrent.AbstractExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) AbstractExecutorService(java.util.concurrent.AbstractExecutorService)

Example 2 with AbstractExecutorService

use of java.util.concurrent.AbstractExecutorService in project atmosphere by Atmosphere.

the class ExecutorsFactory method getMessageDispatcher.

/**
     * Create an {@link ExecutorService} to be used for dispatching messages, not I/O events.
     *
     * @param config the {@link AtmosphereConfig}
     * @param name   a name to use if shared is false.
     * @return {@link ExecutorService}
     */
public static ExecutorService getMessageDispatcher(final AtmosphereConfig config, final String name) {
    final boolean shared = config.framework().isShareExecutorServices();
    boolean useForkJoinPool = config.getInitParameter(USE_FORJOINPOOL, true);
    if (!shared || config.properties().get(BROADCASTER_THREAD_POOL) == null) {
        int numberOfMessageProcessingThread = DEFAULT_MESSAGE_THREAD;
        String s = config.getInitParameter(ApplicationConfig.BROADCASTER_MESSAGE_PROCESSING_THREADPOOL_MAXSIZE);
        if (s != null) {
            numberOfMessageProcessingThread = Integer.parseInt(s);
        }
        if (shared && numberOfMessageProcessingThread == 1) {
            logger.warn("Not enough numberOfMessageProcessingThread for a shareable thread pool {}, " + "Setting it to a newCachedThreadPool", numberOfMessageProcessingThread);
            numberOfMessageProcessingThread = -1;
        }
        AbstractExecutorService messageService;
        logger.trace("Max number of DispatchOp {}", numberOfMessageProcessingThread == -1 ? "Unlimited" : numberOfMessageProcessingThread);
        String threadName = name + "-DispatchOp-";
        if (numberOfMessageProcessingThread == -1) {
            messageService = !useForkJoinPool ? (ThreadPoolExecutor) Executors.newCachedThreadPool(new AtmosphereThreadFactory(shared, threadName)) : new ForkJoinPool(shared, threadName);
        } else {
            messageService = (ThreadPoolExecutor) Executors.newFixedThreadPool(numberOfMessageProcessingThread, new AtmosphereThreadFactory(shared, threadName));
        }
        keepAliveThreads(messageService, config);
        if (shared) {
            config.properties().put(BROADCASTER_THREAD_POOL, messageService);
        }
        return messageService;
    } else {
        return (ExecutorService) config.properties().get(BROADCASTER_THREAD_POOL);
    }
}
Also used : AbstractExecutorService(java.util.concurrent.AbstractExecutorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) AbstractExecutorService(java.util.concurrent.AbstractExecutorService)

Aggregations

AbstractExecutorService (java.util.concurrent.AbstractExecutorService)2 ExecutorService (java.util.concurrent.ExecutorService)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2