将数据发送到 Spring Batch Item Reader(或 Tasklet) [英] Send data to Spring Batch Item Reader (or Tasklet)

查看:40
本文介绍了将数据发送到 Spring Batch Item Reader(或 Tasklet)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要求:

  1. 一个端点 http://localhost:8080/myapp/jobExecution/myJobName/execute,它接收一个 CSV 并使用 univocity 应用一些验证并生成一些 pojo 的列表.
  2. 将该列表发送到 Spring Batch 作业进行一些处理.
  3. 多个用户可以这样做.

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

I want to know if with Spring Batch I can achieve this?

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

I was thinking to use a queue, put the data and execute a Job that pull objects from that queue. But how can I be sure that if other person execute the endpoint and other Job is executing, Spring Batch Knows which Item belongs to a certain execution?

推荐答案

您可以使用队列或继续将步骤后生成的值列表与验证一起放置,并将其作为作业参数的一部分存储在作业中执行上下文.

You can use a queue or go ahead to put the list of values that was generated after the step with validations and store it as part of job parameters in the job execution context.

以下是将列表存储到作业上下文并使用 ItemReader 读取列表的片段.

Below is a snippet to store the list to a job context and read the list using an ItemReader.

Snippet 在 Tasklet 步骤中实现 StepExecutionListener 以放置构建的 List,

Snippet implements StepExecutionListener in a Tasklet step to put List which was constructed,

@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 中获取它.片段供参考,

Now "listOfTenants" are read as part of a Step which has Reader (To allow one thread read at a time), Processor and Writer. You can also store it as a part of Queue and fetch it in a Reader. Snippet for reference,

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..
}

这篇关于将数据发送到 Spring Batch Item Reader(或 Tasklet)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆