Currently working with Scala and zeebe, and in my bpmn I have some service task, which get or send data type of json and my main goal parse this data jsonpath, for example:
{
"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 make get request from some REST API, and I can get
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
}
by giving jsonpath = "$.glossary.GlossDiv.GlossList.GlossEntry.GlossDef"
, but I want to write this json path on my zeebe bpnm and I don't know how to access zeebe jobWorker input/output variables
and the main question is how I can access and use ioMapping which looks like below:
<zeebe:ioMapping>
<zeebe:output source="glossary.GlossDiv.GlossList.GlossEntry.GlossDef" target="GlossDef" />
</zeebe:ioMapping>
I created an example, which hopefully helps to explain that.
First you create a workflow via the Zeebe modeler or the Java model API.
You can use input and output mappings to create new variables or define how variables should be merged into the workflow instance variables, please see the documentation for further information.
I created a similar workflow based on your Question, with an output mapping on the first task.
// 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();
To access the payload you simple call a corresponding method on the given Job.
For example getVariables
returns an string in JSON format, but you can also use the getVariablesAsMap
method to access the variables easier.
client
.newWorker()
.jobType("typeOne")
.handler(
(jobClient, job) -> {
System.out.println(job.getVariables());
jobClient.newCompleteCommand(job.getKey()).send().join();
})
.name("workerOne")
.open();
Our handler code will for example print out
{"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"}}}}}
For the second task we could write the following code.
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();
This will print out:
{"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"}}}}}
So if you want to minimize the variables which the job has access to you can use input variables. If you want to have an result in your workflow variables differently stored you can use the output mapping. With the output mapping you can also overwrite your variables completely.
I hope this helps.