其他分享
首页 > 其他分享> > camunda系列教程3(外部任务)

camunda系列教程3(外部任务)

作者:互联网

官方示例:

启动camunda

https://blog.csdn.net/weixin_44213308/article/details/120609758icon-default.png?t=L892https://blog.csdn.net/weixin_44213308/article/details/120609758

官方资源

https://download.csdn.net/download/weixin_44213308/30620140icon-default.png?t=L892https://download.csdn.net/download/weixin_44213308/30620140

bpm文件

<?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:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_0aui0ws" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.10.0">
  <bpmn:process id="payment-retrieval" name="Payment Retrieval" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1" name="Payment Retrieval Requested">
      <bpmn:outgoing>SequenceFlow_0fn07fk</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:serviceTask id="Task_19pt1nu" name="Charge Credit Card" camunda:type="external" camunda:topic="charge-card">
      <bpmn:incoming>SequenceFlow_187qbq7</bpmn:incoming>
      <bpmn:incoming>SequenceFlow_0ilu8bp</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_1axd8hr</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:endEvent id="EndEvent_1oyw02k" name="Payment Received">
      <bpmn:incoming>SequenceFlow_1axd8hr</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:sequenceFlow id="SequenceFlow_1axd8hr" sourceRef="Task_19pt1nu" targetRef="EndEvent_1oyw02k" />
    <bpmn:sequenceFlow id="SequenceFlow_0fn07fk" sourceRef="StartEvent_1" targetRef="ExclusiveGateway_0r349mg" />
    <bpmn:sequenceFlow id="SequenceFlow_0foxe2y" sourceRef="Task_1j42goo" targetRef="ExclusiveGateway_17ik05z" />
    <bpmn:userTask id="Task_1j42goo" name="Approve Payment" camunda:assignee="superAdmin">
      <bpmn:extensionElements>
        <camunda:formData>
          <camunda:formField id="amount" label="Amount" type="long" />
          <camunda:formField id="item" label="Item" type="string" />
          <camunda:formField id="approved" label="Approved?" type="boolean" />
        </camunda:formData>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_0di0jwo</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_0foxe2y</bpmn:outgoing>
    </bpmn:userTask>
    <bpmn:exclusiveGateway id="ExclusiveGateway_0r349mg" name="Payment Amount?">
      <bpmn:incoming>SequenceFlow_0fn07fk</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_0ilu8bp</bpmn:outgoing>
      <bpmn:outgoing>SequenceFlow_0di0jwo</bpmn:outgoing>
    </bpmn:exclusiveGateway>
    <bpmn:sequenceFlow id="SequenceFlow_0ilu8bp" name="&#60; 1000 €" sourceRef="ExclusiveGateway_0r349mg" targetRef="Task_19pt1nu">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${amount&lt;1000}</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:exclusiveGateway id="ExclusiveGateway_17ik05z" name="Approved?">
      <bpmn:incoming>SequenceFlow_0foxe2y</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_187qbq7</bpmn:outgoing>
      <bpmn:outgoing>SequenceFlow_0ypgah8</bpmn:outgoing>
    </bpmn:exclusiveGateway>
    <bpmn:sequenceFlow id="SequenceFlow_187qbq7" name="Yes" sourceRef="ExclusiveGateway_17ik05z" targetRef="Task_19pt1nu">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${approved}</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="SequenceFlow_0di0jwo" name="&#62;= 1000 €" sourceRef="ExclusiveGateway_0r349mg" targetRef="Task_1j42goo">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${amount&gt;=1000}</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:endEvent id="EndEvent_0fj2aui" name="Payment Retrieval Rejected">
      <bpmn:incoming>SequenceFlow_0ypgah8</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:sequenceFlow id="SequenceFlow_0ypgah8" name="No" sourceRef="ExclusiveGateway_17ik05z" targetRef="EndEvent_0fj2aui">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${!approved}</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
  </bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="payment-retrieval">
      <bpmndi:BPMNEdge id="SequenceFlow_0ypgah8_di" bpmnElement="SequenceFlow_0ypgah8">
        <di:waypoint x="631" y="264" />
        <di:waypoint x="713" y="264" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="665" y="243" width="15" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_0di0jwo_di" bpmnElement="SequenceFlow_0di0jwo">
        <di:waypoint x="272" y="145" />
        <di:waypoint x="272" y="264" />
        <di:waypoint x="374" y="264" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="284" y="176" width="50" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_187qbq7_di" bpmnElement="SequenceFlow_187qbq7">
        <di:waypoint x="606" y="239" />
        <di:waypoint x="606" y="160" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="612" y="193" width="18" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_0ilu8bp_di" bpmnElement="SequenceFlow_0ilu8bp">
        <di:waypoint x="297" y="120" />
        <di:waypoint x="556" y="120" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="404" y="98" width="44" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_0foxe2y_di" bpmnElement="SequenceFlow_0foxe2y">
        <di:waypoint x="474" y="264" />
        <di:waypoint x="581" y="264" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="527.5" y="242.5" width="0" height="13" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_0fn07fk_di" bpmnElement="SequenceFlow_0fn07fk">
        <di:waypoint x="209" y="120" />
        <di:waypoint x="247" y="120" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="183" y="98.5" width="90" height="13" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_1axd8hr_di" bpmnElement="SequenceFlow_1axd8hr">
        <di:waypoint x="656" y="120" />
        <di:waypoint x="713" y="120" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="639.5" y="98.5" width="90" height="13" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
        <dc:Bounds x="173" y="102" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="164" y="138" width="54" height="38" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="ServiceTask_17yjgnl_di" bpmnElement="Task_19pt1nu">
        <dc:Bounds x="556" y="80" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="EndEvent_1oyw02k_di" bpmnElement="EndEvent_1oyw02k">
        <dc:Bounds x="713" y="102" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="708" y="141" width="46" height="25" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="UserTask_1r7ozpl_di" bpmnElement="Task_1j42goo">
        <dc:Bounds x="374" y="224" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="ExclusiveGateway_0r349mg_di" bpmnElement="ExclusiveGateway_0r349mg" isMarkerVisible="true">
        <dc:Bounds x="247" y="95" width="50" height="50" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="249" y="62" width="45" height="25" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="ExclusiveGateway_17ik05z_di" bpmnElement="ExclusiveGateway_17ik05z" isMarkerVisible="true">
        <dc:Bounds x="581" y="239" width="50" height="50" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="579" y="292" width="54" height="13" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="EndEvent_0fj2aui_di" bpmnElement="EndEvent_0fj2aui">
        <dc:Bounds x="713" y="246" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="686" y="285" width="90" height="25" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

新建外部Java程序

pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>org.camunda.bpm.getstarted</groupId>
	<artifactId>charge-card-worker</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<camunda.external-task-client.version>7.15.0</camunda.external-task-client.version>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.camunda.bpm</groupId>
			<artifactId>camunda-external-task-client</artifactId>
			<version>${camunda.external-task-client.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>javax.xml.bind</groupId>
			<artifactId>jaxb-api</artifactId>
			<version>2.3.1</version>
		</dependency>
	</dependencies>

	<profiles>
		<profile>
			<id>jdk9-plus</id>
			<activation><jdk>(1.8,)</jdk></activation>
			<dependencies>
				<dependency>
					<groupId>com.sun.xml.bind</groupId>
					<artifactId>jaxb-impl</artifactId>
					<version>2.3.1</version>
				</dependency>
			</dependencies>
		</profile>
	</profiles>
</project>

Java代码(修改:添加账户和密码)

package org.camunda.bpm.getstarted.chargecard;

import java.awt.Desktop;
import java.net.URI;
import java.util.logging.Logger;

import org.camunda.bpm.client.ExternalTaskClient;
import org.camunda.bpm.client.backoff.ExponentialBackoffStrategy;
import org.camunda.bpm.client.interceptor.auth.BasicAuthProvider;

public class ChargeCardWorker {
	private final static Logger LOGGER = Logger.getLogger(ChargeCardWorker.class.getName());

	public static void main(String[] args) {
		ExternalTaskClient client = ExternalTaskClient.create()
				.baseUrl("http://localhost:82/engine-rest")
				// long polling timeout
				.asyncResponseTimeout(10000)
				// Only change made after turning on basic auth
				.addInterceptor(new BasicAuthProvider("superAdmin","123456"))
				.backoffStrategy(new ExponentialBackoffStrategy(0, 0, 0))
				.build();

		// subscribe to an external task topic as specified in the process
		client.subscribe("charge-card")
				// the default lock duration is 20 seconds, but you can override this
				.lockDuration(1000)
				.handler((externalTask, externalTaskService) -> {
					// Put your business logic here

					// Get a process variable
					String item = (String) externalTask.getVariable("item");
					Long amount = (Long) externalTask.getVariable("amount");
					LOGGER.info("Charging credit card with an amount of '" + amount + "'€ for the item '" + item + "'...");
					
					try {
						Desktop.getDesktop().browse(new URI("https://docs.camunda.org/get-started/quick-start/complete"));
					} catch (Exception e) {
						e.printStackTrace();
					}

					// Complete the task
					externalTaskService.complete(externalTask);
				})
				.open();
	}
}

访问camunda web页面,启动任务

 

 

 

标签:教程,SequenceFlow,外部,bpm,client,import,org,camunda
来源: https://blog.csdn.net/weixin_44213308/article/details/120691326