Spring Boot application: Splitting application into seperate tasks to be ran from command line?

时间:2016-04-25 09:16:19

标签: java spring methods spring-boot spring-batch

Within my Spring boot application I currently run it using the following:

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("my.packages.to.scan")
@EnableScheduling
public class Scheduler {

    public static void main(String[] args){

        SpringApplication.run(Scheduler.class, args);
    }
}

This then finds the following class to run:

@Component
public class MyApplication {

    @Transactional
    @Scheduled(fixedRate = 400000, initialDelay = 1000)
    public void tasks() {

        methodOne();
        methodTwo();
        methodThree();
    }

    public void methodOne() {

    }

    public void methodTwo() {

    }

    public void methodthree() {

    }

}

As can be seen from the above, my application runs all three methods in sequence.

I would like to change my application so any method/task can be ran from the command line at any time, rather than calling the main method and running all three methods in a row.

How can I do so? Do I need to move my methods from the MyApplication class?

1 个答案:

答案 0 :(得分:0)

我建议调查项目Spring Batch。该项目完全符合这些要求。特别是this section of its docs可能是您感兴趣的。它描述了如何从命令行执行弹簧批处理作业。

评论反应: Here is my Github repository with working example。注意shell脚本作为如何从命令行执行某些任务的示例。