我有一个在启动spring boot应用程序后运行的批处理(未配置启动程序,仅由于spring.batch.job.enabled=true
我的工作正在运行)。
我的工作目的是从数据库中导出csv文件。
创建CSV文件后,我想压缩该文件。 我可以在其中压缩的步骤中,在itemWriter中尝试过,压缩后的文件为空,
我的源代码是:
@Bean
public FlatFileItemWriter<User> writer(){
log.info("start writing user ... ");
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.systemDefault());
Instant instant = Instant.now();
date = formatter.format(instant);
date = date.replaceAll("[/,\\s,:]", "-");
FlatFileItemWriter<User> writer = new FlatFileItemWriter<User>();
filePath = config.getPath + date + ".csv";
writer.setResource(new FileSystemResource (filePath));
writer.setAppendAllowed(true);
writer.setLineAggregator(new DelimitedLineAggregator<User>() {
{
setDelimiter(",");
setFieldExtractor(new BeanWrapperFieldExtractor<User>() {
{
setNames(new String[] { "Id", "Name" });
}
});
}
});
writer.close();
try {
File file = new File(config.getPath() + "\\" + date + ".zip");
ZipOutputStream zop = new ZipOutputStream(new FileOutputStream (file));
ZipEntry entry = new ZipEntry(filePath);
zop.putNextEntry(entry);
zop.close();
} catch (Exception e) {
e.printStackTrace();
}
return writer;
}
答案 0 :(得分:0)
我可以在其中压缩的步骤中,在itemWriter中尝试过,压缩后的文件为空,
您需要创建两个步骤:
您的配置不正确。您不需要调用writer.close
,Spring Batch会在编写者生命周期中进行调用。您需要做的是声明一个ItemWriter
类型的bean,并将邮政编码移到另一个步骤。例如:
@Bean
public JdbcCursorItemReader<User> jdbcCursorItemReader() {
// define and return the reader
}
@Bean
public FlatFileItemWriter<User> writer(){
log.info("start writing user ... ");
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withZone(ZoneId.systemDefault());
Instant instant = Instant.now();
date = formatter.format(instant);
date = date.replaceAll("[/,\\s,:]", "-");
FlatFileItemWriter<User> writer = new FlatFileItemWriter<User>();
filePath = config.getPath + date + ".csv";
writer.setResource(new FileSystemResource (filePath));
writer.setAppendAllowed(true);
writer.setLineAggregator(new DelimitedLineAggregator<User>() {
{
setDelimiter(",");
setFieldExtractor(new BeanWrapperFieldExtractor<User>() {
{
setNames(new String[] { "Id", "Name" });
}
});
}
});
return writer;
}
@Bean
public Step exportFile() {
return stepBuilderFactory.get("exportFile")
.chunk(100)
.reader(jdbcCursorItemReader())
.writer(writer())
.build();
}
@Bean
public Step compressFile() {
return stepBuilderFactory.get("compressFile")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
try {
File file = new File(config.getPath() + "\\" + date + ".zip");
ZipOutputStream zop = new ZipOutputStream(new FileOutputStream (file));
ZipEntry entry = new ZipEntry(filePath);
zop.putNextEntry(entry);
zop.close();
} catch (Exception e) {
e.printStackTrace();
}
return RepeatStatus.FINISHED;
}
});
}
然后按照正确的顺序定义这些步骤的工作。