After thinking about how to run an infinite task in Spring Boot (see my previous question), I decided to do this:
@Component
@EnableAsync
public class MyRunner implements CommandLineRunner {
    @Autowired
    private ScheduledExecutorService scheduledExecutorService;
    @Async
    @Override
    public void run(String... args) throws Exception {
        try {
            infiniteLoop();
        } finally {
            this.scheduledExecutorService.shutdown();
        }
    }
}
This allows the SpringApplication#run method to fully complete.
As part of the whole thing, I have declared a ScheduledExecutorService bean. My issue is that if infiniteLoop() throws an unexpected exception, the Executor will prevent the application from shunting down as it is managed externally.
Is there a better way to handle such a case? Or is this a good use of the finally block?
 
    