资源名称未正确反映在第二个文件上

时间:2018-02-16 20:15:06

标签: java spring spring-batch

我有一个春季批处理应用,ModelResourceAware

我正在阅读我的应用程序中的多个文件:

@Data
@Slf4j
public class Model implements ResourceAware {


private String countryCode;
private Resource resource;

public Resource getResource() {
    return resource;
}

@Override
public void setResource(Resource resource) {
    this.resource = resource;
}

}

@Configuration
public class BatchConfiguration {

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Autowired
private StepBuilderFactory stepBuilderFactory;

@Autowired
private MyReader reader;


@Bean
@StepScope
public MultiResourceItemReader<Model> multiResourceItemReader() {

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

    MultiResourceItemReader<Model> resourceItemReader = new MultiResourceItemReader<>();

    final File resourceLocation = FileManager.getResourcesLocationAsFile(filePath);

    String url = "file:" + resourceLocation.getAbsolutePath();

    if (resourceLocation.isDirectory()) {
        url = url + SEPERATOR + filePattern + "*";
    }

    Resource[] resources = new Resource[0];


    try {
        resources = resolver.getResources(url);
    } catch (IOException e) {
    }

    resourceItemReader.setStrict(true);
    resourceItemReader.setResources(resources);
    resourceItemReader.setDelegate(reader());

    return resourceItemReader;
}

@Bean
@StepScope
public FlatFileItemReader<Model> reader() {
    return reader.getReader();
}


@Bean
public ItemProcessor<Model, SCPModel> modelProcessor() {
    return new ModelProcessor();
}

}

当我写入数据库时​​,文件名始终是第一个处理的文件,即使第二个文件记录已插入数据库中?

我在这里错过了什么伎俩?在我的processor中,我将文件名设置如下:

@Override
    public SCPModel process(final Model model) throws Exception {

        scp.setResource(model.getResource().getFilename());
        return scp;
    }

1 个答案:

答案 0 :(得分:0)

事实证明,我的资源文件之一在数据库中没有正确的静态设置。我打印到控制台的项目,资源被正确反映。