版权声明:本文为博主原创文章,未经博主允许不得转载。不经过允许copy,讲追究法律责任,欢迎加入我们的学习提升群523988350,可以相互交流 https://blog.csdn.net/qq_30739519/article/details/89598676
Deploy a Workflow
- 首先启动Zeebe broker。默认访问地址是,
localhost:26500
(default)
示例代码如下:
/*
* Copyright © 2017 camunda services GmbH ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zeebe.example.workflow;
import io.zeebe.client.ZeebeClient;
import io.zeebe.client.ZeebeClientBuilder;
import io.zeebe.client.api.events.DeploymentEvent;
public class WorkflowDeployer {
public static void main(final String[] args) {
final String broker = "localhost:26500";
final ZeebeClientBuilder clientBuilder =
ZeebeClient.newClientBuilder().brokerContactPoint(broker);
try (ZeebeClient client = clientBuilder.build()) {
final DeploymentEvent deploymentEvent =
client.newDeployCommand().addResourceFromClasspath("demoProcess.bpmn").send().join();
System.out.println("Deployment created with key: " + deploymentEvent.getKey());
}
}
}
BPMN流程文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:zeebe="http://camunda.org/schema/zeebe/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="1.5.0-nightly">
<bpmn:process id="demoProcess" isExecutable="true">
<bpmn:startEvent id="start" name="start">
<bpmn:outgoing>SequenceFlow_1sz6737</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="SequenceFlow_1sz6737" sourceRef="start" targetRef="taskA" />
<bpmn:sequenceFlow id="SequenceFlow_06ytcxw" sourceRef="taskA" targetRef="taskB" />
<bpmn:sequenceFlow id="SequenceFlow_1oh45y7" sourceRef="taskB" targetRef="taskC" />
<bpmn:endEvent id="end" name="end">
<bpmn:incoming>SequenceFlow_148rk2p</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="SequenceFlow_148rk2p" sourceRef="taskC" targetRef="end" />
<bpmn:serviceTask id="taskA" name="task A">
<bpmn:extensionElements>
<zeebe:taskDefinition type="foo" />
</bpmn:extensionElements>
<bpmn:incoming>SequenceFlow_1sz6737</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_06ytcxw</bpmn:outgoing>
</bpmn:serviceTask>
<bpmn:serviceTask id="taskB" name="task B">
<bpmn:extensionElements>
<zeebe:taskDefinition type="bar" />
</bpmn:extensionElements>
<bpmn:incoming>SequenceFlow_06ytcxw</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_1oh45y7</bpmn:outgoing>
</bpmn:serviceTask>
<bpmn:serviceTask id="taskC" name="task C">
<bpmn:extensionElements>
<zeebe:taskDefinition type="foo" />
</bpmn:extensionElements>
<bpmn:incoming>SequenceFlow_1oh45y7</bpmn:incoming>
<bpmn:outgoing>SequenceFlow_148rk2p</bpmn:outgoing>
</bpmn:serviceTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="demoProcess">
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="start">
<dc:Bounds x="173" y="102" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="180" y="138" width="22" height="12" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_1sz6737_di" bpmnElement="SequenceFlow_1sz6737">
<di:waypoint xsi:type="dc:Point" x="209" y="120" />
<di:waypoint xsi:type="dc:Point" x="310" y="120" />
<bpmndi:BPMNLabel>
<dc:Bounds x="260" y="105" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="SequenceFlow_06ytcxw_di" bpmnElement="SequenceFlow_06ytcxw">
<di:waypoint xsi:type="dc:Point" x="410" y="120" />
<di:waypoint xsi:type="dc:Point" x="502" y="120" />
<bpmndi:BPMNLabel>
<dc:Bounds x="456" y="105" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="SequenceFlow_1oh45y7_di" bpmnElement="SequenceFlow_1oh45y7">
<di:waypoint xsi:type="dc:Point" x="602" y="120" />
<di:waypoint xsi:type="dc:Point" x="694" y="120" />
<bpmndi:BPMNLabel>
<dc:Bounds x="648" y="105" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="EndEvent_0gbv3sc_di" bpmnElement="end">
<dc:Bounds x="867" y="102" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="876" y="138" width="18" height="12" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="SequenceFlow_148rk2p_di" bpmnElement="SequenceFlow_148rk2p">
<di:waypoint xsi:type="dc:Point" x="794" y="120" />
<di:waypoint xsi:type="dc:Point" x="867" y="120" />
<bpmndi:BPMNLabel>
<dc:Bounds x="831" y="105" width="0" height="0" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="ServiceTask_09m0goq_di" bpmnElement="taskA">
<dc:Bounds x="310" y="80" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="ServiceTask_0sryj72_di" bpmnElement="taskB">
<dc:Bounds x="502" y="80" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="ServiceTask_1xu4l3g_di" bpmnElement="taskC">
<dc:Bounds x="694" y="80" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
创建流程实例
/*
* Copyright © 2017 camunda services GmbH ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zeebe.example.workflow;
import io.zeebe.client.ZeebeClient;
import io.zeebe.client.ZeebeClientBuilder;
import io.zeebe.client.api.events.WorkflowInstanceEvent;
public class WorkflowInstanceCreator {
public static void main(final String[] args) {
final String broker = "127.0.0.1:26500";
final String bpmnProcessId = "demoProcess";
final ZeebeClientBuilder builder = ZeebeClient.newClientBuilder().brokerContactPoint(broker);
try (ZeebeClient client = builder.build()) {
System.out.println("Creating workflow instance");
final WorkflowInstanceEvent workflowInstanceEvent =
client
.newCreateInstanceCommand()
.bpmnProcessId(bpmnProcessId)
.latestVersion()
.send()
.join();
System.out.println(
"Workflow instance created with key: " + workflowInstanceEvent.getWorkflowInstanceKey());
}
}
}
/*
* Copyright © 2017 camunda services GmbH ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zeebe.example.workflow;
import io.zeebe.client.ZeebeClient;
import io.zeebe.client.ZeebeClientBuilder;
import io.zeebe.client.api.ZeebeFuture;
import io.zeebe.client.api.events.WorkflowInstanceEvent;
public class NonBlockingWorkflowInstanceCreator {
public static void main(final String[] args) {
final String broker = "127.0.0.1:26500";
final int numberOfInstances = 100_000;
final String bpmnProcessId = "demoProcess";
final ZeebeClientBuilder builder = ZeebeClient.newClientBuilder().brokerContactPoint(broker);
try (ZeebeClient client = builder.build()) {
System.out.println("Creating " + numberOfInstances + " workflow instances");
final long startTime = System.currentTimeMillis();
long instancesCreating = 0;
while (instancesCreating < numberOfInstances) {
// this is non-blocking/async => returns a future
final ZeebeFuture<WorkflowInstanceEvent> future =
client.newCreateInstanceCommand().bpmnProcessId(bpmnProcessId).latestVersion().send();
// could put the future somewhere and eventually wait for its completion
instancesCreating++;
}
// creating one more instance; joining on this future ensures
// that all the other create commands were handled
client.newCreateInstanceCommand().bpmnProcessId(bpmnProcessId).latestVersion().send().join();
System.out.println("Took: " + (System.currentTimeMillis() - startTime));
}
}
}
Request all Workflows
/*
* Copyright © 2017 camunda services GmbH ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zeebe.example.workflow;
import io.zeebe.client.ZeebeClient;
import io.zeebe.client.ZeebeClientBuilder;
import io.zeebe.client.api.commands.WorkflowResource;
import io.zeebe.client.api.commands.Workflows;
public class DeploymentViewer {
public static void main(final String[] args) {
final String broker = "localhost:26500";
final ZeebeClientBuilder clientBuilder =
ZeebeClient.newClientBuilder().brokerContactPoint(broker);
try (ZeebeClient client = clientBuilder.build()) {
final Workflows workflows = client.newWorkflowRequest().send().join();
System.out.println("Printing all deployed workflows:");
workflows
.getWorkflows()
.forEach(
wf -> {
System.out.println("Workflow resource for " + wf + ":");
final WorkflowResource resource =
client.newResourceRequest().workflowKey(wf.getWorkflowKey()).send().join();
System.out.println(resource);
});
System.out.println("Done");
}
}
}
开启定时器
/*
* Copyright © 2017 camunda services GmbH ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zeebe.example.job;
import io.zeebe.client.ZeebeClient;
import io.zeebe.client.ZeebeClientBuilder;
import io.zeebe.client.api.clients.JobClient;
import io.zeebe.client.api.response.ActivatedJob;
import io.zeebe.client.api.subscription.JobHandler;
import io.zeebe.client.api.subscription.JobWorker;
import java.time.Duration;
import java.util.Scanner;
public class JobWorkerCreator {
public static void main(final String[] args) {
final String broker = "127.0.0.1:26500";
final String jobType = "foo";
final ZeebeClientBuilder builder = ZeebeClient.newClientBuilder().brokerContactPoint(broker);
try (ZeebeClient client = builder.build()) {
System.out.println("Opening job worker.");
final JobWorker workerRegistration =
client
.newWorker()
.jobType(jobType)
.handler(new ExampleJobHandler())
.timeout(Duration.ofSeconds(10))
.open();
System.out.println("Job worker opened and receiving jobs.");
// call workerRegistration.close() to close it
// run until System.in receives exit command
waitUntilSystemInput("exit");
}
}
private static class ExampleJobHandler implements JobHandler {
@Override
public void handle(final JobClient client, final ActivatedJob job) {
// here: business logic that is executed with every job
System.out.println(
String.format(
"[type: %s, key: %s, lockExpirationTime: %s]\n[headers: %s]\n[variables: %s]\n===",
job.getType(),
job.getKey(),
job.getDeadline().toString(),
job.getHeaders(),
job.getVariables()));
client.newCompleteCommand(job.getKey()).send().join();
}
}
private static void waitUntilSystemInput(final String exitCode) {
try (Scanner scanner = new Scanner(System.in)) {
while (scanner.hasNextLine()) {
final String nextLine = scanner.nextLine();
if (nextLine.contains(exitCode)) {
return;
}
}
}
}
}
Handle variables as POJO
/*
* Copyright © 2017 camunda services GmbH ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zeebe.example.data;
import io.zeebe.client.ZeebeClient;
import io.zeebe.client.ZeebeClientBuilder;
import io.zeebe.client.api.clients.JobClient;
import io.zeebe.client.api.response.ActivatedJob;
import io.zeebe.client.api.subscription.JobHandler;
import java.util.Scanner;
public class HandleVariablesAsPojo {
public static void main(final String[] args) {
final String broker = "127.0.0.1:26500";
final ZeebeClientBuilder builder = ZeebeClient.newClientBuilder().brokerContactPoint(broker);
try (ZeebeClient client = builder.build()) {
final Order order = new Order();
order.setOrderId(31243);
client
.newCreateInstanceCommand()
.bpmnProcessId("demoProcess")
.latestVersion()
.variables(order)
.send()
.join();
client.newWorker().jobType("foo").handler(new DemoJobHandler()).open();
// run until System.in receives exit command
waitUntilSystemInput("exit");
}
}
private static class DemoJobHandler implements JobHandler {
@Override
public void handle(final JobClient client, final ActivatedJob job) {
// read the variables of the job
final Order order = job.getVariablesAsType(Order.class);
System.out.println("new job with orderId: " + order.getOrderId());
// update the variables and complete the job
order.setTotalPrice(46.50);
client.newCompleteCommand(job.getKey()).variables(order).send();
}
}
public static class Order {
private long orderId;
private double totalPrice;
public long getOrderId() {
return orderId;
}
public void setOrderId(final long orderId) {
this.orderId = orderId;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(final double totalPrice) {
this.totalPrice = totalPrice;
}
}
private static void waitUntilSystemInput(final String exitCode) {
try (Scanner scanner = new Scanner(System.in)) {
while (scanner.hasNextLine()) {
final String nextLine = scanner.nextLine();
if (nextLine.contains(exitCode)) {
return;
}
}
}
}
}
Request Cluster Topology
/*
* Copyright © 2017 camunda services GmbH ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.zeebe.example.cluster;
import io.zeebe.client.ZeebeClient;
import io.zeebe.client.ZeebeClientBuilder;
import io.zeebe.client.api.commands.Topology;
public class TopologyViewer {
public static void main(final String[] args) {
final String broker = "127.0.0.1:26500";
final ZeebeClientBuilder builder = ZeebeClient.newClientBuilder().brokerContactPoint(broker);
try (ZeebeClient client = builder.build()) {
System.out.println("Requesting topology with initial contact point " + broker);
final Topology topology = client.newTopologyRequest().send().join();
System.out.println("Topology:");
topology
.getBrokers()
.forEach(
b -> {
System.out.println(" " + b.getAddress());
b.getPartitions()
.forEach(
p ->
System.out.println(
" " + p.getPartitionId() + " - " + p.getRole()));
});
System.out.println("Done.");
}
}
}
zeebe qq交流群群号:856546010