org.apache.tomcat.util.threads.ThreadPoolExecutor Java Examples

The following examples show how to use org.apache.tomcat.util.threads.ThreadPoolExecutor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServerConfiguration.java    From livingdoc-core with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onApplicationEvent(ContextClosedEvent event) {
    this.connector.pause();
    log.info("Shutting Down LivingDoc Remote Agent");
    Executor executor = this.connector.getProtocolHandler().getExecutor();
    if (executor instanceof ThreadPoolExecutor) {
        try {
            ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
            threadPoolExecutor.shutdown();
            if (!threadPoolExecutor.awaitTermination(30, TimeUnit.SECONDS)) {
                log.warn("Tomcat thread pool did not shut down gracefully within "
                        + "30 seconds. Proceeding with forceful shutdown");
            }
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
}
 
Example #2
Source File: AbstractEndpoint.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
public void shutdownExecutor() {
    if ( executor!=null && internalExecutor ) {
        if ( executor instanceof ThreadPoolExecutor ) {
            //this is our internal one, so we need to shut it down
            ThreadPoolExecutor tpe = (ThreadPoolExecutor) executor;
            tpe.shutdownNow();
            long timeout = getExecutorTerminationTimeoutMillis();
            if (timeout > 0) {
                try {
                    tpe.awaitTermination(timeout, TimeUnit.MILLISECONDS);
                } catch (InterruptedException e) {
                    // Ignore
                }
                if (tpe.isTerminating()) {
                    getLog().warn(sm.getString("endpoint.warn.executorShutdown", getName()));
                }
            }
            TaskQueue queue = (TaskQueue) tpe.getQueue();
            queue.setParent(null);
        }
        executor = null;
    }
}
 
Example #3
Source File: StandardThreadExecutor.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Start the component and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 *
 *
 */
@Override
protected void startInternal() throws LifecycleException {
    //重写了LinkedBlockingQueue的offer方法。
    taskqueue = new TaskQueue(maxQueueSize);
    TaskThreadFactory tf = new TaskThreadFactory(namePrefix,daemon,getThreadPriority());
    //此ThreadPoolExecutor非彼ThreadPoolExecutor。
    executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS,taskqueue, tf);
    executor.setThreadRenewalDelay(threadRenewalDelay);
    if (prestartminSpareThreads) {
        executor.prestartAllCoreThreads();
    }
    taskqueue.setParent(executor);

    setState(LifecycleState.STARTING);
}
 
Example #4
Source File: AbstractEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
public void shutdownExecutor() {
    if ( executor!=null && internalExecutor ) {
        if ( executor instanceof ThreadPoolExecutor ) {
            //this is our internal one, so we need to shut it down
            ThreadPoolExecutor tpe = (ThreadPoolExecutor) executor;
            tpe.shutdownNow();
            long timeout = getExecutorTerminationTimeoutMillis();
            if (timeout > 0) {
                try {
                    tpe.awaitTermination(timeout, TimeUnit.MILLISECONDS);
                } catch (InterruptedException e) {
                    // Ignore
                }
                if (tpe.isTerminating()) {
                    getLog().warn(sm.getString("endpoint.warn.executorShutdown", getName()));
                }
            }
            TaskQueue queue = (TaskQueue) tpe.getQueue();
            queue.setParent(null);
        }
        executor = null;
    }
}
 
Example #5
Source File: AbstractEndpoint.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * 关闭线程池
 */
public void shutdownExecutor() {
    Executor executor = this.executor;
    if (executor != null && internalExecutor) {
        this.executor = null;
        if (executor instanceof ThreadPoolExecutor) {
            //this is our internal one, so we need to shut it down
            ThreadPoolExecutor tpe = (ThreadPoolExecutor) executor;
            tpe.shutdownNow();
            long timeout = getExecutorTerminationTimeoutMillis();
            if (timeout > 0) {
                try {
                    tpe.awaitTermination(timeout, TimeUnit.MILLISECONDS);
                } catch (InterruptedException e) {
                    // Ignore
                }
                if (tpe.isTerminating()) {
                    getLog().warn(sm.getString("endpoint.warn.executorShutdown", getName()));
                }
            }
            TaskQueue queue = (TaskQueue) tpe.getQueue();
            queue.setParent(null);
        }
    }
}
 
Example #6
Source File: AbstractEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
protected int getMaxThreadsExecutor(boolean useExecutor) {
    if (useExecutor && executor != null) {
        if (executor instanceof java.util.concurrent.ThreadPoolExecutor) {
            return ((java.util.concurrent.ThreadPoolExecutor)executor).getMaximumPoolSize();
        } else if (executor instanceof ResizableExecutor) {
            return ((ResizableExecutor)executor).getMaxThreads();
        } else {
            return -1;
        }
    } else {
        return maxThreads;
    }
}
 
Example #7
Source File: AbstractEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Return the amount of threads that are managed by the pool.
 *
 * @return the amount of threads that are managed by the pool
 */
public int getCurrentThreadCount() {
    if (executor!=null) {
        if (executor instanceof ThreadPoolExecutor) {
            return ((ThreadPoolExecutor)executor).getPoolSize();
        } else if (executor instanceof ResizableExecutor) {
            return ((ResizableExecutor)executor).getPoolSize();
        } else {
            return -1;
        }
    } else {
        return -2;
    }
}
 
Example #8
Source File: AbstractEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Return the amount of threads that are in use
 *
 * @return the amount of threads that are in use
 */
public int getCurrentThreadsBusy() {
    if (executor!=null) {
        if (executor instanceof ThreadPoolExecutor) {
            return ((ThreadPoolExecutor)executor).getActiveCount();
        } else if (executor instanceof ResizableExecutor) {
            return ((ResizableExecutor)executor).getActiveCount();
        } else {
            return -1;
        }
    } else {
        return -2;
    }
}
 
Example #9
Source File: AbstractEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public void createExecutor() {
    internalExecutor = true;
    TaskQueue taskqueue = new TaskQueue();
    TaskThreadFactory tf = new TaskThreadFactory(getName() + "-exec-", daemon, getThreadPriority());
    executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), 60, TimeUnit.SECONDS,taskqueue, tf);
    taskqueue.setParent( (ThreadPoolExecutor) executor);
}
 
Example #10
Source File: TomcatGracefulShutdown.java    From loc-framework with MIT License 5 votes vote down vote up
@Override
public void onApplicationEvent(final ContextClosedEvent event) {
  if (connector == null) {
    log.info("We are running unit test ... ");
    return;
  }
  final Executor executor = connector.getProtocolHandler().getExecutor();
  if (executor instanceof ThreadPoolExecutor) {
    log.info("executor is ThreadPoolExecutor");
    final ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
    if (threadPoolExecutor.isTerminated()) {
      log.info("thread pool executor has terminated");
    } else {
      LocalDateTime startShutdown = LocalDateTime.now();
      LocalDateTime stopShutdown = LocalDateTime.now();

      try {
        threadPoolExecutor.shutdown();
        if (!threadPoolExecutor
            .awaitTermination(tomcatGracefulShutdownProperties.getWaitTime(), TimeUnit.SECONDS)) {
          log.warn("Tomcat thread pool did not shut down gracefully within "
              + tomcatGracefulShutdownProperties
              .getWaitTime() + " second(s). Proceeding with force shutdown");
          threadPoolExecutor.shutdownNow();
        } else {
          log.info("Tomcat thread pool is empty, we stop now");
        }
      } catch (final InterruptedException ex) {
        log.error("The await termination has been interrupted : " + ex.getMessage());
        Thread.currentThread().interrupt();
      } finally {
        final long seconds = Duration.between(startShutdown, stopShutdown).getSeconds();
        log.info("Shutdown performed in " + seconds + " second(s)");
      }
    }
  }
}
 
Example #11
Source File: StandardThreadExecutor.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Start the component and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected void startInternal() throws LifecycleException {

    taskqueue = new TaskQueue(maxQueueSize);
    TaskThreadFactory tf = new TaskThreadFactory(namePrefix,daemon,getThreadPriority());
    executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS,taskqueue, tf);
    executor.setThreadRenewalDelay(threadRenewalDelay);
    if (prestartminSpareThreads) {
        executor.prestartAllCoreThreads();
    }
    taskqueue.setParent(executor);

    setState(LifecycleState.STARTING);
}
 
Example #12
Source File: ThreadLocalLeakPreventionListener.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Updates each ThreadPoolExecutor with the current time, which is the time
 * when a context is being stopped.
 * 
 * @param context
 *            the context being stopped, used to discover all the Connectors
 *            of its parent Service.
 */
private void stopIdleThreads(Context context) {
    if (serverStopping) return;

    if (!(context instanceof StandardContext) ||
        !((StandardContext) context).getRenewThreadsWhenStoppingContext()) {
        log.debug("Not renewing threads when the context is stopping. "
            + "It is not configured to do it.");
        return;
    }

    Engine engine = (Engine) context.getParent().getParent();
    Service service = engine.getService();
    Connector[] connectors = service.findConnectors();
    if (connectors != null) {
        for (Connector connector : connectors) {
            ProtocolHandler handler = connector.getProtocolHandler();
            Executor executor = null;
            if (handler != null) {
                executor = handler.getExecutor();
            }

            if (executor instanceof ThreadPoolExecutor) {
                ThreadPoolExecutor threadPoolExecutor =
                    (ThreadPoolExecutor) executor;
                threadPoolExecutor.contextStopping();
            } else if (executor instanceof StandardThreadExecutor) {
                StandardThreadExecutor stdThreadExecutor =
                    (StandardThreadExecutor) executor;
                stdThreadExecutor.contextStopping();
            }

        }
    }
}
 
Example #13
Source File: AsyncChannelGroupUtil.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private static AsynchronousChannelGroup createAsynchronousChannelGroup() {
    // Need to do this with the right thread context class loader else the
    // first web app to call this will trigger a leak
    ClassLoader original = Thread.currentThread().getContextClassLoader();

    try {
        Thread.currentThread().setContextClassLoader(
                AsyncIOThreadFactory.class.getClassLoader());

        // These are the same settings as the default
        // AsynchronousChannelGroup
        int initialSize = Runtime.getRuntime().availableProcessors();
        ExecutorService executorService = new ThreadPoolExecutor(
                0,
                Integer.MAX_VALUE,
                Long.MAX_VALUE, TimeUnit.MILLISECONDS,
                new SynchronousQueue<Runnable>(),
                new AsyncIOThreadFactory());

        try {
            return AsynchronousChannelGroup.withCachedThreadPool(
                    executorService, initialSize);
        } catch (IOException e) {
            // No good reason for this to happen.
            throw new IllegalStateException(sm.getString("asyncChannelGroup.createFail"));
        }
    } finally {
        Thread.currentThread().setContextClassLoader(original);
    }
}
 
Example #14
Source File: AbstractEndpoint.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void setMinSpareThreads(int minSpareThreads) {
    this.minSpareThreads = minSpareThreads;
    Executor executor = this.executor;
    if (internalExecutor && executor instanceof java.util.concurrent.ThreadPoolExecutor) {
        // The internal executor should always be an instance of
        // j.u.c.ThreadPoolExecutor but it may be null if the endpoint is
        // not running.
        // This check also avoids various threading issues.
        ((java.util.concurrent.ThreadPoolExecutor) executor).setCorePoolSize(minSpareThreads);
    }
}
 
Example #15
Source File: AbstractEndpoint.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void setMaxThreads(int maxThreads) {
    this.maxThreads = maxThreads;
    Executor executor = this.executor;
    if (internalExecutor && executor instanceof java.util.concurrent.ThreadPoolExecutor) {
        // The internal executor should always be an instance of
        // j.u.c.ThreadPoolExecutor but it may be null if the endpoint is
        // not running.
        // This check also avoids various threading issues.
        ((java.util.concurrent.ThreadPoolExecutor) executor).setMaximumPoolSize(maxThreads);
    }
}
 
Example #16
Source File: AbstractEndpoint.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Return the amount of threads that are managed by the pool.
 *
 * @return the amount of threads that are managed by the pool
 */
public int getCurrentThreadCount() {
    if (executor!=null) {
        if (executor instanceof ThreadPoolExecutor) {
            return ((ThreadPoolExecutor)executor).getPoolSize();
        } else if (executor instanceof ResizableExecutor) {
            return ((ResizableExecutor)executor).getPoolSize();
        } else {
            return -1;
        }
    } else {
        return -2;
    }
}
 
Example #17
Source File: AbstractEndpoint.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Return the amount of threads that are in use
 *
 * @return the amount of threads that are in use
 */
public int getCurrentThreadsBusy() {
    if (executor!=null) {
        if (executor instanceof ThreadPoolExecutor) {
            return ((ThreadPoolExecutor)executor).getActiveCount();
        } else if (executor instanceof ResizableExecutor) {
            return ((ResizableExecutor)executor).getActiveCount();
        } else {
            return -1;
        }
    } else {
        return -2;
    }
}
 
Example #18
Source File: AbstractEndpoint.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void createExecutor() {
    internalExecutor = true;
    TaskQueue taskqueue = new TaskQueue();
    TaskThreadFactory tf = new TaskThreadFactory(getName() + "-exec-", daemon, getThreadPriority());
    executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), 60, TimeUnit.SECONDS,taskqueue, tf);
    taskqueue.setParent( (ThreadPoolExecutor) executor);
}
 
Example #19
Source File: AbstractEndpoint.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public void setMinSpareThreads(int minSpareThreads) {
    this.minSpareThreads = minSpareThreads;
    if (running && executor!=null) {
        if (executor instanceof java.util.concurrent.ThreadPoolExecutor) {
            ((java.util.concurrent.ThreadPoolExecutor)executor).setCorePoolSize(minSpareThreads);
        } else if (executor instanceof ResizableExecutor) {
            ((ResizableExecutor)executor).resizePool(minSpareThreads, maxThreads);
        }
    }
}
 
Example #20
Source File: ThreadLocalLeakPreventionListener.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Updates each ThreadPoolExecutor with the current time, which is the time
 * when a context is being stopped.
 *
 * @param context
 *            the context being stopped, used to discover all the Connectors
 *            of its parent Service.
 */
private void stopIdleThreads(Context context) {
    if (serverStopping) return;

    if (!(context instanceof StandardContext) ||
        !((StandardContext) context).getRenewThreadsWhenStoppingContext()) {
        log.debug("Not renewing threads when the context is stopping. "
            + "It is not configured to do it.");
        return;
    }

    Engine engine = (Engine) context.getParent().getParent();
    Service service = engine.getService();
    Connector[] connectors = service.findConnectors();
    if (connectors != null) {
        for (Connector connector : connectors) {
            ProtocolHandler handler = connector.getProtocolHandler();
            Executor executor = null;
            if (handler != null) {
                executor = handler.getExecutor();
            }

            if (executor instanceof ThreadPoolExecutor) {
                ThreadPoolExecutor threadPoolExecutor =
                    (ThreadPoolExecutor) executor;
                threadPoolExecutor.contextStopping();
            } else if (executor instanceof StandardThreadExecutor) {
                StandardThreadExecutor stdThreadExecutor =
                    (StandardThreadExecutor) executor;
                stdThreadExecutor.contextStopping();
            }

        }
    }
}
 
Example #21
Source File: AsyncChannelGroupUtil.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private static AsynchronousChannelGroup createAsynchronousChannelGroup() {
    // Need to do this with the right thread context class loader else the
    // first web app to call this will trigger a leak
    ClassLoader original = Thread.currentThread().getContextClassLoader();

    try {
        Thread.currentThread().setContextClassLoader(
                AsyncIOThreadFactory.class.getClassLoader());

        // These are the same settings as the default
        // AsynchronousChannelGroup
        int initialSize = Runtime.getRuntime().availableProcessors();
        ExecutorService executorService = new ThreadPoolExecutor(
                0,
                Integer.MAX_VALUE,
                Long.MAX_VALUE, TimeUnit.MILLISECONDS,
                new SynchronousQueue<Runnable>(),
                new AsyncIOThreadFactory());

        try {
            return AsynchronousChannelGroup.withCachedThreadPool(
                    executorService, initialSize);
        } catch (IOException e) {
            // No good reason for this to happen.
            throw new IllegalStateException(sm.getString("asyncChannelGroup.createFail"));
        }
    } finally {
        Thread.currentThread().setContextClassLoader(original);
    }
}
 
Example #22
Source File: AbstractEndpoint.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public void setMinSpareThreads(int minSpareThreads) {
    this.minSpareThreads = minSpareThreads;
    Executor executor = this.executor;
    if (internalExecutor && executor instanceof java.util.concurrent.ThreadPoolExecutor) {
        // The internal executor should always be an instance of
        // j.u.c.ThreadPoolExecutor but it may be null if the endpoint is
        // not running.
        // This check also avoids various threading issues.
        ((java.util.concurrent.ThreadPoolExecutor) executor).setCorePoolSize(minSpareThreads);
    }
}
 
Example #23
Source File: AbstractEndpoint.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public void setMaxThreads(int maxThreads) {
    this.maxThreads = maxThreads;
    Executor executor = this.executor;
    if (internalExecutor && executor instanceof java.util.concurrent.ThreadPoolExecutor) {
        // The internal executor should always be an instance of
        // j.u.c.ThreadPoolExecutor but it may be null if the endpoint is
        // not running.
        // This check also avoids various threading issues.
        ((java.util.concurrent.ThreadPoolExecutor) executor).setMaximumPoolSize(maxThreads);
    }
}
 
Example #24
Source File: AbstractEndpoint.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Return the amount of threads that are managed by the pool.
 *
 * @return the amount of threads that are managed by the pool
 */
public int getCurrentThreadCount() {
    Executor executor = this.executor;
    if (executor != null) {
        if (executor instanceof ThreadPoolExecutor) {
            return ((ThreadPoolExecutor) executor).getPoolSize();
        } else if (executor instanceof ResizableExecutor) {
            return ((ResizableExecutor) executor).getPoolSize();
        } else {
            return -1;
        }
    } else {
        return -2;
    }
}
 
Example #25
Source File: AbstractEndpoint.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Return the amount of threads that are in use
 *
 * @return the amount of threads that are in use
 */
public int getCurrentThreadsBusy() {
    Executor executor = this.executor;
    if (executor != null) {
        if (executor instanceof ThreadPoolExecutor) {
            return ((ThreadPoolExecutor) executor).getActiveCount();
        } else if (executor instanceof ResizableExecutor) {
            return ((ResizableExecutor) executor).getActiveCount();
        } else {
            return -1;
        }
    } else {
        return -2;
    }
}
 
Example #26
Source File: AbstractEndpoint.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * 创建I/O密集型的线程池。
 * 重点在内部。
 */
public void createExecutor() {
    internalExecutor = true;
    TaskQueue taskqueue = new TaskQueue();
    TaskThreadFactory tf = new TaskThreadFactory(getName() + "-exec-", daemon, getThreadPriority());
    executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), 60, TimeUnit.SECONDS,taskqueue, tf);
    taskqueue.setParent( (ThreadPoolExecutor) executor);
}
 
Example #27
Source File: SystemBootAutoConfiguration.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * Custom tomcat executor
 * 
 * @param protocol
 * @return
 */
private Executor customTomcatExecutor(AbstractProtocol<?> protocol) {
	TaskThreadFactory tf = new TaskThreadFactory(protocol.getName() + "-exe-", true, protocol.getThreadPriority());
	TaskQueue taskqueue = new TaskQueue();
	Executor executor = new ThreadPoolExecutor(protocol.getMinSpareThreads(), protocol.getMaxThreads(), 60, TimeUnit.SECONDS,
			taskqueue, tf);
	taskqueue.setParent((ThreadPoolExecutor) executor);
	return executor;
}
 
Example #28
Source File: StandardThreadExecutor.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Start the component and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected void startInternal() throws LifecycleException {

    taskqueue = new TaskQueue(maxQueueSize);
    TaskThreadFactory tf = new TaskThreadFactory(namePrefix,daemon,getThreadPriority());
    executor = new ThreadPoolExecutor(getMinSpareThreads(), getMaxThreads(), maxIdleTime, TimeUnit.MILLISECONDS,taskqueue, tf);
    executor.setThreadRenewalDelay(threadRenewalDelay);
    if (prestartminSpareThreads) {
        executor.prestartAllCoreThreads();
    }
    taskqueue.setParent(executor);

    setState(LifecycleState.STARTING);
}
 
Example #29
Source File: ThreadLocalLeakPreventionListener.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Updates each ThreadPoolExecutor with the current time, which is the time
 * when a context is being stopped.
 * 
 * @param context
 *            the context being stopped, used to discover all the Connectors
 *            of its parent Service.
 */
private void stopIdleThreads(Context context) {
    if (serverStopping) return;

    if (!(context instanceof StandardContext) ||
        !((StandardContext) context).getRenewThreadsWhenStoppingContext()) {
        log.debug("Not renewing threads when the context is stopping. "
            + "It is not configured to do it.");
        return;
    }

    Engine engine = (Engine) context.getParent().getParent();
    Service service = engine.getService();
    Connector[] connectors = service.findConnectors();
    if (connectors != null) {
        for (Connector connector : connectors) {
            ProtocolHandler handler = connector.getProtocolHandler();
            Executor executor = null;
            if (handler != null) {
                executor = handler.getExecutor();
            }

            if (executor instanceof ThreadPoolExecutor) {
                ThreadPoolExecutor threadPoolExecutor =
                    (ThreadPoolExecutor) executor;
                threadPoolExecutor.contextStopping();
            } else if (executor instanceof StandardThreadExecutor) {
                StandardThreadExecutor stdThreadExecutor =
                    (StandardThreadExecutor) executor;
                stdThreadExecutor.contextStopping();
            }

        }
    }
}
 
Example #30
Source File: AsyncChannelGroupUtil.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
private static AsynchronousChannelGroup createAsynchronousChannelGroup() {
    // Need to do this with the right thread context class loader else the
    // first web app to call this will trigger a leak
    ClassLoader original = Thread.currentThread().getContextClassLoader();

    try {
        Thread.currentThread().setContextClassLoader(
                AsyncIOThreadFactory.class.getClassLoader());

        // These are the same settings as the default
        // AsynchronousChannelGroup
        int initialSize = Runtime.getRuntime().availableProcessors();
        ExecutorService executorService = new ThreadPoolExecutor(
                0,
                Integer.MAX_VALUE,
                Long.MAX_VALUE, TimeUnit.MILLISECONDS,
                new SynchronousQueue<Runnable>(),
                new AsyncIOThreadFactory());

        try {
            return AsynchronousChannelGroup.withCachedThreadPool(
                    executorService, initialSize);
        } catch (IOException e) {
            // No good reason for this to happen.
            throw new IllegalStateException(sm.getString("asyncChannelGroup.createFail"));
        }
    } finally {
        Thread.currentThread().setContextClassLoader(original);
    }
}