【问题标题】:Send data to Spring Batch Item Reader (or Tasklet)将数据发送到 Spring Batch Item Reader(或 Tasklet)
【发布时间】:2020-08-24 17:12:46
【问题描述】:

我有以下要求:

  1. 一个端点 http://localhost:8080/myapp/jobExecution/myJobName/execute,它接收 CSV 并使用单一性来应用一些验证并生成一些 pojo 的列表。
  2. 将该列表发送到 Spring Batch Job 进行一些处理。
  3. 多个用户可以执行此操作。

我想知道使用 Spring Batch 是否可以做到这一点?

我正在考虑使用队列,放置数据并执行从该队列中提取对象的作业。但是如何确定如果其他人执行端点并且其他作业正在执行,Spring Batch 知道哪个 Item 属于某个执行?

【问题讨论】:

    标签: spring-boot spring-batch spring-batch-tasklet


    【解决方案1】:

    您可以使用队列或继续将步骤后生成的值列表与验证一起放入作业执行上下文中作为作业参数的一部分。

    下面是一个 sn-p,用于将列表存储到作业上下文并使用 ItemReader 读取列表。

    Snippet在一个Tasklet步骤中实现StepExecutionListener来放入构造好的List,

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        //tenantNames is a List<String> which was constructed as an output of an evaluation logic
        stepExecution.getJobExecution().getExecutionContext().put("listOfTenants", tenantNames); 
        return ExitStatus.COMPLETED;
    }
    

    现在“listOfTenants”作为步骤的一部分被读取,该步骤具有读取器(允许一次读取一个线程)、处理器和写入器。您还可以将其存储为 Queue 的一部分并在 Reader 中获取。片段供参考,

    public class ReaderStep implements ItemReader<String>, StepExecutionListener {
    
       private List<String> tenantNames;
    
       @Override
       public void beforeStep(StepExecution stepExecution) {
    
           try {
               tenantNames = (List<String>)stepExecution.getJobExecution().getExecutionContext()
                    .get("listOfTenants");
               logger.debug("Sucessfully fetched the tenant list from the context");
           } catch (Exception e) {
               // Exception block
           }
       }
    
       @Override
       public synchronized String read() throws Exception {
    
          String tenantName = null;
    
           if(tenantNames.size() > 0) {
              tenantName = tenantNames.get(0);
              tenantNames.remove(0);
              return tenantName;
           }
    
           logger.info("Completed reading all tenant names");
           return null;
       }
    
       // Rest of the overridden methods of this class..
    }
    

    【讨论】:

      【解决方案2】:

      是的。 Spring boot 将在不同的线程中执行这些作业。所以 Spring 知道哪些项目属于哪个执行。 注意:您可以使用类似记录相关 ID。这将帮助您过滤特定请求的日志。 https://dzone.com/articles/correlation-id-for-logging-in-microservices

      【讨论】:

        猜你喜欢
        • 2021-09-25
        • 1970-01-01
        • 2018-12-22
        • 2020-11-03
        • 2018-09-19
        • 2023-03-15
        • 2016-04-20
        • 1970-01-01
        • 2013-06-13
        相关资源
        最近更新 更多