2016-04-25 23 views
3

在我的Spring boot application我目前使用运行以下:Spring Boot应用程序:将应用程序分成独立的任务从命令行运行?

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

    public static void main(String[] args){ 

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

这则发现下面的类来运行:

@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() { 

    } 

} 

正如从以上可以看出,我的应用程序运行所有3方法依次为

我想变化我的应用程序,以便任何方法/任务可以在命令行中随时跑,而不是调用的主要方法和运行行中的所有三种方法。

我该怎么做?我需要从MyApplication类中移动我的方法吗?

+0

您是否希望能够在应用程序的运行时期间运行任务,或者只能在启动时作为命令行中的参数运行? – dunni

+0

我希望它们能够作为cmd行上的参数运行? – java123999

回答

0

我建议看看项目Spring Batch。该项目正是为了这样的要求。特别是this section of its docs可能是你的兴趣所在。它描述了如何从命令行执行弹簧批处理作业。

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

+0

好的,有没有一种简单的方法使用java,我可以配置我的应用程序能够单独调用每个方法? – java123999

+0

是的,有!看看JMX –

+0

我现在在看它,你能举一个我将如何使用它的例子吗? – java123999

相关问题