Spring Step Scheduling

时间:2017-02-07 15:24:04

标签: java spring spring-batch spring-xd

我正在尝试安排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()

1 个答案:

答案 0 :(得分:0)

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

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>

希望它能带给你前进。