使用 zeebe input/output 映射发送 jsonPath
Sending jsonPath by using zeebe input/output mapping
目前正在使用 Scala 和 zeebe,在我的 bpmn 中我有一些服务任务,它获取或发送数据类型 json 和我的主要目标解析此数据 json 路径,例如:
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
ServiceTask 从一些 REST API 获取请求,我可以获取
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
}
通过给 jsonpath = "$.glossary.GlossDiv.GlossList.GlossEntry.GlossDef"
,但我想在我的 zeebe bpnm 上写这个 json 路径,我不知道如何访问 zeebe jobWorker input/output 个变量
主要问题是如何访问和使用如下所示的 ioMapping:
<zeebe:ioMapping>
<zeebe:output source="glossary.GlossDiv.GlossList.GlossEntry.GlossDef" target="GlossDef" />
</zeebe:ioMapping>
我创建了一个示例,希望有助于解释这一点。
首先,您通过 Zeebe modeler or the Java model API 创建一个工作流。
您可以使用输入和输出映射来创建新变量或定义变量应如何合并到工作流实例变量中,请参阅 documentation for further information。
我根据你的问题创建了一个类似的工作流,在第一个任务上有一个输出映射。
// given
final var client = CLIENT_RULE.getClient();
// deploy workflow
final var workflow =
Bpmn.createExecutableProcess("processId")
.startEvent()
.serviceTask(
"task1",
t ->
t.zeebeTaskType("typeOne")
.zeebeOutput("glossary.GlossDiv.GlossList.GlossEntry.GlossDef", "GlossDef"))
.serviceTask("task2", t -> t.zeebeTaskType("typeTwo"))
.endEvent()
.done();
client.newDeployCommand().addWorkflowModel(workflow, "process.bpmn").send().join();
// create workflow instance with payload
client
.newCreateInstanceCommand()
.bpmnProcessId("processId")
.latestVersion()
.variables(
"{"
+ "\"glossary\": {\"title\": \"example glossary\","
+ "\"GlossDiv\": {"
+ "\"title\": \"S\","
+ "\"GlossList\": {"
+ "\"GlossEntry\": {"
+ "\"ID\": \"SGML\","
+ "\"SortAs\": \"SGML\","
+ "\"GlossTerm\": \"Standard Generalized Markup Language\","
+ "\"Acronym\": \"SGML\","
+ "\"Abbrev\": \"ISO 8879:1986\","
+ "\"GlossDef\": {"
+ "\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\","
+ "\"GlossSeeAlso\": [\"GML\", \"XML\"]"
+ "},"
+ "\"GlossSee\": \"markup\""
+ "}"
+ "}"
+ "}"
+ "}"
+ "}").send().join();
要访问负载,您只需在给定的作业上调用相应的方法即可。
例如 getVariables
returns 格式为 JSON 的字符串,但您也可以使用 getVariablesAsMap
方法更轻松地访问变量。
client
.newWorker()
.jobType("typeOne")
.handler(
(jobClient, job) -> {
System.out.println(job.getVariables());
jobClient.newCompleteCommand(job.getKey()).send().join();
})
.name("workerOne")
.open();
我们的处理程序代码将例如打印出来
{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}
对于第二个任务,我们可以编写以下代码。
final CountDownLatch secondJobLatch = new CountDownLatch(1);
client
.newWorker()
.jobType("typeTwo")
.handler(
(jobClient, job) -> {
System.out.println(job.getVariables());
jobClient.newCompleteCommand(job.getKey()).send().join();
secondJobLatch.countDown();
})
.name("workerTwo")
.open();
secondJobLatch.await();
这将打印出:
{"GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}
所以如果你想最小化作业可以访问的变量,你可以使用输入变量。如果您希望以不同方式存储工作流变量中的结果,您可以使用输出映射。使用输出映射,您还可以完全覆盖您的变量。
希望对您有所帮助。
目前正在使用 Scala 和 zeebe,在我的 bpmn 中我有一些服务任务,它获取或发送数据类型 json 和我的主要目标解析此数据 json 路径,例如:
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
ServiceTask 从一些 REST API 获取请求,我可以获取
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
}
通过给 jsonpath = "$.glossary.GlossDiv.GlossList.GlossEntry.GlossDef"
,但我想在我的 zeebe bpnm 上写这个 json 路径,我不知道如何访问 zeebe jobWorker input/output 个变量
主要问题是如何访问和使用如下所示的 ioMapping:
<zeebe:ioMapping>
<zeebe:output source="glossary.GlossDiv.GlossList.GlossEntry.GlossDef" target="GlossDef" />
</zeebe:ioMapping>
我创建了一个示例,希望有助于解释这一点。
首先,您通过 Zeebe modeler or the Java model API 创建一个工作流。
您可以使用输入和输出映射来创建新变量或定义变量应如何合并到工作流实例变量中,请参阅 documentation for further information。
我根据你的问题创建了一个类似的工作流,在第一个任务上有一个输出映射。
// given
final var client = CLIENT_RULE.getClient();
// deploy workflow
final var workflow =
Bpmn.createExecutableProcess("processId")
.startEvent()
.serviceTask(
"task1",
t ->
t.zeebeTaskType("typeOne")
.zeebeOutput("glossary.GlossDiv.GlossList.GlossEntry.GlossDef", "GlossDef"))
.serviceTask("task2", t -> t.zeebeTaskType("typeTwo"))
.endEvent()
.done();
client.newDeployCommand().addWorkflowModel(workflow, "process.bpmn").send().join();
// create workflow instance with payload
client
.newCreateInstanceCommand()
.bpmnProcessId("processId")
.latestVersion()
.variables(
"{"
+ "\"glossary\": {\"title\": \"example glossary\","
+ "\"GlossDiv\": {"
+ "\"title\": \"S\","
+ "\"GlossList\": {"
+ "\"GlossEntry\": {"
+ "\"ID\": \"SGML\","
+ "\"SortAs\": \"SGML\","
+ "\"GlossTerm\": \"Standard Generalized Markup Language\","
+ "\"Acronym\": \"SGML\","
+ "\"Abbrev\": \"ISO 8879:1986\","
+ "\"GlossDef\": {"
+ "\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\","
+ "\"GlossSeeAlso\": [\"GML\", \"XML\"]"
+ "},"
+ "\"GlossSee\": \"markup\""
+ "}"
+ "}"
+ "}"
+ "}"
+ "}").send().join();
要访问负载,您只需在给定的作业上调用相应的方法即可。
例如 getVariables
returns 格式为 JSON 的字符串,但您也可以使用 getVariablesAsMap
方法更轻松地访问变量。
client
.newWorker()
.jobType("typeOne")
.handler(
(jobClient, job) -> {
System.out.println(job.getVariables());
jobClient.newCompleteCommand(job.getKey()).send().join();
})
.name("workerOne")
.open();
我们的处理程序代码将例如打印出来
{"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}
对于第二个任务,我们可以编写以下代码。
final CountDownLatch secondJobLatch = new CountDownLatch(1);
client
.newWorker()
.jobType("typeTwo")
.handler(
(jobClient, job) -> {
System.out.println(job.getVariables());
jobClient.newCompleteCommand(job.getKey()).send().join();
secondJobLatch.countDown();
})
.name("workerTwo")
.open();
secondJobLatch.await();
这将打印出:
{"GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}
所以如果你想最小化作业可以访问的变量,你可以使用输入变量。如果您希望以不同方式存储工作流变量中的结果,您可以使用输出映射。使用输出映射,您还可以完全覆盖您的变量。
希望对您有所帮助。