首页 文章

无法从Spring Batch Admin应用程序启动Spring TaskScheduler

提问于
浏览
1

我有一个spring批处理管理项目(1.3.0.RC1),我正在尝试使用cron表达式启用TaskScheduler来运行作业 .

我有一个 Spring 季批量管理项目,我有另一个项目与我的批处理作业 . 我可以通过管理控制台手动执行该工作(意味着它加载正常并且有效) . 但是,作业永远不会在cron计划中执行 .

如果我使用main方法配置java类并使用相同的作业xml文件将其作为java应用程序运行,则作业将在cron调度上运行(换句话说,在Spring批处理管理控制台之外运行作业) . 这也应该验证xml配置是否正常 .

这工作:project2 app.java

public static void main(String[] args) {
    String springConfig = "META-INF/spring/batch/jobs/scheduleTest.xml";
    ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
}

project2 META-INF / spring / jobs / scheduleTest.xml

<job id="test-job"
        xmlns="http://www.springframework.org/schema/batch">
        <step id="testJob">
            <tasklet transaction-manager="transactionManager" ref="testOutput"/>
        </step>
    </job>


    <bean id="testOutput" class="com.default.test.testOutput" />


    <task:scheduled-tasks>
        <task:scheduled ref="runIt" method="run" cron="30 * * * * ?" />
    </task:scheduled-tasks>

    <bean id="runIt" class="com.default.scheduler.RunScheduler">
        <property name="job" ref="test-job" />
        <property name="jobLauncher" ref="jobLauncher"/>
        <property name="runCron" value="true" />
    </bean>

作业每30秒运行一次,并将一些测试输出打印到控制台 .

不起作用:当我运行Spring Batch管理控制台时,project2与project1打包在一起,job.xml文件成为类路径的一部分,并且作业在控制台中注册并可以启动 . 我可以从控制台运行它,它工作正常,但它不会自动在预定的时间运行 . 在我手动执行它之后,它也没有在预定的时间开始运行 .

有谁看到我在这里做错了什么?任何指针都将非常感激 . 或者如果有人知道在SBA中使用Spring TaskScheduler(不是quartz)的例子,那也会有所帮助 . 提前致谢 . 我的猜测是cron表达式被存储到一个不再存在或SBA控制台不知道的应用程序上下文中 .

1 回答

  • 0

    我在SBA 1.3.0.RELEASE中遇到同样的问题 . 它适用于手动加载上下文的main方法,但在SBA中它不运行TaskScheduler .

    出于调试目的,我有一个ApplicationListener . 在onApplicationEvent方法中,我手动创建并运行自己的TaskScheduler .

    META-INF / Spring /批号/职位/ SBA-configuration.xml文件

    <job id="test-job"
        xmlns="http://www.springframework.org/schema/batch">
        <step id="testJob">
            <tasklet transaction-manager="transactionManager" ref="testOutput"/>
        </step>
    </job>
    
    <bean id="testOutput" class="com.default.test.testOutput" />
    
    
    <task:scheduled-tasks>
        <task:scheduled ref="runIt" method="run" cron="30 * * * * ?" />
    </task:scheduled-tasks>
    
    <bean id="runIt" class="com.default.scheduler.RunScheduler">
        <property name="job" ref="test-job" />
        <property name="jobLauncher" ref="jobLauncher"/>
        <property name="runCron" value="true" />
    </bean>
    
    <bean id="appContextListener" class="com.qmn.AppContextListener" />
    <bean id="myJobRunner" class="com.qmn.MyJobRunner"/>
    <task:scheduler id="scheduler" pool-size="10" />
    

    com.qmn.AppContextListener

    public class AppContextListener implements ApplicationListener<ContextRefreshedEvent> {
    
        @Autowired
        private WebApplicationContext wac; //this context doesn't contain beans which are in sba-configuration.xml
    
        @Autowired
        private ThreadPoolTaskScheduler scheduler; //in sba-configuration.xml but doesn't have in wac. Spring still injects success.
    
        @Autowired
        private MyJobRunner myJobRunner; //in sba-configuration.xml but doesn't have in wac
    
        @Autowired
        private Job job; //in sba-configuration.xml but doesn't have in wac
    
        @Autowired
        private JobLauncher jobLauncher; // default jobLauncher of SBA
    
        @Override
        public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
            XmlWebApplicationContext context = contextRefreshedEvent.getSource() instanceof XmlWebApplicationContext ?
                    (XmlWebApplicationContext) contextRefreshedEvent.getSource() : null;
    
            if(context != null && context.getNamespace().contains("Batch Servlet")) { // only work with my spring batch admin servlet
                myJobRunner.setJob(job);
                myJobRunner.setJobLauncher(jobLauncher);
                CronTrigger trigger = new CronTrigger("*/5 * * * * *");
                ScheduledFuture<?> scedulefuture = scheduler.schedule(myJobRunner, trigger);
            }
        }
    
    }
    

    代码运行良好,每5秒运行一次 . 所以我猜SBA吃了在xml config中定义的TaskScheduler . 这是Spring Batch Admin的错误吗?

相关问题