This is the configuration;

        // @Async will use this if not created then it will use SimpleAsyncTaskExecutor
    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        
        var executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(10);
        executor.setAllowCoreThreadTimeOut(true); // Default keep-alive seconds are set to 60
        executor.setWaitForTasksToCompleteOnShutdown(true); // To my understanding allows graceful shutdown
        executor.setThreadNamePrefix("AES-Async-");
        executor.initialize();
        
        return executor;
        
    }

This is where I call this Async method

    @Bean
    public CommandLineRunner commandLineRunner() {
        
        return runner -> {
            asyncService.processAsyncRequest();
            System.exit(1); // also used intelliJ exit button instead of this
        };
        
    }

Now here is the Async Method, @EnableAsync is configured

    @SneakyThrows
    @Async
    public void processAsyncRequest() {
        for (int i = 0 ; i < 20 ; i++) {
            log.info("AES-Async-Thread will be sleeping for 5 sec");
            Thread.sleep(5000);
        }
    }

Now this method does not completes its execution and the application terminates

I want this to work without setting this setAwaitTerminationSeconds(int awaitTerminationSeconds)

The documentation says for setWaitForTasksToCompleteOnShutdown(true):

Set whether to wait for scheduled tasks to complete on shutdown, not interrupting running tasks and executing all tasks in the queue.

I tried this setAwaitTerminationSeconds(int awaitTerminationSeconds) but it just waits till the timeout reaches and terminates the Async task while its executing

I don't know what I am missing, can anyone help me with this I don't want to implement shutdown hooks or anything just trying this abstraction to work as I intended.

0

There are 0 best solutions below