# 1.1 配置引入
<dependency>
<groupId>cn.kstry.framework</groupId>
<artifactId>kstry-core</artifactId>
<version>最新版本</version>
</dependency>
2
3
4
5
# 1.2 项目引入
# 1.2.1 开启Kstry容器
TIP
kstry框架与spring容器有较为密切的关联和依赖,当前版本暂时只能在spring环境中运行
@EnableKstry(bpmnPath = "./bpmn/*.bpmn")
@SpringBootApplication
public class KstryDemoApplication {
public static void main(String[] args) {
SpringApplication.run(KstryDemoApplication.class, args);
}
}
2
3
4
5
6
7
8
@EnableKstry
注解代表了要启动Kstry容器
🟢bpmnPath
: 指定bpmn文件位置。Kstry可视化的服务节点编排,使用的就是bpmn文件
# 1.2.2 编写组件代码
@TaskComponent(name = "goods")
public class GoodsService {
@NoticeResult
@TaskService(name = "init-base-info")
public GoodsDetail initBaseInfo(@ReqTaskParam(reqSelf = true) GoodsDetailRequest request) {
return GoodsDetail.builder().id(request.getId()).name("商品").build();
}
}
2
3
4
5
6
7
8
9
@TaskComponent
作用:
🟢 与 Spring 容器中@Component
注解有着相同的作用,将被标注的类组件托管至Spring容器中
🟢 指定该类成为了 Kstry 容器可解析的任务组件,可在其中定义服务节点(被@TaskService
注解修饰的方法称为服务节点)
🔷 name
:指定组件名称,与 bpmn 流程配置文件中task-component
属性对应
@TaskService
作用:
🟢 指定该方法是Kstry容器中的服务节点,也是在bpmn配置文件中可编排的最小执行单元,对应于bpmn配置文件中的bpmn:serviceTask
节点
🟢 该注解只能定义在Kstry容器可解析的任务组件类中,否则将不被解析
🔷 name
:指定该服务节点的名称,与 bpmn 配置文件中的task-service
属性进行匹配对应
🔷 desc
:节点描述信息。流程配置中未指定节点名称时,尝试获取@TaskService
注解的desc
属性作为name
@NoticeResult
作用:
🟢 指定执行结果将被通知到StoryBus中的哪些作用域中,@NoticeResult
说明,该方法执行结果将被通知到result域,最终作为Story的执行结果返回给调用方
@ReqTaskParam
作用:
🟢 @ReqTaskParam
标注在服务节点的方法参数上,用来从StoryBus的req域获取变量值,并直接赋值给被标注的参数项
🔷 reqSelf
:只有从req域获取参数时才有这个属性,该属性代表将客户端传入的request对象直接赋值给被标注的参数项
# 1.2.3 定义bpmn配置文件
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL">
<bpmn:process id="Process_0zcsieh" isExecutable="true">
<bpmn:startEvent id="kstry-demo-goods-show" name="kstry-demo-goods-show">
<bpmn:outgoing>Flow_1w64322</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:serviceTask id="Activity_04a99ll" name="show goods">
<bpmn:extensionElements>
<camunda:properties>
<camunda:property name="task-component" value="goods" />
<camunda:property name="task-service" value="init-base-info" />
</camunda:properties>
</bpmn:extensionElements>
<bpmn:incoming>Flow_1w64322</bpmn:incoming>
<bpmn:outgoing>Flow_0f9ephk</bpmn:outgoing>
</bpmn:serviceTask>
<bpmn:sequenceFlow id="Flow_1w64322" sourceRef="kstry-demo-goods-show" targetRef="Activity_04a99ll" />
<bpmn:endEvent id="Event_1jdtnd8">
<bpmn:incoming>Flow_0f9ephk</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_0f9ephk" sourceRef="Activity_04a99ll" targetRef="Event_1jdtnd8" />
</bpmn:process>
</bpmn:definitions>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
TIP
BPMN文件(流程图文件)编辑软件地址:Camunda Modeler (opens new window)
🟢 bpmn:startEvent
中的id属性,指定Story的执行ID,全局唯一
🔷 id
: 1.1.x版本之前需要符合一定格式的前缀, 默认是:story-def-
,可通过配置文件进行修改,如下:
## application.yml
kstry:
story:
prefix: kstry-demo- # 指定 Story 的 StartId 前缀
2
3
4
1.1.x版本之后,格式限制被取消,保证id不重复的前提下,可以定义任意格式字符串
# 1.2.4 执行Story
@RestController
@RequestMapping("/goods")
public class GoodsController {
@Resource
private StoryEngine storyEngine;
@PostMapping("/show")
public GoodsDetail showGoods(@RequestBody GoodsDetailRequest request) {
StoryRequest<GoodsDetail> req = ReqBuilder.returnType(GoodsDetail.class).startId("kstry-demo-goods-show").request(request).build();
TaskResponse<GoodsDetail> fire = storyEngine.fire(req);
if (fire.isSuccess()) {
return fire.getResult();
}
return null;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
🟢 从Spring容器中注入StoryEngine执行器
🟢 ReqBuilder构建执行入参,传入startId,request。调用fire方法,获取最终结果
# 1.3 测试
二、流程编排 →