Spring 步骤安排
Spring Step Scheduling
我正在尝试在 Spring 批处理作业中安排步骤。我尝试使用 .但是,当我尝试在 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()
您需要添加一个 class 来实现启动作业所需的逻辑,然后让调度程序调用该 class 中的方法。这个新内容 class:
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>
希望它能给你带来进步。
我正在尝试在 Spring 批处理作业中安排步骤。我尝试使用 .但是,当我尝试在 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()
您需要添加一个 class 来实现启动作业所需的逻辑,然后让调度程序调用该 class 中的方法。这个新内容 class:
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>
希望它能给你带来进步。