我如何将一个作业中从数据库中获取的值作为参数传递给 Spring 框架中的另一个作业?请提供示例代码。
2 回答
我猜你的工作是指计划任务。我处于同样的情况,其中 2 个工作相互依赖,例如:
<task:scheduled-tasks>
<task:scheduled ref="deleteExpiredAccountsJob" method="launch" fixed-delay="100000" />
</task:scheduled-tasks>
<task:scheduled-tasks>
<task:scheduled ref="emailDeletedAccountsConfirmationJob" method="launch" fixed-delay="100000" />
</task:scheduled-tasks>
我所做的是将 ACCOUNTS 表上的 DELETE 标志设置为 true,并让 emailDeletedAccountsConfirmationJob 只读取 DELETE = true 的 ACCOUNTS
另一种解决方案,如果你想在 ItemReaders 之间共享数据,那就是拥有一个 Java Spring 配置类“@Configuration”并在这个类中使用 @Bean 注释声明你的 ItemReaders,并且还有一个私有的同步 Map 或 List 将被共享在所有线程/作业或您的春季批处理步骤之间,您可以访问阅读器中的同步列表。
@Configuration
public class MySpringConfig {
private List<String> list = Collections.synchronizedList(new ArrayList<String>());
@Bean
@Scope("step")
public JdbcCursorItemReader readerOne(){
//add data to list
}
@Bean
@Scope("step")
public JdbcCursorItemReader readerTwo(){
//check for certain data in the list, if exists ... do something
}
在 Spring Batch 中,作业被认为是不同的用例,没有框架强加的概念关系。您可以将作业视为独立的处理应用程序。
典型的批处理系统由数据源和数据接收器(例如消息队列、数据库或文件系统)组成,因此一项作业正在生成数据,旨在由应用程序的另一部分处理。
您可以重新设计您的设计以使用任务而不是工作。如果您有多个相互依赖的流程步骤,这将很有用。有一种方法可以从 StepExecution 访问 JobExecution。
我推荐阅读优秀的文章以及“Spring Batch in Action”。