Java 如何从spring批处理步骤返回自定义RepeatStatus
步骤类:GenerateReferenceEnumberJava 如何从spring批处理步骤返回自定义RepeatStatus,java,spring,spring-boot,spring-batch,Java,Spring,Spring Boot,Spring Batch,步骤类:GenerateReferenceEnumber package com.npst.imps.action; import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.scope.context.ChunkContext; import org.springframework.batch.core.step.tasklet.Tasklet; import or
package com.npst.imps.action;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import com.npst.imps.utils.TransactionResponseData;
public class GenerateReferenceNumber implements Tasklet {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
double rrn= Math.random();
chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("rrn", rrn);
double tid= (double) chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().get("tid");
chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("trnsactionstatus", "RRN generated for Tid::"+tid+" is "+rrn);
TransactionResponseData transactionResponseData =(TransactionResponseData) chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().get("transactionResponseData");
transactionResponseData.setRrn(rrn+"");
chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("transactionResponseData", transactionResponseData);
return RepeatStatus.FINISHED;
}
}
与Repeatstatus.FINISHED不同,我如何返回自己定义的状态,并根据它们决定下一步。自定义状态,如成功、失败、部分等
batchjob.xml
<beans:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-3.0.xsd">
<job id="MBSFT">
<step id="PrepareTid" allow-start-if-complete="true" next="PrepareRRN">
<tasklet ref="PrepareTransactionId" />
</step>
<step id="PrepareRRN" allow-start-if-complete="true">
<tasklet ref="GenerateReferenceNumber" />
<next on="COMPLETED" to="IdentifyImpsService" />
</step>
<step id="IdentifyImpsService" allow-start-if-complete="true">
<tasklet ref="IdentifyIMPSRequestType" />
<next on="COMPLETED" to="FetchNBIN" />
</step>
<step id="FetchNBIN" allow-start-if-complete="true">
<tasklet ref="FetchNBINFromIFSC" />
</step>
</job>
</beans:beans>
我想这是不可能的。
您可以将自定义返回状态放入StepExecution,使用将属性从步骤移动到作业执行上下文,然后使用重定向流。您可以轻松地从
StepExecutionListener
发送自定义状态。
ExitStatus是简单枚举。您可以传递任何字符串作为退出状态
根据存在状态决定批处理流程
下面是示例java配置代码
//Return status from step listener
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
if(condition1)
return new ExitStatus("CUSTOM_STATUS");
if(condition2)
return new ExitStatus("CUSTOM_STATUS_XYZ");
if(condition3)
return new ExitStatus.COMPLETED
}
@Bean
public Job myJob(JobBuilderFactory jobs) throws Exception {
return jobs.get("myJob")
.start(Step1())
.next(Step2()).on("CUSTOM_STATUS").to(step3())
.next(Step2()).on("CUSTOM_STATUS_XYZ").to(step4())
.next(Step2())).on("COMPLETED").to(step4())
.build()
.listener(listener)
.preventRestart()
.build();
}
@如果答案对你有帮助,你能接受吗?所以其他有相同问题的人可以使用相同的解决方案:)但返回自定义状态的原因是什么?谁来处理这个状态?