人类永远会选择他们理解的,摒弃不理解的。这个世界唯一剩下的动物,就是他们已经驯服的那些,匍匐在他们的脚边,或者那些非常警觉,一察觉到他们靠近就逃离的动物,没有介于二者之间的。——《西部世界》
今天上手用了下liteflow
,感觉非常不错
首先引入
1 2 3 4 5
| <dependency> <groupId>com.yomahub</groupId> <artifactId>liteflow-spring-boot-starter</artifactId> <version>2.11.4.2</version> </dependency>
|
编写A
组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
import com.yomahub.liteflow.annotation.LiteflowComponent; import com.yomahub.liteflow.core.NodeComponent; import com.yomahub.liteflow.slot.DefaultContext;
@LiteflowComponent("a") public class ACmp extends NodeComponent {
@Override public void process() { System.out.println("ACmp"); var requestData = this.getRequestData(); System.out.println(requestData); DefaultContext firstContextBean = this.getFirstContextBean(); var arg = firstContextBean.getData("arg"); System.out.println(arg); firstContextBean.setData("arg", "v" + requestData); }
}
|
B
组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
import com.yomahub.liteflow.annotation.LiteflowComponent; import com.yomahub.liteflow.core.NodeComponent; import com.yomahub.liteflow.slot.DefaultContext;
@LiteflowComponent("b") public class BCmp extends NodeComponent {
@Override public void process() { System.out.println("BCmp"); var requestData = this.getRequestData(); System.out.println(requestData); DefaultContext firstContextBean = this.getFirstContextBean(); var arg = firstContextBean.getData("arg"); System.out.println(arg); } }
|
C
组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
import com.yomahub.liteflow.annotation.LiteflowComponent; import com.yomahub.liteflow.core.NodeComponent;
@LiteflowComponent("c") public class CCmp extends NodeComponent {
@Override public void process() { System.out.println("CCmp"); } }
|
然后是编排文件和配置文件
1 2 3 4
| resources ├── config | └── flow.el.xml └── application.yml
|
config/flow.el.xml
1 2 3 4 5 6
| <?xml version="1.0" encoding="UTF-8"?> <flow> <chain name="chain1"> THEN(a, b, c); </chain> </flow>
|
application.yml
1 2
| liteflow: rule-source: config/flow.el.xml
|
运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
import com.yomahub.liteflow.core.FlowExecutor; import com.yomahub.liteflow.flow.LiteflowResponse; import com.yomahub.liteflow.slot.DefaultContext; import jakarta.annotation.Resource; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest class LiteflowTest {
@Resource private FlowExecutor flowExecutor;
@Test void test() { LiteflowResponse response = flowExecutor.execute2Resp("chain1", "arg"); DefaultContext firstContextBean = response.getFirstContextBean(); var arg = firstContextBean.getData("arg"); assertThat(arg).isEqualTo("varg"); } }
|