Shutting down after task completion

时间:2017-12-18 06:44:10

标签: spring java-8 postconstruct

I have spring application written in the following way -

@SpringBootApplication
@Import({
        ApplicationLoader.class
})
public class MyApplication {

    @Bean
    public Clock clock() {
        return Clock.systemDefaultZone();
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

The ApplicationLoader class looks like below

public class ApplicationLoader {

    private final MyService myService;

    public ApplicationLoader(MyService myService) {
        // create
        this.myService = myService;
    }

    @PostConstruct
    void startJob() {
        // perform some long running task
        myService.runLongRunningTask();
    }
}

Once ApplicationLoader finishes its job, Spring Application continues to run while I want it to stop.

Is there a way I can introduce a stop to this entire application lifecycle which runs after the application is done performing the long running task? I don't mind a rewrite of ApplicationLoader or MyApplication if there is a better way. My aim is to instantiate MyService and run myService.runLongRunningTask() based on certain runtime conditions.

0 个答案:

没有答案
相关问题