gpt4 book ai didi

spring-batch - @BeforeStep 注释的方法没有被调用

转载 作者:行者123 更新时间:2023-12-03 11:11:32 24 4
gpt4 key购买 nike

我正在写一个 Spring 批处理作业。但是在加载这个实现tasklet接口(interface)的Archive类时,注释@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();

}
}

最佳答案

第一种解决方案可以提取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 内, processorswriters我不确定。

关于spring-batch - @BeforeStep 注释的方法没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29935824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com