我必须在Spring Batch作业中实现以下用例:
StoredProcedureItemReader
StoredProcedureItemReader
。我提出了以下策略:
ExecutionContext
ExecutionContext
我无法理解第二个SP ItemReader将如何访问第一个SP ItemReader返回的提供程序列表。分区有帮助吗?另外,有没有更好的策略来实现这一目标?
编辑1:
这是ItemProcessor的原始实现(作为浓缩):
@Scope("step")
public class FetchReportFromProviderProcessor implements
ItemProcessor<RiscProvider, List<SogReportRecord>>, ItemStream {
StoredProcedureItemReader<SogReportRecord> reader = new StoredProcedureItemReader<SogReportRecord>();
private DataSource dataSource;
@Value("#{jobParameters['date']}")
private String date;
@Override
public List<SogReportRecord> process(final RiscProvider item) throws Exception {
SogReportRecord record = null;
List<SogReportRecord> records = new ArrayList<SogReportRecord>();
SqlParameter[] sqlParameters = new SqlParameter[] {new SqlParameter(OracleTypes.CURSOR)};
reader.setParameters(sqlParameters);
reader.setPreparedStatementSetter(new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(0, item.getPrefix());
ps.setString(1, date);
}
});
while( (record = reader.read()) != null ) {
records.add(record);
}
return records;
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void open(ExecutionContext executionContext)
throws ItemStreamException {
reader.setDataSource(dataSource);
reader.setProcedureName("RISC_GET_DAYMOVEINOUT");
reader.open(executionContext);
}
@Override
public void update(ExecutionContext executionContext)
throws ItemStreamException {
reader.update(executionContext);
}
@Override
public void close() throws ItemStreamException {
reader.close();
}
}
XML部分:
<batch:job id="SOG_MOVEINOUT_REPORT_GENERATOR">
<batch:step id="GET_REPORTS">
<batch:tasklet>
<batch:chunk reader="getProviders"
processor="fetchRecordsFromProvider"
writer="sogReportWriter" commit-interval="500" />
</batch:tasklet>
</batch:step>
</batch:job>
<!-- Reader to fetch list of providers -->
<bean id="getProviders" class="org.springframework.batch.item.database.StoredProcedureItemReader">
<property name="dataSource" ref="dataSource" />
<property name="procedureName" value="RISC_GET_PROVIDER" />
<property name="parameters">
<list>
<bean class="org.springframework.jdbc.core.SqlOutParameter">
<constructor-arg index="0" value="providers" />
<constructor-arg index="1">
<util:constant static-field="oracle.jdbc.OracleTypes.CURSOR" />
</constructor-arg>
</bean>
</list>
</property>
<property name="refCursorPosition" value="1" />
<property name="rowMapper">
<bean class="com.kpn.risc.ProviderRowMapper" />
</property>
</bean>
<bean id="fetchRecordsFromProvider" class="com.kpn.risc.FetchReportFromProviderProcessor">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sogReportWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<property name="resource" value="file:///${batch.job.report.dir}/report-#{stepExecutionContext['provider']}.csv" />
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.PassThroughFieldExtractor" />
</property>
</bean>
</property>
</bean>
在上面的代码中,处理器将其工作委托给SP ItemReader。但是在调用read()
方法之前,读者无法正确初始化。是否可以或建议在ItemReader
内拨打ItemProcessor
?
答案 0 :(得分:0)
您所描述的内容直接属于批处理的Driving Query模式。本质上,查询定义了另一个查询用来驱动它的查询的ids(或在你的情况下是提供者)。通常,ItemReader
读取id并将每个id传递给ItemProcessor
以进行丰富(“其他”查询)。然后将结果传递给ItemWriter
。
您可以在此处阅读Spring Batch文档的Common Batch Patterns部分中有关Driving Driving模式的更多信息:http://docs.spring.io/spring-batch/trunk/reference/html/patterns.html