Java Spring Boot,线程未关闭

时间:2019-12-10 15:20:18

标签: java multithreading spring-boot

我有一个 spring boot Java应用程序,正在尝试正常关机。我正在使用关机端点。

我的应用程序正在接收WebSocket个连接。如果尚无连接,则应用程序可以正确关闭,但是在第一次连接发生后,由于连接创建的线程不受正常关闭的影响,关闭将被阻塞。

关闭应用程序的方法是使用ContextClosedEvent

像这样:

@Override
    @EventListener
    public void onApplicationEvent(final ContextClosedEvent event) {
        connector.pause();
        Executor executor = this.connector.getProtocolHandler().getExecutor();
        if (executor instanceof ThreadPoolExecutor) {
            try {
                ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;

                threadPoolExecutor.shutdown();
                if (!threadPoolExecutor.awaitTermination(TIMEOUT, TimeUnit.SECONDS)) {

                    threadPoolExecutor.shutdownNow();

                    if (!threadPoolExecutor.awaitTermination(TIMEOUT, TimeUnit.SECONDS)) {
                        logger.error("Tomcat thread pool did not terminate");
                    }
                }
            } catch (InterruptedException  ex)  {
                Thread.currentThread().interrupt();
            }
        }

我的线程是这样创建的:

private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(10);

private void foo(String bar) {
        Runnable runnable = new Runnable()

        ScheduledFuture<?> future = executorService.schedule(runnable, sessionTimeout, TimeUnit.SECONDS);
        sessionFutures.put(sessionId, future);
    }

已创建且不受threadPoolExecutor.shutdown()或.shutdownNow()影响的线程为Name and description of the thread

我的问题是为什么该线程没有关闭,我如何使其关闭?

我的假设是,我不将正在创建的任务与正在运行的当前执行程序相关联。

0 个答案:

没有答案