1

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

我有一个春季批处理管理项目,我的批处理作业还有另一个项目。我可以通过管理控制台手动执行该作业(这意味着它可以正常加载并且可以正常工作)。但是,该作业永远不会按 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(不是石英)的示例,那也会有所帮助。提前致谢。我的猜测是 cron 表达式正在存储到不再存在或 SBA 控制台不知道的应用程序上下文中。

4

1 回答 1

0

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

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

META-INF/spring/batch/jobs/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 配置中定义的 TaskScheduler。这是 Spring Batch Admin 的错误吗?

于 2014-11-07T10:25:02.363 回答