我正在使用Spring Batch Java配置。我的工作需要从数据库中读取一些数据并写入文件,以及根据某种情况对表进行更新。我正在使用CompositeItemWriter,一位作家将文件写入文件,另一位作家将值更新到表中。我的问题是,当作业在块中间失败时,可以将文件写入文件,但是不会回滚更新的数据。如果有人有任何建议,请尽快通知我。这是我的配置类
{
@Configuration
public class AchFulfillmentEmailBatchConfig extends AbstractCommonBatchJobConfig {
@Bean(name = "achFulfillmentEmailCompositeWriter")
public CompositeItemWriter<AchFulfillmentEmail> achFulfillmentEmailCompositeWriter() {
CompositeItemWriter<AchFulfillmentEmail> writer = new CompositeItemWriter<AchFulfillmentEmail>();
writer.setDelegates(Arrays.asList(achFulfillmentEmailFileWriter(), achFulfillmentEmailUpdateWriter()));
return writer;
}
@Bean(name = "achFulfillmentEmailFileWriter")
public AchFulfillmentEmailFileWriter achFulfillmentEmailFileWriter() {
return new AchFulfillmentEmailFileWriter();
}
@Bean(name = "achFulfillmentEmailFlatFileWriter")
public FlatFileItemWriter<AchFulfillmentEmail> achFulfillmentEmailFlatFileWriter() {
mmLogMgr.info("AchFulfillmentEmailWriter: achFulfillmentEmailFlatFileWriter");
String fileName = AchFulfillmentBatchJobUtil.getFileName(ACH_FULFILLMENT_EMAIL_JOB_NAME);
FlatFileItemWriter<AchFulfillmentEmail> emailFulfillmentFlatFileWriter = new FlatFileItemWriter<>();
emailFulfillmentFlatFileWriter.setResource(new FileSystemResource(fileName));
DelimitedLineAggregator<AchFulfillmentEmail> delLineAgg = new DelimitedLineAggregator<AchFulfillmentEmail>();
delLineAgg.setDelimiter(AchFulfillmentBatchJobUtil.getColumnDelimiter(ACH_FULFILLMENT_EMAIL_JOB_NAME));
BeanWrapperFieldExtractor<AchFulfillmentEmail> fieldExtractor = new BeanWrapperFieldExtractor<AchFulfillmentEmail>();
fieldExtractor.setNames(AchFulfillmentBatchJobUtil.getColumnNames(ACH_FULFILLMENT_EMAIL_JOB_NAME));
delLineAgg.setFieldExtractor(fieldExtractor);
emailFulfillmentFlatFileWriter.setLineAggregator(delLineAgg);
return emailFulfillmentFlatFileWriter;
}
@Bean(name = "achFulfillmentEmailListener")
public AchFulfillmentEmailListener achFulfillmentEmailListener() {
return new AchFulfillmentEmailListener();
}
@SuppressWarnings({"rawtypes", "unchecked"})
@Bean(name = "achFulfillmentEmailReader")
@Scope(value = "step", proxyMode = ScopedProxyMode.DEFAULT)
public ItemStreamReader<AchFulfillmentEmail> achFulfillmentEmailReader(@Value("#{jobExecutionContext['current.business.date']}") final String currentBusinessDate,
@Value("#{jobExecutionContext['start.business.date']}") final String startBusinessDate)
throws Exception {
if (currentBusinessDate == null) {
coreLogMgr.log(AchFulfillmentErrorConstants.ACH_FULFILLMENT_EMAIL_CURRENT_BUSINESS_DATE_NULL,
"currentBusinessDate is null.");
throw new Exception("currentBusinessDate is null.");
}
if (startBusinessDate == null) {
coreLogMgr.log(AchFulfillmentErrorConstants.ACH_FULFILLMENT_EMAIL_START_BUSINESS_DATE_NULL,
"startBusinessDate is null.");
throw new Exception("startBusinessDate is null.");
}
AchFulfillmentEmailReader achFulfillmentEmailReader = new AchFulfillmentEmailReader();
achFulfillmentEmailReader.setCurrentBusinessDate(new Date(
DateUtil.stringToDate(currentBusinessDate, TDACommonsConstants.DATE_FORMAT).getTime()));
achFulfillmentEmailReader.setStartBusinessDate(new Date(
DateUtil.stringToDate(startBusinessDate, TDACommonsConstants.DATE_FORMAT).getTime()));
achFulfillmentEmailReader.setDataSource(prodDataSource);
SqlStatementHelper achFulfillmentEmailReaderHelper =
new SqlStatementHelper("/sql/AchQueue.basesql.properties", "/sql/AchQueue.cachesql.properties");
String sqlStatement = achFulfillmentEmailReaderHelper.getSqlStatement("ACHQueue.RetrieveFulfillment");
achFulfillmentEmailReader.setSql(sqlStatement);
AchFulfillmentEmailMapper rowMapper = new AchFulfillmentEmailMapper();
achFulfillmentEmailReader.setRowMapper(rowMapper);
// achFulfillmentEmailReader.setSaveState(false);
List parameters = new ArrayList();
parameters.add(currentBusinessDate);
parameters.add(ProcessedFlag.TO_BE_PROCESSED.getCode());
parameters.add(QueuedType.EMAIL.getCode());
ListPreparedStatementSetter ps = new ListPreparedStatementSetter();
ps.setParameters(parameters);
achFulfillmentEmailReader.setPreparedStatementSetter(ps);
return achFulfillmentEmailReader;
}
@Bean(name = "achFulfillmentEmailStep1")
public Step achFulfillmentEmailStep1() throws BatchRuntimeException, Exception {
mmLogMgr.info("AchFulfillmentEmailBatchConfig : achFulfillmentEmailStep1");
return getStepBuilderFactory().get("achFulfillmentEmailStep1")
.allowStartIfComplete(true)
.<AchFulfillmentEmail, AchFulfillmentEmail> chunk(AchFulfillmentBatchJobUtil.getChunkValue("achfulfillmentemail"))
.reader(achFulfillmentEmailReader(null, null))
.writer(achFulfillmentEmailCompositeWriter())
.stream(achFulfillmentEmailFlatFileWriter())
.transactionManager(transactionManager)
.build();
}
@Bean(name = "achFulfillmentEmailUpdateWriter")
public ItemWriter<AchFulfillmentEmail> achFulfillmentEmailUpdateWriter() {
return new AchFulfillmentEmailUpdateWriter();
}
@Bean(name = "achFulfillmentEmailJob")
public Job job() throws BatchRuntimeException, Exception {
mmLogMgr.info("AchFulfillmentEmailBatchConfig : job");
return getJobBuilderFactory().get("achFulfillmentEmailJob")
.listener(achFulfillmentEmailListener())
.start(achFulfillmentEmailStep1())
.on(ExitStatus.FAILED.getExitCode())
.fail()
.on(ExitStatus.COMPLETED.getExitCode())
.end() // end of TransactionBuilder
.end() // end of FlowBuilder
.build();
}
}
}