我正在写一份春季批处理作业。但是当这个Archive类在加载时实现了tasklet接口时,注释@BeforeStep下的方法没有被调用。谁能帮我这个 ? 谢谢
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
public class Archive implements Tasklet{
@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
throws Exception {
System.out.println("in execute method :)");
return RepeatStatus.FINISHED;
}
@BeforeStep
public void retrieveInterstepData(StepExecution stepExecution){
JobExecution jobExecution = stepExecution.getJobExecution();
ExecutionContext jobContext = jobExecution.getExecutionContext();
}
}
答案 0 :(得分:13)
执行第三个解决方案:您可能没有将您的tasklet注册为侦听器,因此不会从一开始就调用带注释的方法。您可以在作业xml定义中将tasklet引用作为侦听器提供,如下所示:
<job id="yourJob" >
<step id="step1">
<tasklet ref="archive">
<listeners>
<listener ref="archive" />
</listeners>
</
</step>
</job>
您还必须使用以下命令注释Archive类:
@Component("archive")
@Scope("step")
答案 1 :(得分:7)
第一个解决方案可以从ExecutionContext
方法中提取execute
ChunkContext
,并根据需要对其进行处理。
ExecutionContext jobContext = chunkContext.getStepContext()
.getStepExecution()
.getJobExecution()
.getExecutionContext();
第二个解决方案可以实现StepExecutionListener
并覆盖beforeStep
方法。你会有类似的东西:
public class Archive implements Tasklet, StepExecutionListener{
@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
throws Exception {
System.out.println("in execute method :)");
return RepeatStatus.FINISHED;
}
@Override
public void beforeStep(final StepExecution stepExecution) {
JobExecution jobExecution = stepExecution.getJobExecution();
ExecutionContext jobContext = jobExecution.getExecutionContext();
}
}
我有类似的问题,我们就这样克服了。至于为什么@BeforeStep
没有在tasklet上调用,但它在readers
,processors
和writers
内,我不确定。