有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java ThreadPoolTaskExecutor保持运行,如何控制关机?

我正在用ThreadPoolTaskExecutor编写spring启动应用程序。 这是我的应用程序类:

import info.gandraweb.apns.service.MessageService;
import info.gandraweb.apns.settings.AppSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@SpringBootApplication
@EnableScheduling
@EnableAutoConfiguration
@EnableAsync
public class Application implements AsyncConfigurer{


    private static final Logger logger = LoggerFactory.getLogger(Application.class);


    @Autowired
    private AppSettings appSettings;

    public static void main(String[] args) {
        ApplicationContext applicationContext = SpringApplication.run(Application.class, args);
        AppSettings appSettings = applicationContext.getBean(AppSettings.class);
        logger.info(appSettings.toString());

        test(applicationContext);
    }

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setThreadNamePrefix("MyExecutor-");
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(50);
        taskExecutor.setQueueCapacity(1000);
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }


    private static void test(ApplicationContext applicationContext) {
        MessageService messageService = applicationContext.getBean(MessageService.class);
        messageService.processNewMessages();
    }

}**

我正在调用的内部测试方法

messageService.processNewMessages()

反过来,如果DB中存在某个任务,则调用另一个bean上的异步方法来处理它。 当没有处理应用程序完成的任务时
如果数据库中有任何任务要在执行后处理,应用程序将继续运行,并且永远不会完成。。。。似乎从未调用ThreadPoolTaskExecutor上的shutdown()方法

然后,我实现了ApplicationListener接口,并在ApplicationEvent(ContextClosedEvent)上记录了内部的情况

因此,当没有处理应用程序完成的任务时,将触发ContextClosedEvent。 如果数据库中有任何任务要在执行后处理,则不会触发ContextClosedEvent

当我从应用程序中删除AsyncConfigurer时,将触发ContextClosedEvent并关闭应用程序。但在这种情况下,我无法配置ThreadPoolTaskExecutor

那么,我如何配置ThreadPoolTaskExecutor并设法正常关闭此执行器,或触发ContextClosedEvent。。。哪种方式更适合优雅地关闭它


共 (0) 个答案