if ("618".equals(day)){ System.out.println("668"); }else if ("11.11".equals(day)){ System.out.println("888"); }else if ("12.12".equals(day)){ System.out.println("180"); }else{ System.out.println("998"); }public interface NodeComponent { void process() ;}@Componentpublic class AComponent extends NodeComponent { @Override public void process() { /** * 这里逻辑省略
*
*/
System.out.println("618");
}
}
@Component
public class BComponent extends NodeComponent {
@Override
public void process() {
/**
* 这里逻辑省略
*
*/
System.out.println("11.11");
}
}
//下面省略12.12//主流程@Componentpublic class TestFlow { @Resource private Map<String, NodeComponent> map; public void run(String name) throws Exception { for (Map.Entry<String, NodeComponent> entry : map.entrySet()) { String key = entry.getKey(); if (StringUtils.equals(name,key)){ entry.getValue().process(); } } }}LiteFlow就是为解耦复杂逻辑而生,如果你要对复杂业务逻辑进行新写或者重构,用liteflow最合适不过。它是一个轻量,快速的组件式流程引擎框架,组件编排,帮助解耦业务代码,让每一个业务片段都是一个组件,并支持热加载规则配置,实现即时修改。
使用Liteflow,你需要去把复杂的业务逻辑按代码片段拆分成一个个小组件,并定义一个规则流程配置。这样,所有的组件,就能按照你的规则配置去进行复杂的流转。
image.png
Liteflow适用于拥有复杂逻辑的业务,比如说价格引擎,下单流程,规则校验等,这些业务往往都拥有很多步骤,这些步骤完全可以按照业务粒度拆分成一个个独立的组件,进行装配复用变更。使用Liteflow,你会得到一个灵活度高,扩展性很强的系统。因为组件之间相互独立,也也可以避免改一处而动全身的这样的风险。
Liteflow的Gitee仓库:https://gitee.com/dromara/liteFlow
Liteflow的Github仓库:https://github.com/dromara/liteflow
<dependency> <groupId>com.yomahub</groupId> <artifactId>liteflow-spring-boot-starter</artifactId> <version>${project.parent.version}</version> </dependency>@Component("a")public class AComponent extends NodeComponent { @Override public void process() { String str = this.getSlot().getRequestData(); System.out.println(str); System.out.println("Acomponent executed!"); this.getSlot().setOutput(this.getNodeId(), "A component output"); }}@Component("b")public class BComponent extends NodeComponent { @Override public void process() { try { Thread.sleep(400L); String[] temp = new String[1000]; } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Bcomponent executed!"); }}@Componentpublic class TestFlow{ @Resource private FlowExecutor flowExecutor; @Override public void run(String... args) throws Exception { Slot slot = flowExecutor.execute("chain3", "it's a request"); System.out.println(slot); }}image.png
<?xml version="1.0" encoding="UTF-8"?><flow> <chain name="chain1"> <then value="a,cond(b|d)"/> <!-- cond是条件节点,根据cond里的逻辑决定路由到b节点还是d节点,可以配置多个 --> <then value="e,f,g"/> </chain> <chain name="chain2"> <then value="a,c"/> <!-- then表示串行 --> <when value="b,d"/> <!-- when表示串行 --> <then value="e,f,g"/> </chain></flow>liteflow.rule-source=config/flow.xml#------以下非必须-------#slot的数量,默认值为1024liteflow.slot-size=2048#异步线程最长的等待时间秒(只用于when),默认值为15liteflow.when-max-wait-second=20#是否开启监控log打印,默认值为falseliteflow.monitor.enable-log=true#监控队列存储大小,默认值为200liteflow.monitor.queue-limit=300#监控一开始延迟多少执行,默认值为300000毫秒,也就是5分钟liteflow.monitor.delay=10000#监控日志打印每过多少时间执行一次,默认值为300000毫秒,也就是5分钟liteflow.monitor.period=100001) 引入依赖2) 定义流程节点继承NodeComponent 3) 注入: @Resource private FlowExecutor flowExecutor;4)application.properties: liteflow.rule-source=config/flow.xml原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。