This article documents the application of pipeline design patterns in business process orchestration.
Pipeline mode refers to pipeline mode, also known as pipeline mode. The incoming data is designed to be processed through a pre-defined series of stages, with the output of each stage being the input of the next stage.
In this example, by defining a pipelineproduct, a pipelinejob, and a pipelinenode, the assembly of an entire assembly line is completed, and the "raw materials" are processed into "commodities". Among them, the pipeline product is responsible for carrying the product information of each stage;The pipeline task is responsible for the processing of the product at different stages;Pipeline nodes constrain the relationship between pipeline products and tasks, and define the execution mode of tasks through semaphores.
The tool dependencies are as follows.
cn.hutool hutool-all latest version
package com.example.demo.pipeline.model;** Pipeline Product Interface * param semaphore * author * date 2023 05 15 11:49 * Public interface PipelineProduct {}
package com.example.demo.pipeline.model;** Pipeline Task Interface * param Pipeline Product * author * date 2023 05 15 11:52 * @functionalinterfacepublic interface pipelinejob * p execute(p product);}
package com.jd.baoxian.mall.market.service.pipeline.model;import j**a.util.function.predicate;** Pipeline Node Definition * param semaphore * param pipeline product * author * date 2023 05 15 11:54 * public interface pipelinenode>package comexample.demo.api;import com.example.demo.model.request.demoreq;import com.example.demo.model.response.demoresp;import com.example.demo.pipeline.factory.pipelineformanagersubmit;import org.springframework.stereotype.service;import j**ax.annotation.resource;** demo-api * author * date 2023 08 06 16:27 * @servicepublic class demomanagerapi * public demoresp managersubmit(demoreq requestdata) }package comexample.demo.model.request;** Demo input parameter * author * date 2023 08 06 16:33 * public class demoreq {}package com.example.demo.model.response;import lombok.data;** Demo parameters * author * date 2023 08 06 16:33 * @datapublic class demoresp * public static demoresp buildres(string message) }It is assumed that the process of reviewing the submission needs to include: parameter validation, locking, unlocking, and transaction commit.
package com.example.demo.pipeline.factory.job;import cn.hutool.json.jsonutil;import com.example.demo.model.request.demoreq;import com.example.demo.pipeline.factory.demopipelineproduct;import lombok.extern.slf4j.slf4j;import org.springframework.stereotype.service;** Lock-implementation layer * author * date 2023 05 17 17:00 * @service@slf4jpublic class checkrequestlockjob extends abstractdemojob ] lock, thread number: {}", jsonutil.tojsonstr(userrequestdata), tradeid); return demopipelineproduct.demosignalenum.normal; }package com.example.demo.pipeline.factory.job;import cn.hutool.json.jsonutil;import com.example.demo.model.request.demoreq;import com.example.demo.pipeline.factory.demopipelineproduct;import lombok.extern.slf4j.slf4j;import org.springframework.stereotype.service;** Unlock-Implementation-Layer * author * date 2023 05 17 17:00 * @service@slf4jpublic class checkrequestunlockjob extends abstractdemojob ] unlocked, thread number: {}", jsonutil.tojsonstr(userrequestdata), tradeid); return demopipelineproduct.demosignalenum.normal; }package com.example.demo.pipeline.factory.job;import cn.hutool.json.jsonutil;import com.example.demo.model.request.demoreq;import com.example.demo.pipeline.factory.demopipelineproduct;import lombok.extern.slf4j.slf4j;import org.springframework.stereotype.component;** Audit-Parameter Validation-Implementation Class * author * date 2023 05 15 19:50 * @slf4j@componentpublic class managerCheckParamJob extends abstractdemojob ] Input parameter validation, thread number: {}", jsonutil.tojsonstr(userrequestdata), tradeid);Non-Null Verification Valid Verification The verification is passed, and the return demopipelineproductdemosignalenum.normal; }package com.example.demo.pipeline.factory.job;import cn.hutool.json.jsonutil;import com.example.demo.model.request.demoreq;import com.example.demo.model.response.demoresp;import com.example.demo.pipeline.factory.demopipelineproduct;import lombok.extern.slf4j.slf4j;import org.springframework.stereotype.service;** Review - Information Submission - Business Implementation * author * date 2023 05 12 14:36 * @service@slf4jpublic Class ManagerSubmitJob extends AbstractDemojob ] Information submission, thread number: {}", jsonutil.tojsonstr(userrequestdata), tradeid); productdata.setuserresponsedata(demoresp.buildres("Success"));catch (exception ex) ", jsonutil.tojsonstr(userrequestdata), ex); throw ex; }return demopipelineproduct.demosignalenum.normal; }For the input and return parameter conversion, the pipeline task execution sequence and the execution semaphore are constructed.
package com.example.demo.pipeline.factory;import com.example.demo.model.request.demoreq;import com.example.demo.model.response.demoresp;import com.example.demo.pipeline.factory.job.checkrequestlockjob;import com.example.demo.pipeline.factory.job.checkrequestunlockjob;import com.example.demo.pipeline.factory.job.managercheckparamjob;import com.example.demo.pipeline.factory.job.managersubmitjob;import lombok.requiredargsconstructor;import lombok.extern.slf4j.slf4j;import org.springframework.stereotype.service;import j**ax.annotation.postconstruct;import j**a.util.objects;import j**a.util.uuid;** Pipeline Factory Entrance - Audit Pipeline * author * date 2023 05 15 19:52 * @slf4j@service@requiredargsconstructorpublic class PipelineForManagerSubmit ** Assembly Process Chain * private void AssemblyManagerSubmit() ** Review - Submit processing * param requestdata input parameter * return * public demoresp managersubmitcheck(demoreq requestdata) return finalproductgetproductdata().getuserresponsedata();** Review - Initialize the pipeline data of the request * param requestdata input parameter * return the initial pipeline data * private demopipelineProduct Manager submitCheckInitial(Demoreq requestdata) }This article focuses on the abstraction and application of the pipeline pattern, and the above examples are for personal understanding only. In practice, this case is better than dealing with various business scenarios with redundant rules, and is convenient for rule orchestration.
Points to be improved: Each task actually implies the order of execution, which can be further realized
For the final step of "pipeline assembly", it can be further abstracted by configuring the description, so that the changes can be controlled on the description of each "pipeline task", and the rule items can be "pluggable".
Author: JD Insurance Hou Yadong **Please indicate**.