Camunda
大约 2 分钟
发布流程
完成流程设计后,需要发布流程
官网教程知乎
create Api
http://localhost:8080/engine-rest/deployment/create
Parameters Request Body
xml 是 bpmn 文件,发布流程需要传送文件的文件流数据
const dpm = new FormData()
dpm.append('deployment-name', 'payment')
dpm.append('deployment-source', 'Camunda Modeler')
dpm.append('enable-duplicate-filtering', true)
dpm.append(
'Content-Disposition: form-data;name="payment.bpmn";filename="payment.bpmn";Content-Type: text/xml',
new Blob([xml])
)
开始流程
请求接口
http://localhost:8080/engine-rest/process-definition/key/${ID}/start
请求头设置 Content-Type
= application/json
请求参数
{
"variables": {
"amount": {
"value":555,
"type":"integer"
},
"item": {
"value": "item-xyz"
}
}
}
process-instance
Activity Instance
通过 id 检索给定流程实例的活动实例(树 API
/process-instance/dcb58801-34c1-11ed-8348-00d8617d5d1d/activity-instances
form-variables
form-variables 检索任务的表单项,如果定义了表单,则返回表单项内容
// GET
/task/{id}/form-variables
UserTask
// API
http://localhost:8080/engine-rest/task/0e9396b2-34bd-11ed-b695-00d8617d5d1d/submit-form
// Parameters:
{
"variables": {
"amount": {
"value": 9568751
},
"item": {
"value": "item"
},
"approved": {
"value": true
}
}
}
Service Tasks
Service Tasks 触发有几种方法,以下是使用 External Tasks 触发.
External Tasks 可以选择java
或NodeJs
编写外部任务,每当走到 Service Task,都会触发外部任务
上图是一个触发 Service Task 的流程实例,当前节点是在 Service Task 处
Implement an external task worker
const { Client, logger } = require('camunda-external-task-client-js')
const open = require('open')
var fs = require('fs') // 引入fs模块
// configuration for the Client:
// - 'baseUrl': url to the Process Engine
// - 'logger': utility to automatically log important events
// - 'asyncResponseTimeout': long polling timeout (then a new request will be issued)
const config = {
baseUrl: 'http://localhost:8080/engine-rest',
use: logger,
asyncResponseTimeout: 10000,
}
// create a Client instance with custom configuration
const client = new Client(config)
// susbscribe to the topic: 'charge0914'
client.subscribe('charge0914', async function ({ task, taskService }) {
// Put your business logic here
// Get a process variable
const amount = task.variables.get('amount')
const item = task.variables.get('item')
console.log(
`Charging credit card with an amount of ${amount}€ for the item '${item}'...`
)
fs.writeFile(
'./taskService.txt',
JSON.stringify(task),
{ flag: 'a' },
function (err) {
if (err) {
throw err
}
// 写入成功后读取测试
// fs.readFile('./taskService.txt', 'utf-8', function(err, data) {
// if (err) {
// throw err;
// }
// console.log(data);
// });
}
)
open('https://docs.camunda.org/get-started/quick-start/success')
// Complete the task
await taskService.complete(task)
})
通过 complete
可以完成该流程任务
complete Api
示例所需要的参数对应上图
// API 接口
http://localhost:8080/engine-rest/external-task/{externalTaskid}/complete
// Request Body
{
"workerId": ${workerId}
}
Worker Id 为空
在使用时,我发现当 externalTask
没有开启脚本的时候WorkerId
有为空的情况, node ./xx.js
运行脚本就没有问题了。