@Bean
public TaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
    taskExecutor.setCorePoolSize(2);
    taskExecutor.setMaxPoolSize(2);
    taskExecutor.setWaitForTasksToCompleteOnShutdown(true);//<---
		taskExecutor.setAwaitTerminationSeconds(30);//<---
    taskExecutor.initialize();
    return taskExecutor;
}

Ref

https://www.baeldung.com/spring-boot-graceful-shutdown