workflow之sdk-java
一、workflow是什么?
workflow是为了完成一些预定的目的,根据一组规则,而制定的一系列步骤。对于一个开发者来说,workflow则是将复杂的业务规则以及控制流程以图形化的方法声明出来,组成一个高度可视化的图形环境。(来自百度百科 https://blog.csdn.net/scwulian/article/details/20543049)
二、sdk-java
https://github.com/serverlessworkflow/sdk-java
SDK Version | JDK Version |
---|---|
5.0.0 and after | 11 |
4.0.x and before | 8 |
build projects:
To build project and run tests locally:
git clone https://github.com/serverlessworkflow/sdk-java.git
mvn clean install
Maven projects:
a) Add the following repository to your pom.xml section:repositories
oss.sonatype.org-snapshot
http://oss.sonatype.org/content/repositories/snapshots
false
true
b) Add the following dependencies to your pom.xml section:dependencies
io.serverlessworkflow
serverlessworkflow-api
5.0.0-SNAPSHOT
io.serverlessworkflow
serverlessworkflow-spi
5.0.0-SNAPSHOT
io.serverlessworkflow
serverlessworkflow-validation
5.0.0-SNAPSHOT
io.serverlessworkflow
serverlessworkflow-diagram
5.0.0-SNAPSHOT
io.serverlessworkflow
serverlessworkflow-util
5.0.0-SNAPSHOT
这里使用的是5.0的依赖,所以JDK要11及以上
三、How to Use
You can create a Workflow instance from JSON/YAML source:
Let's say you have a simple YAML based workflow definition:
我们准备一个yaml文件, test.yaml, 放到resources目录下
id: greeting
version: '1.0'
name: Greeting Workflow
start: Greet
description: Greet Someone
functions:
- name: greetingFunction
operation: file://myapis/greetingapis.json#greeting
states:
- name: Greet
type: operation
actions:
- functionRef:
refName: greetingFunction
arguments:
name: "${ .greet.name }"
actionDataFilter:
results: "${ .payload.greeting }"
stateDataFilter:
output: "${ .greeting }"
end: true
准备个 WorkFlowerTest.java
package com.liufei.demo;
import io.serverlessworkflow.api.Workflow;
import io.serverlessworkflow.api.interfaces.State;
import io.serverlessworkflow.api.interfaces.WorkflowDiagram;
import io.serverlessworkflow.diagram.WorkflowDiagramImpl;
import io.serverlessworkflow.utils.WorkflowUtils;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class WorkFlowerTest {
@Test
public void test() throws IOException {
ClassPathResource classPathResource = new ClassPathResource("test.yaml");
InputStream inputStream = classPathResource.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
String source = readFileAsString(inputStreamReader);
try {
Workflow workflow = Workflow.fromSource(source);
WorkflowDiagram workflowDiagram = new WorkflowDiagramImpl();
workflowDiagram.setWorkflow(workflow).showLegend(true);
String diagramSVG = workflowDiagram.getSvgDiagram();
System.out.println("结果:" + diagramSVG);
State startingState = WorkflowUtils.getStartingState(workflow);
System.out.println("状态:" + startingState.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
public String readFileAsString(Reader reader) {
try {
StringBuilder fileData = new StringBuilder(1000);
char[] buf = new char[1024];
int numRead;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
return fileData.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
结果返回的是个svg文件的内容:
点击查看SVG内容
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
我们创建一个test.svg文件,然后将内容copy过去,使用浏览器打开
这个意思是说电脑上没有安装 Graphviz。所以生成的svg文件内容不完整。
手动安装下:
https://graphviz.org/download/
安装完成之后,打开cmd开下版本:dot -version
然后再重新运行下上面的java程序。重新生成svg内容
点击查看SVG内容
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
可以看到一个流程图就出来了。
四、其他语言
https://github.com/serverlessworkflow/specification
五:相关资料
PlantUML简述: https://plantuml.com/zh/