我会尝试为您的不同点提供一些想法。
1 - 读取表值并将它们作为作业参数传递
我在这里看到 2 个解决方案:
您可以执行“手动”查询(即不使用 springbatch),然后执行业务逻辑以将结果作为 JobParameters 传递(您只需要 JobLauncher 或 CommandLineJobRunner,请参阅 Springbatch Documentation §4.4):
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean(jobName);
// Do your business logic and your database query here.
// Create your parameters
JobParameter parameter = new JobParameter(resultOfQuery);
// Add them to a map
Map<String, JobParameter> parameters = new HashMap<String, JobParameter>();
parameters.add("yourParameter", parameter);
// Pass them to the job
JobParameters jobParameters = new JobParameters(parameters);
JobExecution execution = jobLauncher.run(job, parameters);
另一种解决方案是添加JobExecutionListener 并覆盖方法beforeJob 进行查询,然后将结果保存在executionContext 中(然后您可以使用:#{jobExecutionContext[name]} 访问)。
@Override
public void beforeJob(JobExecution jobExecution) {
// Do your business logic and your database query here.
jobExecution.getExecutionContext().put(key, value);
}
在每种情况下,您都可以使用 SpringBatch ItemReader 进行查询。例如,您可以将 item reader 声明为侦听器的字段(不要忘记 setter)并将其配置为:
<batch:listener>
<bean class="xx.xx.xx.YourListener">
<property name="reader">
<bean class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="dataSource"></property>
<property name="sql" value="${yourSQL}"></property>
<property name="rowMapper">
<bean class="xx.xx.xx.YourRowMapper"></bean>
</property>
</bean>
</property>
</bean>
</batch:listener>
2 - 根据上一步的结果读取表格
再一次,您可以使用JobExecutionContext 在步骤之间存储和检索数据。然后,您可以实现 StepExecutionListener 来覆盖方法 beforeStep 并访问 StepExecution 这将引导您到 JobExecution。
3 - 将表格读取结果与文件读取结果一起发送
没有“默认”CompositeItemReader 可以让您同时阅读 2 个来源,但我认为这不是您真正想要做的。
对于您的情况,我将在 <batch:chunk> 中将“表格阅读器”声明为阅读器,然后声明一个自定义 ItemProcessor,它将具有另一个 ItemReader 字段。这位读者将是您的FlatFileItemReader。然后,您可以手动开始读取并在 process 方法中应用您的业务逻辑。