我有一个spring批处理应用程序
Model
是
ResourceAware
.
我正在应用程序中读取多个文件:
@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;
}