Skip to content

Commit

Permalink
changed delegates to external task
Browse files Browse the repository at this point in the history
  • Loading branch information
plungu committed Jul 22, 2022
1 parent 07c2c28 commit b5000cb
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ build/

### VS Code ###
.vscode/

### Rando ###
.DS_Store
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- Camunda External Task Client -->
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter-external-task-client</artifactId>
<version>7.17.0</version>
</dependency>

<!-- Spring Dependencies -->
<dependency>
Expand All @@ -39,6 +45,12 @@
<artifactId>fluent-hc</artifactId>
</dependency>

<!-- Mail API -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package io.camunda.getstarted.controller;
package io.camunda.getstarted.facade;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.entity.ContentType;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -19,14 +16,14 @@
import java.util.logging.Logger;

@Controller
public class WorkflowController {
public class ProcessController {

@Value("${camunda.api}")
String camundaApi;

private final Logger LOGGER = Logger.getLogger(Class.class.getName());

public WorkflowController() {}
public ProcessController() {}

/**
* Start a workflow sync with REST
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.camunda.getstarted.service.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Profile("email")
@Service
public class EmailServiceImpl {

JavaMailSender emailSender;

@Autowired
EmailServiceImpl(JavaMailSender emailSender) {
this.emailSender = emailSender;
}

public void sendSimpleMessage(String to, String subject, String text) {

SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("[email protected]");
message.setTo(to);
message.setSubject(subject);
message.setText(text);
emailSender.send(message);
}

public void sendBccMessage(String to, String from, String bcc, String subject, String text) {

SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setBcc(bcc);
message.setText(text);
emailSender.send(message);
}
}
43 changes: 43 additions & 0 deletions src/main/java/io/camunda/getstarted/worker/EmailWorker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.camunda.getstarted.worker;

import io.camunda.getstarted.service.email.EmailServiceImpl;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
@ExternalTaskSubscription("email")
public class EmailWorker implements ExternalTaskHandler {
private static final Logger LOGGER = Logger.getLogger(Class.class.getName());

private EmailServiceImpl emailService;

public EmailWorker() {}

@Autowired
public EmailWorker(EmailServiceImpl emailService) {
this.emailService = emailService;
}

@Override
public void execute(ExternalTask externalTask, ExternalTaskService externalTaskService) {
// get variables
final String emailSubject = externalTask.getVariable("subject");
final String emailBody = externalTask.getVariable("message");
final String emailTo = externalTask.getVariable("email");

// we could call an external service to create the loan documents here
LOGGER.info("\n\n Sending email with message content: "+ emailBody +"\n\n");
emailService.sendSimpleMessage(emailTo, emailSubject, emailBody);

// complete the external task
externalTaskService.complete(externalTask);

}
}
59 changes: 59 additions & 0 deletions src/main/java/io/camunda/getstarted/worker/UserDataWorker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.camunda.getstarted.worker;

import io.camunda.getstarted.service.email.EmailServiceImpl;
import org.apache.http.client.fluent.Request;
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.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.logging.Logger;

@Component
@ExternalTaskSubscription("get-user")
public class UserDataWorker implements ExternalTaskHandler {
private static final Logger LOGGER = Logger.getLogger(Class.class.getName());

@Value("${data.api.uri}")
private String dataApiUri;

private EmailServiceImpl emailService;

public UserDataWorker() {}

@Override
public void execute(ExternalTask externalTask, ExternalTaskService externalTaskService) {

LOGGER.info("\n\n Get business data from business API");

// get variables
final String searchTerm = (String) externalTask.getVariable("searchTerm");
final String emailAddress = (String) externalTask.getVariable("email");

String dataURI = dataApiUri + "/" + searchTerm + emailAddress;
LOGGER.info(" \n\n ====>> Data URI " + dataURI + "\n");

// Use fluent HTTP api to execute request
String content = null;
try {
content = Request.Get(dataURI).execute().returnContent().asString();
} catch (IOException e) {
e.printStackTrace();
}
LOGGER.info(" \n\n ====>> Response Body " + content + "\n");

final String emailBody = (String) externalTask.getVariable("message");

VariableMap variables = Variables.createVariables();
variables.put("user", content);
// complete the external task
externalTaskService.complete(externalTask, variables);

}
}
15 changes: 15 additions & 0 deletions src/main/resources/application-client.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
camunda.bpm.client:
# the URL pointing to the Camunda Platform Runtime REST API
base-url: ${CAMUNDA_REST_BASE}
# defines how many milliseconds the External Tasks are locked until they can be fetched again
lock-duration: 10000
# subscriptions:
# scoreProvider: # topic name of the External Service Task
# variable-names: [] # our business logic doesn't require any variables, so don't fetch them
# process-definition-key: loan_process # only filter for External Tasks with this process definition key
# loanGranter:
# variable-names: customerId, creditScore # only fetch these two variables
# process-definition-key: loan_process

# increase the log level of the application
logging.level.org.camunda.bpm.client: DEBUG
6 changes: 6 additions & 0 deletions src/main/resources/application-data.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Properties for setting up the DATA-API client config
# override properties for environments in respective file
# e.g. application-prod.properties for production

#### Custom Data API
data.api.uri=${DATA_API_URI}
16 changes: 16 additions & 0 deletions src/main/resources/application-email.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Properties for setting up the SMTP config
# override properties for environments in respective file
# e.g. application-prod.properties for production

#spring.freemarker.template-loader-path=classpath:/email-templates

#spring.mail.host=smtp.mailtrap.io
#spring.mail.port=587
#spring.mail.username=a06e8a5a4c8c57
#spring.mail.password=daf405eeb0f14f
#spring.mail.properties.mail.smtp.auth=true
#spring.mail.properties.mail.smtp.starttls.enable=true

#####
spring.mail.host=${MAIL_HOST}
spring.mail.port=${MAIL_PORT}
101 changes: 101 additions & 0 deletions src/main/resources/models/send-email.bpmn
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?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:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:bioc="http://bpmn.io/schema/bpmn/biocolor/1.0" xmlns:color="http://www.omg.org/spec/BPMN/non-normative/color/1.0" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.0.0" camunda:diagramRelationId="a6933e38-c3a8-46f1-ba56-b2cff5897ab0">
<bpmn:collaboration id="Collaboration_0x345xv">
<bpmn:documentation>Notta</bpmn:documentation>
<bpmn:participant id="Participant_send-email" name="Send Email" processRef="send-email" />
</bpmn:collaboration>
<bpmn:process id="send-email" name="Send Email" isExecutable="true">
<bpmn:sequenceFlow id="Flow_09sedic" sourceRef="Activity_1ubwszn" targetRef="Activity_12mvci9" />
<bpmn:sequenceFlow id="Flow_01jky9y" sourceRef="Event_send-email" targetRef="Activity_1ubwszn" />
<bpmn:startEvent id="Event_send-email" name="Start Sample Send Email" camunda:asyncBefore="true">
<bpmn:outgoing>Flow_01jky9y</bpmn:outgoing>
<bpmn:messageEventDefinition id="MessageEventDefinition_1rgq06e" messageRef="Message_0rabq8f" />
</bpmn:startEvent>
<bpmn:endEvent id="Event_02rpi5s" name="End Process">
<bpmn:incoming>Flow_0oym0m4</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_0oym0m4" sourceRef="Activity_send-email" targetRef="Event_02rpi5s" />
<bpmn:sendTask id="Activity_send-email" name="Send Email" camunda:type="external" camunda:topic="email">
<bpmn:extensionElements />
<bpmn:incoming>Flow_03mrh78</bpmn:incoming>
<bpmn:outgoing>Flow_0oym0m4</bpmn:outgoing>
</bpmn:sendTask>
<bpmn:serviceTask id="Activity_1ubwszn" name="Get User" camunda:type="external" camunda:topic="get-user">
<bpmn:extensionElements>
<camunda:inputOutput>
<camunda:inputParameter name="searchTerm">users/search/findContactByEmail?email=</camunda:inputParameter>
</camunda:inputOutput>
</bpmn:extensionElements>
<bpmn:incoming>Flow_01jky9y</bpmn:incoming>
<bpmn:outgoing>Flow_09sedic</bpmn:outgoing>
</bpmn:serviceTask>
<bpmn:sequenceFlow id="Flow_03mrh78" sourceRef="Activity_12mvci9" targetRef="Activity_send-email" />
<bpmn:userTask id="Activity_12mvci9" name="Review Message">
<bpmn:extensionElements>
<camunda:formData>
<camunda:formField id="message" label="Message" type="string" defaultValue="${message}" />
</camunda:formData>
</bpmn:extensionElements>
<bpmn:incoming>Flow_09sedic</bpmn:incoming>
<bpmn:outgoing>Flow_03mrh78</bpmn:outgoing>
</bpmn:userTask>
</bpmn:process>
<bpmn:message id="Message_1cqq97x" />
<bpmn:message id="Message_1kp89j3" name="Message_start-blood-pressure-notification-determination-process" />
<bpmn:message id="Message_0rifso3" name="Message_start-blood-pressure-interpretation" />
<bpmn:message id="Message_1g2hwte" name="Message_start-patient-gap-monitoring" />
<bpmn:message id="Message_0wxfult" name="Message_start-patient-notification" />
<bpmn:message id="Message_0n02ff8" name="Message_start-provider-notification" />
<bpmn:message id="Message_02dzx9s" name="Message_complete-escalation" />
<bpmn:message id="Message_0wp5ggn" name="Message_trigger-blood-pressure-notification" />
<bpmn:message id="Message_0om3086" name="Message_start-blood-pressure-notification-determination-process" />
<bpmn:message id="Message_00kjhev" name="Message_start-blood-pressure-interpretation" />
<bpmn:message id="Message_0rabq8f" name="send-email" />
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Collaboration_0x345xv">
<bpmndi:BPMNShape id="Participant_1bmalqz_di" bpmnElement="Participant_send-email" isHorizontal="true">
<dc:Bounds x="160" y="80" width="1140" height="290" />
<bpmndi:BPMNLabel />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="Flow_0oym0m4_di" bpmnElement="Flow_0oym0m4">
<di:waypoint x="1050" y="200" />
<di:waypoint x="1172" y="200" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_01jky9y_di" bpmnElement="Flow_01jky9y">
<di:waypoint x="298" y="200" />
<di:waypoint x="410" y="200" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_09sedic_di" bpmnElement="Flow_09sedic">
<di:waypoint x="510" y="200" />
<di:waypoint x="670" y="200" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_03mrh78_di" bpmnElement="Flow_03mrh78">
<di:waypoint x="770" y="200" />
<di:waypoint x="950" y="200" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="Event_01z9ka7_di" bpmnElement="Event_send-email" bioc:stroke="#000000" bioc:fill="#ffffff" color:background-color="#ffffff" color:border-color="#000000">
<dc:Bounds x="262" y="182" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="249" y="225" width="64" height="27" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_02rpi5s_di" bpmnElement="Event_02rpi5s">
<dc:Bounds x="1172" y="182" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="1159" y="225" width="63" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0vus8ue_di" bpmnElement="Activity_send-email">
<dc:Bounds x="950" y="160" width="100" height="80" />
<bpmndi:BPMNLabel />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_17v3q21_di" bpmnElement="Activity_1ubwszn">
<dc:Bounds x="410" y="160" width="100" height="80" />
<bpmndi:BPMNLabel />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_14q8pkq_di" bpmnElement="Activity_12mvci9">
<dc:Bounds x="670" y="160" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>

0 comments on commit b5000cb

Please sign in to comment.