繁体   English   中英

弹簧步调度

[英]Spring Step Scheduling

我正在尝试安排Spring Batch作业中的步骤。 我尝试使用。 但是,当我尝试在SpringXd上部署作业时,它失败了。 以下是我面临的代码和错误

<batch:job id="addB" restartable="false">
<batch:step id="AddB" >
        <tasklet ref="addBTasklet" />
</batch:step>
</batch:job>

<task:scheduler id="taskScheduler" pool-size="1"/>
        <task:scheduled-tasks scheduler="taskScheduler">        
        <task:scheduled ref="AddB" method="execute" cron="*/5 * * * * ?"/>
 </task:scheduled-tasks>

我收到此错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
'org.springframework.scheduling.support.ScheduledMethodRunnable#0': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.springframework.scheduling.support.ScheduledMethodRunnable]:` Constructor threw exception; nested exception is java.lang.NoSuchMethodException: 
org.springframework.batch.core.step.tasklet.TaskletStep.execute()

您将需要添加一个实现所需逻辑的类以启动作业,然后让调度程序调用该类中的方法。 新课程的内容:

package mypackage;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class TriggerScheduledJob {

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    @Qualifier("addB")
    private Job addBJob;

    @Autowired
    @Qualifier("addC")
    private Job addCJob;

    public void triggerAddB() {
        JobParameters param = new JobParametersBuilder().addString("id", UUID.randomUUID().toString()).toJobParameters();
        try {
            JobExecution execution = jobLauncher.run(addBJob, param);
            System.out.println("Job executed with exit status = " + execution.getStatus());
        } catch (Exception e) {
            System.out.println("Failed to execute job. " + e.getMessage());
        }
    }

    public void triggerAddC() {
        JobParameters param = new JobParametersBuilder().addString("id", UUID.randomUUID().toString()).toJobParameters();
        try {
            JobExecution execution = jobLauncher.run(addCJob, param);
            System.out.println("Job addC executed with exit status = " + execution.getStatus());
        } catch (Exception e) {
            System.out.println("Failed to execute job. " + e.getMessage());
        }
    }
}

然后调整您的Spring应用程序上下文,使其创建该新bean,并修改调度程序以调用该bean的相关方法以触发您要启动的实际作业:

<batch:job id="addB" restartable="false">
    <batch:step id="AddB" >
        <tasklet ref="addBTasklet" />
    </batch:step>
</batch:job>

<batch:job id="addC" restartable="false">
    <batch:step id="AddC" >
        <tasklet ref="addCTasklet" />
    </batch:step>
</batch:job>

<bean id="triggerScheduledJob" class="mypackage.TriggerScheduledJob" />

<task:scheduler id="taskScheduler" pool-size="1"/>
<task:scheduled-tasks scheduler="taskScheduler">        
    <task:scheduled ref="triggerScheduledJob" method="triggerAddB" cron="*/5 * * * * ?"/>
</task:scheduled-tasks>
<task:scheduled-tasks scheduler="taskScheduler">        
    <task:scheduled ref="triggerScheduledJob" method="triggerAddC" cron="*/5 * * * * ?"/>
</task:scheduled-tasks>

希望它能带您前进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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