简体   繁体   English

异步Spring Batch。 分两步创建工作

[英]Asynchronous Spring Batch. Create job with 2 steps

I have a requirement to call a webservice multiple times and pass data that is created using data from multiple Tables 我需要多次调用Web服务并传递使用多个表中的数据创建的数据

UI -> Controller -> Service -> ( fetch data ( Table1, table2) and run some validation) 1. If validation is failed - return error message and stop. UI->控制器->服务->(获取数据(Table1,Table2)并运行一些验证)1.如果验证失败-返回错误消息并停止。 2. If validation is passed - call JobLauncher and return "Task launched" message. 2.如果通过验证-呼叫JobLauncher并返回“任务已启动”消息。

In The asynchronous job , I think of following 2 steps. 在异步作业中,我想到了以下两个步骤。

  1. BulkInsertStep 批量插入步骤

    • I need to call DB to query 2 more tables ( Table3, table4) and create a large dataset, from earlier fetched Table1, Table2 and current fetched table3, table4 我需要调用DB来查询另外2个表(Table3,table4),并从早期获取的Table1,Table2和当前获取的table3,table4创建一个大型数据集

    • Insert created dataset into table5 将创建的数据集插入到table5中

  2. WebServiceStep ( AysncTaskExecutor) WebServiceStep(AysncTaskExecutor)

    • Reader: Query from Table5 读者:来自Table5的查询
    • Processor: Call Webservice for each row. 处理器:为每一行调用Web服务。
    • Writer: update Table5 编写器:更新Table5

I dont know if BulkInsertStep qualifies to be step in BatchJob or not. 我不知道BulkInsertStep是否资格成为BatchJob中的步骤。 Basically its a for loop in for loop in for loop to create a List of DTO objects. 基本上,它的for循环在for循环中在for循环中创建DTO对象的列表。 What goes to reader, processor or writer is little confusing.. 读取器,处理器或写入器的内容很少令人困惑。

Avoiding BulkInsertStep avoids bulk insert and also reading in WebServiceStep , but if later user wants to rerun failed webservice records, it will be hard to figure out what permutations has already been processed and what permutations needs to be processed. 避免BulkInsertStep可以避免批量插入,也可以读取WebServiceStep ,但是如果以后用户想要重新运行失败的Webservice记录,则将很难弄清已经处理了哪些排列以及需要处理哪些排列。

Please suggest a design or another way to achieve the same thing. 请提出一种设计或另一种方法来实现同一目标。 Requirements: 要求:

  1. Webservice calls for large data takes some time. Web服务调用大数据需要一些时间。 So UI cannot wait for response. 因此,UI无法等待响应。
  2. User can rerun task, which processes failed tasks. 用户可以重新运行任务,该任务将处理失败的任务。

I dont know if BulkInsertStep qualifies to be step in BatchJob or not. 我不知道BulkInsertStep是否有资格成为BatchJob中的步骤。

I think you definetely need to have this as Reader-Processor-Writer . 我认为您绝对需要将此作为Reader-Processor-Writer While it is a bit complicated to write it pays off to you with such abilities as Restartability and Failure tolerance. 尽管编写起来有点复杂,但它具有诸如重新启动性和故障容忍性之类的功能,可为您带来回报。 I was also thinking before that Reader-Processor-Writer is an overkill, but at the end it also leads to better structured and maintainable code. 我还曾想过, Reader-Processor-Writer是一个过大的杀伤力,但最终它还会导致更好的结构化和可维护的代码。

Avoiding BulkInsertStep avoids bulk insert and also reading in WebServiceStep, but if later user wants to rerun failed webservice records, it will be hard to figure out what permutations has already been processed and what permutations needs to be processed. 避免BulkInsertStep可以避免批量插入,也可以在WebServiceStep中读取,但是如果以后用户想要重新运行失败的Webservice记录,则将很难弄清已经处理了哪些排列以及需要处理哪些排列。

I've done something similar before. 我以前做过类似的事情。 I would recommend here to store after every call to your WebService the result in the database. 我建议在每次调用WebService之后将结果存储在数据库中。 This is how table could look like: 这是表格的样子:

- requestId 
- requestURL
- method (GET/POST/PUT/etc.) 
- requestBody (CLOB)
- requestHeaders 
- responseCode 
- responseBody (CLOB)
- responseHeaders

With this table I could be sure that there will be no duplicate WebService calls. 使用此表,我可以确定不会有重复的WebService调用。 If the batch job fails, I could simply restart Job on this step and continue processing starting with uncompleted calls. 如果批处理作业失败,则可以在此步骤上重新启动Job并继续以未完成的调用开始处理。

For this you will need to have additional step to convert your data into requests (to prepare them), and then again using reader-processor-writer do WebService calls. 为此,您将需要采取其他步骤将数据转换为请求(以准备请求),然后再次使用reader-processor-writer进行WebService调用。

Webservice calls for large data takes some time. Web服务调用大数据需要一些时间。 So UI cannot wait for response. 因此,UI无法等待响应。

I assume you will start anyways whole job in separate thread. 我认为您无论如何都要在单独的线程中开始整个工作。 You need to configure your job launcher that it will use ThreadPoolTaskExecutor . 您需要配置作业启动器,使其使用ThreadPoolTaskExecutor Then job will run asyncronously. 然后作业将异步运行。

User can rerun task, which processes failed tasks. 用户可以重新运行任务,该任务将处理失败的任务。

This is simply achieved. 这是简单实现的。 You have two options : 您有两种选择:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM