首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring Step调度

Spring Step调度
EN

Stack Overflow用户
提问于 2017-02-07 23:24:04
回答 1查看 785关注 0票数 0

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

代码语言:javascript
复制
<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>

我得到了这个错误:

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-08 18:17:58

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

代码语言:javascript
复制
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的相关方法来触发您想要启动的实际作业:

代码语言:javascript
复制
<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>

希望它能带给你进步。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42093555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档