VOOZH about

URL: https://dzone.com/articles/external-task-client-in-camunda-with-spring-boot-a

⇱ External Task Client Implementation in Camunda With Spring Boot Application


Related

  1. DZone
  2. Coding
  3. Frameworks
  4. External Task Client Implementation in Camunda With Spring Boot Application

External Task Client Implementation in Camunda With Spring Boot Application

See how to use Spring Boot starter dependency to run the external task in Camunda.

By Jul. 27, 21 · Tutorial
Likes
Comment
Save
10.7K Views

Join the DZone community and get the full member experience.

Join For Free

Today, we will be using Spring Boot starter dependency to run the external task in Camunda.

Create a Spring Boot project with the embedded Camunda Engine using this link. Use version 7.15.0.

Other Requirements:

  1. Spring Boot 2.4.3
  2. Camunda 7.15.0
  3. Eclipse or any other IDE
  4. Maven

Import the Maven project into Eclipse and add the below-mentioned files.

Add below additional dependency to the pom.xml file.

XML
<dependency>
 <groupId>org.camunda.bpm.springboot</groupId>
 <artifactId>camunda-bpm-spring-boot-starter-external-task-client</artifactId>
 <version>7.15.0</version>
</dependency>


Use Camunda modeler to save the below XML content with .bpmn extension to get the process model, which is having external service tasks as component.

XML
<?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: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" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_1q1cbhv" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="3.4.1">
 <bpmn:process id="externalTaskClientProcess" name="External Task Client" isExecutable="true">
 <bpmn:startEvent id="StartEvent_1" name="Start">
 <bpmn:extensionElements>
 <camunda:executionListener event="start">
 <camunda:script scriptFormat="Groovy">println("process started")</camunda:script>
 </camunda:executionListener>
 </bpmn:extensionElements>
 <bpmn:outgoing>SequenceFlow_1hatjbw</bpmn:outgoing>
 </bpmn:startEvent>
 <bpmn:sequenceFlow id="SequenceFlow_1hatjbw" sourceRef="StartEvent_1" targetRef="Task_08imlsr" />
 <bpmn:serviceTask id="Task_08imlsr" name="Get Interview Rating (1 to 10)" camunda:type="external" camunda:topic="ratingProvider">
 <bpmn:incoming>SequenceFlow_1hatjbw</bpmn:incoming>
 <bpmn:outgoing>SequenceFlow_1x5a8md</bpmn:outgoing>
 </bpmn:serviceTask>
 <bpmn:exclusiveGateway id="ExclusiveGateway_0hmq2sa">
 <bpmn:incoming>SequenceFlow_1x5a8md</bpmn:incoming>
 <bpmn:outgoing>SequenceFlow_08h0v2e</bpmn:outgoing>
 <bpmn:outgoing>SequenceFlow_0p8irmo</bpmn:outgoing>
 </bpmn:exclusiveGateway>
 <bpmn:sequenceFlow id="SequenceFlow_1x5a8md" sourceRef="Task_08imlsr" targetRef="ExclusiveGateway_0hmq2sa" />
 <bpmn:sequenceFlow id="SequenceFlow_08h0v2e" name="Rating > 6" sourceRef="ExclusiveGateway_0hmq2sa" targetRef="Task_1g5ymte">
 <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${candidateScore > 6}</bpmn:conditionExpression>
 </bpmn:sequenceFlow>
 <bpmn:sequenceFlow id="SequenceFlow_0p8irmo" name="Rating <= 6" sourceRef="ExclusiveGateway_0hmq2sa" targetRef="Task_0f7ms7k">
 <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${candidateScore <= 6}</bpmn:conditionExpression>
 </bpmn:sequenceFlow>
 <bpmn:exclusiveGateway id="ExclusiveGateway_0gfoi7x">
 <bpmn:incoming>SequenceFlow_05v92zi</bpmn:incoming>
 <bpmn:incoming>SequenceFlow_1g31asm</bpmn:incoming>
 <bpmn:outgoing>SequenceFlow_18vlx25</bpmn:outgoing>
 </bpmn:exclusiveGateway>
 <bpmn:sequenceFlow id="SequenceFlow_05v92zi" sourceRef="Task_0f7ms7k" targetRef="ExclusiveGateway_0gfoi7x" />
 <bpmn:endEvent id="EndEvent_01rktcj" name="End">
 <bpmn:incoming>SequenceFlow_18vlx25</bpmn:incoming>
 </bpmn:endEvent>
 <bpmn:sequenceFlow id="SequenceFlow_18vlx25" sourceRef="ExclusiveGateway_0gfoi7x" targetRef="EndEvent_01rktcj" />
 <bpmn:sequenceFlow id="SequenceFlow_1g31asm" sourceRef="Task_1g5ymte" targetRef="ExclusiveGateway_0gfoi7x" />
 <bpmn:serviceTask id="Task_1g5ymte" name="Get Rating" camunda:type="external" camunda:topic="candidateRating">
 <bpmn:incoming>SequenceFlow_08h0v2e</bpmn:incoming>
 <bpmn:outgoing>SequenceFlow_1g31asm</bpmn:outgoing>
 </bpmn:serviceTask>
 <bpmn:scriptTask id="Task_0f7ms7k" name="Candidate Rejected" scriptFormat="Groovy">
 <bpmn:incoming>SequenceFlow_0p8irmo</bpmn:incoming>
 <bpmn:outgoing>SequenceFlow_05v92zi</bpmn:outgoing>
 <bpmn:script>println ("Candidate with score: " + candidateScore +" is rejected.");</bpmn:script>
 </bpmn:scriptTask>
 <bpmn:textAnnotation id="TextAnnotation_1qzfqxc">
 <bpmn:text>ratingProvider</bpmn:text>
 </bpmn:textAnnotation>
 <bpmn:association id="Association_0ce5iq8" sourceRef="Task_08imlsr" targetRef="TextAnnotation_1qzfqxc" />
 <bpmn:textAnnotation id="TextAnnotation_0vo2qx6">
 <bpmn:text>candidateRating</bpmn:text>
 </bpmn:textAnnotation>
 <bpmn:association id="Association_0y3fb6h" sourceRef="Task_1g5ymte" targetRef="TextAnnotation_0vo2qx6" />
 </bpmn:process>
 <bpmndi:BPMNDiagram id="BPMNDiagram_1">
 <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="externalTaskClientProcess">
 <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
 <dc:Bounds x="179" y="269" width="36" height="36" />
 <bpmndi:BPMNLabel>
 <dc:Bounds x="185" y="312" width="25" height="14" />
 </bpmndi:BPMNLabel>
 </bpmndi:BPMNShape>
 <bpmndi:BPMNEdge id="SequenceFlow_1hatjbw_di" bpmnElement="SequenceFlow_1hatjbw">
 <di:waypoint x="215" y="287" />
 <di:waypoint x="270" y="287" />
 </bpmndi:BPMNEdge>
 <bpmndi:BPMNShape id="ServiceTask_0ark1zc_di" bpmnElement="Task_08imlsr">
 <dc:Bounds x="270" y="247" width="100" height="80" />
 </bpmndi:BPMNShape>
 <bpmndi:BPMNShape id="ExclusiveGateway_0hmq2sa_di" bpmnElement="ExclusiveGateway_0hmq2sa" isMarkerVisible="true">
 <dc:Bounds x="425" y="262" width="50" height="50" />
 </bpmndi:BPMNShape>
 <bpmndi:BPMNEdge id="SequenceFlow_1x5a8md_di" bpmnElement="SequenceFlow_1x5a8md">
 <di:waypoint x="370" y="287" />
 <di:waypoint x="425" y="287" />
 </bpmndi:BPMNEdge>
 <bpmndi:BPMNEdge id="SequenceFlow_08h0v2e_di" bpmnElement="SequenceFlow_08h0v2e">
 <di:waypoint x="450" y="262" />
 <di:waypoint x="450" y="200" />
 <di:waypoint x="550" y="200" />
 <bpmndi:BPMNLabel>
 <dc:Bounds x="454" y="183" width="51" height="14" />
 </bpmndi:BPMNLabel>
 </bpmndi:BPMNEdge>
 <bpmndi:BPMNEdge id="SequenceFlow_0p8irmo_di" bpmnElement="SequenceFlow_0p8irmo">
 <di:waypoint x="450" y="312" />
 <di:waypoint x="450" y="400" />
 <di:waypoint x="550" y="400" />
 <bpmndi:BPMNLabel>
 <dc:Bounds x="451" y="403" width="57" height="14" />
 </bpmndi:BPMNLabel>
 </bpmndi:BPMNEdge>
 <bpmndi:BPMNShape id="ExclusiveGateway_0gfoi7x_di" bpmnElement="ExclusiveGateway_0gfoi7x" isMarkerVisible="true">
 <dc:Bounds x="715" y="262" width="50" height="50" />
 </bpmndi:BPMNShape>
 <bpmndi:BPMNEdge id="SequenceFlow_05v92zi_di" bpmnElement="SequenceFlow_05v92zi">
 <di:waypoint x="650" y="400" />
 <di:waypoint x="740" y="400" />
 <di:waypoint x="740" y="312" />
 </bpmndi:BPMNEdge>
 <bpmndi:BPMNShape id="EndEvent_01rktcj_di" bpmnElement="EndEvent_01rktcj">
 <dc:Bounds x="822" y="269" width="36" height="36" />
 <bpmndi:BPMNLabel>
 <dc:Bounds x="830" y="312" width="20" height="14" />
 </bpmndi:BPMNLabel>
 </bpmndi:BPMNShape>
 <bpmndi:BPMNEdge id="SequenceFlow_18vlx25_di" bpmnElement="SequenceFlow_18vlx25">
 <di:waypoint x="765" y="287" />
 <di:waypoint x="822" y="287" />
 </bpmndi:BPMNEdge>
 <bpmndi:BPMNEdge id="SequenceFlow_1g31asm_di" bpmnElement="SequenceFlow_1g31asm">
 <di:waypoint x="650" y="200" />
 <di:waypoint x="740" y="200" />
 <di:waypoint x="740" y="262" />
 </bpmndi:BPMNEdge>
 <bpmndi:BPMNShape id="ServiceTask_0ktpo8l_di" bpmnElement="Task_1g5ymte">
 <dc:Bounds x="550" y="160" width="100" height="80" />
 </bpmndi:BPMNShape>
 <bpmndi:BPMNShape id="ScriptTask_0jfnbcz_di" bpmnElement="Task_0f7ms7k">
 <dc:Bounds x="550" y="360" width="100" height="80" />
 </bpmndi:BPMNShape>
 <bpmndi:BPMNShape id="TextAnnotation_1qzfqxc_di" bpmnElement="TextAnnotation_1qzfqxc">
 <dc:Bounds x="370" y="160" width="100" height="30" />
 </bpmndi:BPMNShape>
 <bpmndi:BPMNEdge id="Association_0ce5iq8_di" bpmnElement="Association_0ce5iq8">
 <di:waypoint x="356" y="247" />
 <di:waypoint x="407" y="190" />
 </bpmndi:BPMNEdge>
 <bpmndi:BPMNShape id="TextAnnotation_0vo2qx6_di" bpmnElement="TextAnnotation_0vo2qx6">
 <dc:Bounds x="650" y="80" width="100" height="30" />
 </bpmndi:BPMNShape>
 <bpmndi:BPMNEdge id="Association_0y3fb6h_di" bpmnElement="Association_0y3fb6h">
 <di:waypoint x="638" y="160" />
 <di:waypoint x="686" y="110" />
 </bpmndi:BPMNEdge>
 </bpmndi:BPMNPlane>
 </bpmndi:BPMNDiagram>
</bpmn:definitions>

If seen the saved content in diagram view in modeler, below is the diagram that should be there.

Add the below code snippet in application.yaml.

YAML
camunda.bpm.client:
 base-url: http://localhost:8080/engine-rest #URL of Process Engine
 lock-duration: 20000 # time in ms & external task will be locked
 subscriptions:
 ratingProvider: 
 variable-names: [] 
 process-definition-key: externalTaskClientProcess 
 candidateRating:
 variable-names: candidateScore
      process-definition-key: externalTaskClientProcess


Add the below external task implementations used in the process model namely ratingProvider and candidateRating.

Java
package com.example.workflow;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.camunda.bpm.client.spring.annotation.ExternalTaskSubscription;
import org.camunda.bpm.client.task.ExternalTask;
import org.camunda.bpm.client.task.ExternalTaskHandler;
import org.camunda.bpm.client.task.ExternalTaskService;
import org.camunda.bpm.engine.variable.VariableMap;
import org.camunda.bpm.engine.variable.Variables;
import org.springframework.stereotype.Component;

@Component
@ExternalTaskSubscription("ratingProvider") 
public class GenerateRatingHandler implements ExternalTaskHandler {

	Logger logger = Logger.getLogger("GenerateRatingHandler");
	@Override
	public void execute(ExternalTask externalTask, ExternalTaskService externalTaskService) {
		
		String candidateId = "IN-" + (Math.random() * 100);
		int candidateScore = (int) (Math.random() * 11);

		VariableMap variables = Variables.createVariables();
		variables.put("candidateId", candidateId);
		variables.put("candidateScore", candidateScore);

		externalTaskService.complete(externalTask, variables); 

		logger.log(Level.INFO, "NOPE Rating Genrator External task is completed for Candidate {0} with rating {1}", new Object[]{candidateId, candidateScore});

	}

}
Java
package com.example.workflow;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.camunda.bpm.client.spring.annotation.ExternalTaskSubscription;
import org.camunda.bpm.client.task.ExternalTask;
import org.camunda.bpm.client.task.ExternalTaskHandler;
import org.camunda.bpm.client.task.ExternalTaskService;
import org.springframework.stereotype.Component;

@Component
@ExternalTaskSubscription("candidateRating") 
public class CandidateRatingHanlder implements ExternalTaskHandler {

	Logger logger = Logger.getLogger("CandidateRatingHanlder");
	@Override
	public void execute(ExternalTask externalTask, ExternalTaskService externalTaskService) {
		
		 int candidateScore = externalTask.getVariable("candidateScore");

		 //Logic to process based on rating goes here
		 
		 externalTaskService.complete(externalTask);
		 
		 logger.log(Level.INFO, "Processing candidate Score received {0}", new Object[]{ candidateScore});

	}

}


Run the application using the main class file and observe the output where the external tasks implemented in the Spring Boot project will be getting executed.

Spring Framework Spring Boot Task (computing) Camunda application Implementation

Opinions expressed by DZone contributors are their own.

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Distributed Task Synchronization: Leveraging ShedLock in Spring
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: