您好,登錄后才能下訂單哦!
本篇內容主要講解“stepchain框架有什么作用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“stepchain框架有什么作用”吧!
stepchain 通用業務流程流水線處理框架。
類似于Commons Chain和Commons Pipeline這樣的Java Pipeline Step Chain用于組織復雜處理流程執行的流行技術。
Feature: 1、支持通用業務job、services子流程無限制拆分。 2、支持業務子流程串行化、業務子流程并行化,可配置化。 3、支持Config業務子流程開啟或禁用、配置串行或并行以及并行數的統一配置。 4、支持業務流程以及子流程任意無限嵌套。 5、支持配置中心、緩存、統一數據接口、redis、Es、日志Trace等。 6、支持并行分支,支持條件分支if/else、switch、loop子流程. 7、支持Processor定時調度FixedRate、FixedDelay。 備注:只開源了通用部分(不影響使用),去除了有關框架組件包括:配置中心、緩存中心、數據接口以及業務相關DataMiddle等部分API。
Maven Dependency: Maven(Not Use Spring Boot): <dependency> <groupId>com.github.zengfr.project</groupId> <artifactId>stepchain</artifactId> <version>0.0.7</version> <dependency> Maven(Use Spring Boot): <dependency> <groupId>com.github.zengfr.project</groupId> <artifactId>stepchain-spring-boot-starter</artifactId> <version>0.0.7</version> <dependency> Gradle: compile group: 'com.github.zengfr.project', name: 'stepchain', version: '0.0.7' compile group: 'com.github.zengfr.project', name: 'stepchain-spring-boot-starter', version: '0.0.7'
1、StepChain 的中心思想是什么?如何做到通用的? 答: 1.1、任何業務邏輯處理抽象成1\input輸入 2\ processor處理器 3\output輸出.中間過程結果產生和組合成dataMiddle。 1.2、任何業務邏輯處理使用多個processor組合執行。 2、StepChain 如何并行和串行執行多個processor? 答: 串行step=pipeline.createStep();step.put(processors);//processors串行執行. 并行step=pipeline.createStep(4);step.put(processors);//processors同時4個并行執行. 3、Stepchain 如何創建processor? 3.1、實現 IProcessor 接口。 3.2、使用IProcessorBuilder: <I> IProcessor<I, Boolean> createProcessor(Predicate<I> predicate); <I> IProcessor<I, Boolean> createProcessor(Consumer<I> consumer); <I, O> IProcessor<I, O> createProcessor(Function<I, O> func); 4、StepChain 如何復用和組合processor? 4.1、使用IChainBuilder、IChain: 4.2、使用IProcessorBuilder: <A, B, C> IProcessor<A, C> createProcessor(IProcessor<A, B> first, IProcessor<B, C> second); <A, B, C, D> IProcessor<A, D> createProcessor(IProcessor<A, B> processor1, IProcessor<B, C> processor2, IProcessor<C, D> processor3); 5、StepChain 如何按條件復用和組合processor? 答: case1、已有trueProcessor\falseProcessor2個 創建 validator 則按條件執行2則之1. IConditionSelectorProcessor<String, Boolean, String> p3 = pipeline.createConditionValidatorProcessor(validator, trueProcessor, falseProcessor); case2、已有processor 創建 validator 創建循環執行體,validator 返回false時終止執行。 IConditionLoopProcessor<String, String> p2 = pipeline.createConditionLoopProcessor(validator, processor); case3、已有processor創建 switch 邏輯,根據selector返回的key執行某1分支branchProcessor如果返回的key不在分支中 則執行默認key對應的分支branchProcessor。 IConditionSelectorProcessor<String, String, String> p1 = pipeline.createConditionSelectorProcessor(selector); p1.setBranch(S key, IProcessor<I, O> processor); p1setDefaultBranch(S key); case4、已有processor創建 if/else if/else 邏輯,根據validator返回的結果與result對比一致則執行分支branchProcessor,如果沒有返回一致的 則執行默認分支branchProcessor。 pipeline.createConditionValidatorSelectorProcessor(); public interface IConditionValidatorSelectorProcessor<I,O> extends IProcessor<I, O> { void setBranch(IProcessor<I, Boolean> validator,Boolean result,IProcessor<I, O> processor); void setDefaultBranch(IProcessor<I, O> processor); }
public interface IStep<I> extends IStepProcessor<I> { void put(IStepProcessor<I> processor); void put(IStepProcessor<I>... processorArray); void put(Collection<StepProcessor<I>> processors); void put(IProcessor<I, Boolean> processor); void put(IProcessor<I, Boolean>... processorArray); void put(IChain<I, Boolean> chain); void put(IChain<I, Boolean>... processorArray); void put(Function<I, Boolean> func); void put(Function<I, Boolean>... processorArray); } public interface IChain<A, B> extends IProcessor<A, B> { <C> IChain<A, C> next(IProcessor<B, C> process); <C> IChain<A, C> next(Function<B, C> func); } public interface IChainBuilder { <A, B> IChain<A, B> createChain(Function<A, B> func); <A, B> IChain<A, B> createChain(IProcessor<A, B> processor); <A, B, C> IChain<A, C> createChain(IProcessor<A, B> processor1, IProcessor<B, C> processor2); } public interface IStepBuilder { <T> IStep<T> createStep(); <T> IStep<T> createStep(int parallelCount); <T> IStep<T> createStep(String parallelCountConfigName); }
StepChainSpringBootTest.java
PipelineTest.java
Demo&Test you can use AbstractProcessor AbstractStepProcessor
import com.github.zengfr.project.stepchain abstract class AbstractProcessor<I, O> implements Processor<I, O>{} abstract class AbstractStepProcessor<A> extends AbstractProcessor<A, Boolean> implements StepProcessor<A>{}
import com.github.zengfr.project.stepchain.Chain; import com.github.zengfr.project.stepchain.Pipeline; import com.github.zengfr.project.stepchain.Step; import com.github.zengfr.project.stepchain.context.ContextBuilder; import com.github.zengfr.project.stepchain.context.UnaryContext; import com.github.zengfr.project.stepchain.test.context.SetProductContext; import com.github.zengfr.project.stepchain.test.context.SetProductDataMiddle; import com.github.zengfr.project.stepchain.test.processor.DiscountProcessor; import com.github.zengfr.project.stepchain.test.processor.FeeProcessor; import com.github.zengfr.project.stepchain.test.processor.IncreaseProcessor; import com.github.zengfr.project.stepchain.test.processor.InitProcessor; import com.github.zengfr.project.stepchain.test.processor.TaxProcessor; public class PipelineTest { public static void testPipeline(IPipeline pipeline) throws Exception { //Demo精簡版 只開源了通用部分(不影響使用) SetProductRequest req = new SetProductRequest(); SetProductResponse resp = new SetProductResponse(); SetProductDataMiddle middle = new SetProductDataMiddle(); SetProductContext context = new SetProductContext(req, middle, resp); IStep<SetProductContext> step = pipeline.createStep(); step.put(new InitProcessor()); step.put(new TaxProcessor()); step.put(new FeeProcessor()); step.put(new IncreaseProcessor()); step.put(new DiscountProcessor()); step.put((c) -> { c.middle.Price += 10; return true; }); step.process(context); System.out.println(context.middle.Price); } public static void testPipeline2(IPipeline pipeline) throws Exception { Function<UnaryContext<Integer>, Boolean> func = (context) -> { if (context.context == null) context.context = 1; context.context += 1; return true; }; Function<UnaryContext<Integer>, String> func3 = (context) -> { if (context.context == null) context.context = 1; context.context += 1; return JSON.toJSONString(context.context); }; UnaryContext<Integer> context = pipeline.createContext(12345678); IStep<UnaryContext<Integer>> step = pipeline.createStep(); IStep<UnaryContext<Integer>> step2 = pipeline.createStep(); IChain<UnaryContext<Integer>, Boolean> c2 = pipeline.createChain(func); IChain<UnaryContext<Integer>, String> c3 = pipeline.createChain(func3); Function<String, Integer> func4 = null; Function<Integer, String> func5 = null; Function<String, Boolean> func6 = null; IChain<String,Integer > c4 = pipeline.createChain(func4); IChain<Integer, String> c5 = pipeline.createChain(func5); IChain<String, Boolean> c6 = pipeline.createChain(func6); IChain<UnaryContext<Integer>, Boolean> c7 = c3.next(c4).next(c5).next(c6); step2.put(c2); step2.put(step); step2.put(func); //step2.put(c7); step2.process(context); System.out.println(context.context); } public static void testPipeline3(IPipeline pipeline) throws Exception { IProcessor<String, String> selector = null; IProcessor<String, Boolean> validator = null; IProcessor<String, String> processor = null; IProcessor<String, String> first = null; IProcessor<String, String> second = null; IConditionSelectorProcessor<String, Boolean, String> p3 = pipeline.createConditionValidatorProcessor(validator, first, second); IConditionLoopProcessor<String, String> p2 = pipeline.createConditionLoopProcessor(validator, processor); IConditionSelectorProcessor<String, String, String> p1 = pipeline.createConditionSelectorProcessor(selector); }
@RunWith(SpringRunner.class) @SpringBootTest(classes = StepChainTestApplication.class) public class StepChainSpringBootTest { @Autowired protected IPipeline pipeline; @Test public void testPipeline() throws Exception { PipelineTest.testPipeline(pipeline); } @Test public void testPipeline2() throws Exception { PipelineTest.testPipeline2(pipeline); }
到此,相信大家對“stepchain框架有什么作用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。