[英]Spring Batch: Pass over names of read-in files to the next step
I'm new to Spring Batch and I got started off using Spring Boot & Spring Batch official tutorial . 我是Spring Batch的新手,我开始使用Spring Boot和Spring Batch官方教程 。
The problem I have is that, after reading in some files using spring batch, I want to store the names of those files and remove them in the next step. 我遇到的问题是,在使用spring批处理读取一些文件后,我想存储这些文件的名称并在下一步中删除它们。
I first tried StepExecutionContext to pass data but apparently the data that I wished to pass along was way to big. 我首先尝试使用StepExecutionContext来传递数据,但显然我希望传递的数据是大的。 So I tried saving the name of the files that I read when I initialized my Reader and reading that file in my next step. 所以当我初始化Reader并在下一步中读取该文件时,我尝试保存我读取的文件的名称。
@Bean
public MultiResourceItemReader<Widget> widgetMultiReader() {
MultiResourceItemReader<Widget> reader = new MultiResourceItemReader<Widget>();
ClassLoader cl = this.getClass().getClassLoader();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
try {
System.out.println("file:" + dataFileLocation + "Widget*.dat");
Resource[] resources = resolver.getResources("file:" + dataFileLocation + "Widget*.dat");
System.out.println("FOUND " + resources.length + " Widget.dat files");
StringBuilder sb = new StringBuilder();
for (int i=0; i<resources.length; i++) {
System.out.println(resources[i].getFilename());
sb.append(resources[i].getFilename());
if(i < resources.length-1){
sb.append("\n");
}
}
reader.setResources(resources);
reader.setDelegate(widgetReader());
File tempFolder = new File(dataFileLocation + Constants.TEMP_FOLDER_NAME);
tempFolder.mkdir();
File tempFile = new File(tempFolder.getAbsolutePath(), "read_in_files.tmp");
tempFile.createNewFile();
FileWriter fileWriter = new FileWriter(tempFile, false);
fileWriter.write(sb.toString());
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
return reader;
}
This was all working really well but turns out if I do this, the code inside the bean will execute automatically even if it's not defined in the job step. 这一切都运行得很好,但如果我这样做,bean内的代码将自动执行,即使它没有在作业步骤中定义。 This apparently is because of dependency injection ( Spring boot spring.batch.job.enabled=false not able to recognize ) 这显然是因为依赖注入( Spring boot spring.batch.job.enabled = false无法识别 )
So I have to change my method yet again can you guys help me out here please? 所以我必须再改变一下我的方法,你能帮助我吗?
All I want to do is to read in a set of text files, read in the data inside it, store it to the database (this is already done) THEN, remove/archive/move those files in the next step so if there's something like FlatFileParseException, entire job will stop and not remove the files. 我想要做的就是读取一组文本文件,读入其中的数据,将其存储到数据库中(这已经完成)然后,在下一步中删除/存档/移动这些文件,这样如果有的话与FlatFileParseException一样,整个作业将停止并且不会删除文件。
Create a service that you can use to pass the data between the steps 创建可用于在步骤之间传递数据的服务
By annotating the service with @JobScope
or @StepScope
it will only live during that step or job. 通过使用@JobScope
或@StepScope
注释服务,它将仅在该步骤或作业期间@StepScope
。
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.List;
@Service
@JobScope
public class FilesService {
private List<File> files;
public List<File> getFiles() {
return files;
}
public void setFiles(List<File> files) {
this.files = files;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.