Java Code Examples for org.apache.tomcat.util.threads.ThreadPoolExecutor#shutdownNow()

The following examples show how to use org.apache.tomcat.util.threads.ThreadPoolExecutor#shutdownNow() . 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: 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 2
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 3
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 4
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)");
      }
    }
  }
}