【问题标题】:Passing parameters from one job to another in Spring batch framework在 Spring 批处理框架中将参数从一个作业传递到另一个作业
【发布时间】:2023-04-02 07:39:01
【问题描述】:

如何将一个作业中从数据库中获取的值作为参数传递给 Spring 框架中的另一个作业?请提供示例代码。

【问题讨论】:

  • “工作”是什么意思?石英工作? @预定?还有什么?
  • 你指的是 Spring Batch 和 Spring Batch Jobs 吗?

标签: spring spring-batch


【解决方案1】:

我猜你指的是计划任务。我处于同样的情况,即 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
    }

【讨论】:

    【解决方案2】:

    在 Spring Batch 中,作业 被视为不同的用例,框架没有强加概念上的关系。您可以将作业视为独立的处理应用程序。

    典型的批处理系统由数据源和数据接收器(如消息队列、数据库或文件系统)组成,因此一项作业正在生成数据,旨在由应用程序的另一部分处理。

    您可以重新设计您的设计以使用任务而不是工作。如果您有多个相互依赖的流程步骤,这将很有用。 一种从 StepExecution 访问 JobExecution 的方法。

    我推荐阅读优秀文章以及“Spring Batch in Action”。

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多