java - Spring 批处理 : Pass over names of read-in files to the next step

标签 java spring spring-boot spring-batch batch-processing

我是 Spring Batch 的新手,我开始使用 Spring Boot 和 Spring Batch 官方 tutorial .

我遇到的问题是,在使用 Spring Batch 读取一些文件后,我想存储这些文件的名称并在下一步中删除它们。

我第一次尝试 StepExecutionContext 来传递数据,但显然我希望传递的数据太大了。因此,我尝试保存初始化 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;
}

这一切都工作得很好,但事实证明,如果我这样做,bean 内的代码将自动执行,即使它没有在作业步骤中定义。这显然是由于依赖注入(inject)( Spring boot spring.batch.job.enabled=false not able to recognize )

所以我必须再次改变我的方法,你们能帮我吗?

我想做的就是读入一组文本文件,读入其中的数据,将其存储到数据库(这已经完成),然后在下一步中删除/存档/移动这些文件,这样如果出现类似 FlatFileParseException 的情况,整个作业将停止并且不会删除文件。

最佳答案

创建可用于在步骤之间传递数据的服务

通过使用 @JobScope@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;
    }
}

关于java - Spring 批处理 : Pass over names of read-in files to the next step,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42383404/

相关文章:

java - JPA 事务 - CrudRepository #save(List) 未回滚

java - 如何在 Spring Boot 中创建 JDBC TokenStore?

java - 通过 JSON 从 Java 到 Javascript 的 Unicode 字符串

java - 在 Spring Hibernate java 项目中使用 "Envers"审计表

spring - 如何通过上下文更改Spring Security角色?

java - 如何使用 Spring Boot 2.3.1 将http流量重定向到https

java - 启动 JHipster 应用程序无法正常工作 : app hangs on initial logs and do not start [EDITED]

java - 从网络浏览器录制视频

java - 实例变量同步

java - 比较两个 arrayList<String> java 中的单词