spring - 第一个 Spring 批处理作业被第二个作业的详细信息覆盖

标签 spring spring-mvc quartz-scheduler spring-batch

我有一个集成了 Spring Batch 的 Spring MVC 应用程序。我的 Spring 批处理作业通过 cron 触发器执行。

我在 xml 中定义了两个批处理作业。第二个批处理作业工作正常,但是当我执行第一个批处理作业时,它执行了第二个批处理作业。

我认为我错误地注册了我的职位。有人可以帮助我解决可能出现的问题吗?

谢谢

第二个作业定义 (job-bulletin-bar-msg-update.xml)...

<job id="job-bulletin-bar-msg-update" xmlns="http://www.springframework.org/schema/batch">
    <step id="step1">
        <tasklet>
            <chunk 
                reader="bulletinBarUpdateJdbcCursorItemReader" 
                writer="bulletinBarUpdateItemWriter" 
                commit-interval="1000" />
        </tasklet>
    </step>
</job>

第一个作业定义 (job-daily-tran-counts.xml)...

<job id="job-daily-tran-counts" xmlns="http://www.springframework.org/schema/batch">
    <step id="step1">
        <tasklet>
            <chunk 
                reader="dailyTranCountJdbcCursorItemReader" 
                writer="dailyTranCountItemWriter" 
                commit-interval="1000" />
        </tasklet>
    </step>
</job>

将作业添加到调度程序和注册表......

<beans profile="pre,prod">
    <!-- jobs -->
    <import resource="./jobs/job-daily-tran-counts.xml" />
    <import resource="./jobs/job-bulletin-bar-msg-update.xml" />    

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
           <list>
              <ref bean="dailyTranCountJobDetail" />
              <ref bean="bulletinBarMsgUpdateJobDetail" />
           </list>
        </property>
        <property name="triggers">
           <list>
              <ref bean="dailyTranCountCronTrigger" />
              <ref bean="bulletinBarMsgUpdateCronTrigger" />
           </list>
        </property>
    </bean>

    <!-- scheduling properties -->
    <util:properties id="batchProps" location="classpath:batch.properties" />
    <context:property-placeholder properties-ref="batchProps" />        

    <!-- triggers -->
    <bean id="dailyTranCountCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="dailyTranCountJobDetail" />
        <property name="cronExpression" value="#{batchProps['cron.dailyTranCounts']}" />
    </bean>


    <bean id="bulletinBarMsgUpdateCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="bulletinBarMsgUpdateJobDetail" />
        <property name="cronExpression" value="#{batchProps['cron.bulletinBarUpdateMsg']}" />
    </bean>     



    <!-- job detail -->
    <bean id="dailyTranCountJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="com.myer.reporting.batch.JobLauncherDetails" />
        <property name="group" value="quartz-batch" />
        <property name="jobDataAsMap">
            <map>
                <entry key="jobName" value="job-daily-tran-counts" />
                <entry key="jobLocator" value-ref="jobRegistry" />
                <entry key="jobLauncher" value-ref="jobLauncher" />
            </map>
        </property>
    </bean> 

    <bean id="bulletinBarMsgUpdateJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass" value="com.myer.reporting.batch.JobLauncherDetails" />
        <property name="group" value="quartz-batch" />
        <property name="jobDataAsMap">
            <map>
                <entry key="jobName" value="job-bulletin-bar-msg-update" />
                <entry key="jobLocator" value-ref="jobRegistry" />
                <entry key="jobLauncher" value-ref="jobLauncher" />
            </map>
        </property>
    </bean>

</beans>            

这是我的工作启动器详细信息类(class)。我只是从其他地方复制的...

public class JobLauncherDetails extends QuartzJobBean {

    static final String JOB_NAME = "jobName";

    private JobLocator jobLocator;

    private JobLauncher jobLauncher;

    public void setJobLocator(JobLocator jobLocator) {
        this.jobLocator = jobLocator;
    }

    public void setJobLauncher(JobLauncher jobLauncher) {
        this.jobLauncher = jobLauncher;
    }

    @SuppressWarnings("unchecked")
    protected void executeInternal(JobExecutionContext context) {

        Map<String, Object> jobDataMap = context.getMergedJobDataMap();

        String jobName = (String) jobDataMap.get(JOB_NAME);

        JobParameters jobParameters = getJobParametersFromJobMap(jobDataMap);

        try {
            jobLauncher.run(jobLocator.getJob(jobName), jobParameters);
        } catch (JobExecutionException e) {
            e.printStackTrace();
        }
    }

    //get params from jobDataAsMap property, job-quartz.xml
    private JobParameters getJobParametersFromJobMap(Map<String, Object> jobDataMap) {

        JobParametersBuilder builder = new JobParametersBuilder();

        for (Entry<String, Object> entry : jobDataMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof String && !key.equals(JOB_NAME)) {
                builder.addString(key, (String) value);
            } else if (value instanceof Float || value instanceof Double) {
                builder.addDouble(key, ((Number) value).doubleValue());
            } else if (value instanceof Integer || value instanceof Long) {
                builder.addLong(key, ((Number) value).longValue());
            } else if (value instanceof Date) {
                builder.addDate(key, (Date) value);
            } else {
                // JobDataMap contains values which are not job parameters
                // (ignoring)
            }
        }

        //need unique job parameter to rerun the same job
        builder.addDate("run date", new Date());

        return builder.toJobParameters();

    }

}

这是我的batch.properties...

#http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger
cron.dailyTranCounts=0 15 07 * * ? *
cron.bulletinBarUpdateMsg=0 30 07 * * ? *

更多信息。为了尝试了解更多信息,我在 org.springframework.batch.core.launch.support.SimpleJobLauncher 上放置了一个日志。

当我在 spring 配置中仅启用第一个作业时,日志显示 job-daily-tran-counts 大约需要 2.5 分钟才能执行...

[INFO] 2014-03-19 07:15:00,187 org.springframework.batch.core.launch.support.SimpleJobLauncher run - Job: [FlowJob: [name=job-daily-tran-counts]] launched with the following parameters: [{run date=1395173700052}]
[INFO] 2014-03-19 07:17:43,934 org.springframework.batch.core.launch.support.SimpleJobLauncher run - Job: [FlowJob: [name=job-daily-tran-counts]] completed with the following parameters: [{run date=1395173700052}] and the following status: [COMPLETED]

当我在 spring 配置中只启用这两个作业时,日志显示 job-daily-tran-counts 大约只需要 8 毫秒,而实际上它应该需要更长的时间。即它什么也没做...

[INFO] 2014-04-15 07:15:00,225 org.springframework.batch.core.launch.support.SimpleJobLauncher run - Job: [FlowJob: [name=job-daily-tran-counts]] launched with the following parameters: [{run date=1397510100128}]
[INFO] 2014-04-15 07:15:08,977 org.springframework.batch.core.launch.support.SimpleJobLauncher run - Job: [FlowJob: [name=job-daily-tran-counts]] completed with the following parameters: [{run date=1397510100128}] and the following status: [COMPLETED]
[INFO] 2014-04-15 07:30:00,009 org.springframework.batch.core.launch.support.SimpleJobLauncher run - Job: [FlowJob: [name=job-bulletin-bar-msg-update]] launched with the following parameters: [{run date=1397511000007}]
[INFO] 2014-04-15 07:30:05,217 org.springframework.batch.core.launch.support.SimpleJobLauncher run - Job: [FlowJob: [name=job-bulletin-bar-msg-update]] completed with the following parameters: [{run date=1397511000007}] and the following status: [COMPLETED]

最佳答案

我也有类似的问题。对我有用的是重命名步骤 ID,以便它们是唯一的。值得一试

关于spring - 第一个 Spring 批处理作业被第二个作业的详细信息覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22978790/

相关文章:

java - Spring java中的ToString和EqualsAndHashCode注解

java - RabbitMQ 请求响应和消息确认

java - spring mvc中上传下载xlsx文件

grails - 具有非定期cron表达式槽的运行作业

java - 在 Java 中使用 quartz 进行作业链接

spring - 如何将spring boot项目转换为spring mvc

java - Spring 5 - 在 HQL 中使用未知 SQL 函数

java - Spring MVC 后端返回 404 (Rest)

java - Spring : Unsatisfied dependency

java - 启动 Quartz Scheduler 而不触发触发器