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

com.argo.service.pool.TrackingThreadPoolTaskExecutor Maven / Gradle / Ivy

The newest version!
package com.argo.service.pool;

import org.springframework.core.task.TaskRejectedException;
import org.springframework.scheduling.SchedulingTaskExecutor;
import org.springframework.scheduling.concurrent.ExecutorConfigurationSupport;
import org.springframework.util.Assert;

import java.util.concurrent.*;

/**
 * Created by user on 1/25/15.
 */
public class TrackingThreadPoolTaskExecutor extends ExecutorConfigurationSupport implements SchedulingTaskExecutor {

    private final Object poolSizeMonitor = new Object();

    private int corePoolSize = 1;

    private int maxPoolSize = Integer.MAX_VALUE;

    private int keepAliveSeconds = 60;

    private boolean allowCoreThreadTimeOut = false;

    private int queueCapacity = Integer.MAX_VALUE;

    private ThreadPoolExecutor threadPoolExecutor;


    /**
     * Set the ThreadPoolExecutor's core pool size.
     * Default is 1.
     * 

This setting can be modified at runtime, for example through JMX. */ public void setCorePoolSize(int corePoolSize) { synchronized (this.poolSizeMonitor) { this.corePoolSize = corePoolSize; if (this.threadPoolExecutor != null) { this.threadPoolExecutor.setCorePoolSize(corePoolSize); } } } /** * Return the ThreadPoolExecutor's core pool size. */ public int getCorePoolSize() { synchronized (this.poolSizeMonitor) { return this.corePoolSize; } } /** * Set the ThreadPoolExecutor's maximum pool size. * Default is {@code Integer.MAX_VALUE}. *

This setting can be modified at runtime, for example through JMX. */ public void setMaxPoolSize(int maxPoolSize) { synchronized (this.poolSizeMonitor) { this.maxPoolSize = maxPoolSize; if (this.threadPoolExecutor != null) { this.threadPoolExecutor.setMaximumPoolSize(maxPoolSize); } } } /** * Return the ThreadPoolExecutor's maximum pool size. */ public int getMaxPoolSize() { synchronized (this.poolSizeMonitor) { return this.maxPoolSize; } } /** * Set the ThreadPoolExecutor's keep-alive seconds. * Default is 60. *

This setting can be modified at runtime, for example through JMX. */ public void setKeepAliveSeconds(int keepAliveSeconds) { synchronized (this.poolSizeMonitor) { this.keepAliveSeconds = keepAliveSeconds; if (this.threadPoolExecutor != null) { this.threadPoolExecutor.setKeepAliveTime(keepAliveSeconds, TimeUnit.SECONDS); } } } /** * Return the ThreadPoolExecutor's keep-alive seconds. */ public int getKeepAliveSeconds() { synchronized (this.poolSizeMonitor) { return this.keepAliveSeconds; } } /** * Specify whether to allow core threads to time out. This enables dynamic * growing and shrinking even in combination with a non-zero queue (since * the max pool size will only grow once the queue is full). *

Default is "false". Note that this feature is only available on Java 6 * or above. On Java 5, consider switching to the backport-concurrent * version of ThreadPoolTaskExecutor which also supports this feature. * @see ThreadPoolExecutor#allowCoreThreadTimeOut(boolean) */ public void setAllowCoreThreadTimeOut(boolean allowCoreThreadTimeOut) { this.allowCoreThreadTimeOut = allowCoreThreadTimeOut; } /** * Set the capacity for the ThreadPoolExecutor's BlockingQueue. * Default is {@code Integer.MAX_VALUE}. *

Any positive value will lead to a LinkedBlockingQueue instance; * any other value will lead to a SynchronousQueue instance. * @see LinkedBlockingQueue * @see SynchronousQueue */ public void setQueueCapacity(int queueCapacity) { this.queueCapacity = queueCapacity; } protected ExecutorService initializeExecutor( ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) { BlockingQueue queue = createQueue(this.queueCapacity); ThreadPoolExecutor executor = new TrackingThreadPoolExecutor( this.corePoolSize, this.maxPoolSize, this.keepAliveSeconds, TimeUnit.SECONDS, queue, threadFactory, rejectedExecutionHandler); if (this.allowCoreThreadTimeOut) { executor.allowCoreThreadTimeOut(true); } this.threadPoolExecutor = executor; return executor; } /** * Create the BlockingQueue to use for the ThreadPoolExecutor. *

A LinkedBlockingQueue instance will be created for a positive * capacity value; a SynchronousQueue else. * @param queueCapacity the specified queue capacity * @return the BlockingQueue instance * @see LinkedBlockingQueue * @see SynchronousQueue */ protected BlockingQueue createQueue(int queueCapacity) { if (queueCapacity > 0) { return new LinkedBlockingQueue(queueCapacity); } else { return new SynchronousQueue(); } } /** * Return the underlying ThreadPoolExecutor for native access. * @return the underlying ThreadPoolExecutor (never {@code null}) * @throws IllegalStateException if the ThreadPoolTaskExecutor hasn't been initialized yet */ public ThreadPoolExecutor getThreadPoolExecutor() throws IllegalStateException { Assert.state(this.threadPoolExecutor != null, "ThreadPoolTaskExecutor not initialized"); return this.threadPoolExecutor; } /** * Return the current pool size. * @see ThreadPoolExecutor#getPoolSize() */ public int getPoolSize() { return getThreadPoolExecutor().getPoolSize(); } /** * Return the number of currently active threads. * @see ThreadPoolExecutor#getActiveCount() */ public int getActiveCount() { return getThreadPoolExecutor().getActiveCount(); } public void execute(Runnable task) { Executor executor = getThreadPoolExecutor(); try { executor.execute(task); } catch (RejectedExecutionException ex) { throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex); } } public void execute(Runnable task, long startTimeout) { execute(task); } public Future submit(Runnable task) { ExecutorService executor = getThreadPoolExecutor(); try { return executor.submit(task); } catch (RejectedExecutionException ex) { throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex); } } public Future submit(Callable task) { ExecutorService executor = getThreadPoolExecutor(); try { return executor.submit(task); } catch (RejectedExecutionException ex) { throw new TaskRejectedException("Executor [" + executor + "] did not accept task: " + task, ex); } } /** * This task executor prefers short-lived work units. */ public boolean prefersShortLivedTasks() { return true; } @Override public void afterPropertiesSet() { super.afterPropertiesSet(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy