简体   繁体   中英

Spring Batch Job Scheduler

Is there any way to specify Scheduler for specific Spring Batch job configured via XML without utils RunScheduler class like this: https://www.mkyong.com/spring-batch/spring-batch-and-spring-taskscheduler-example/ ?

So for now my config looks like this:

 <batch:job id="testJob" job-repository="jobRepository" parent="jobParent">
        <batch:step id="testStep" allow-start-if-complete="true">
            <batch:tasklet>
                <batch:chunk
                        reader="testReader"
                        processor="testProcessor"
                        writer="jmsWriter">
                </batch:chunk>
            </batch:tasklet>
        </batch:step>
    </batch:job>


 <task:scheduled-tasks>
        <task:scheduled ref="testJobLauncher" method="runJob" cron="0 */5 * * * *"/>
    </task:scheduled-tasks>

    <bean id="testJobLauncher"
          class="com.test.RunScheduler"
          p:job-ref="testJob"
          p:jobLauncher-ref="jobLauncher"
     "/>



@Component
public class RunScheduler {

    private JobLauncher jobLauncher;
    private Job job;

    public void runJob() {
        try {
            String dateParam = new Date().toString();
                JobParameters param = new JobParametersBuilder().addString("date", dateParam).toJobParameters();
                JobExecution execution = jobLauncher.run(job, param);               
        } catch (Exception e) {
            LOGGER.error("Can't start job", e);
            throw new RuntimeException(e);
        }
    }

    public Job getJob() {
        return job;
    }

    public void setJob(Job job) {
        this.job = job;
    }

    public JobLauncher getJobLauncher() {
        return jobLauncher;
    }

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

}

Is there way not use RunScheduler class and just handle it using XML config?

您可以将@EnableScheduling和cronSequenceGenerator的功能用于调度和cron设置,而无需依赖util类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM