Skip to content

Commit

Permalink
Add Spring Boot example
Browse files Browse the repository at this point in the history
  • Loading branch information
aowss committed Dec 5, 2023
1 parent 079aafd commit 01b57c8
Show file tree
Hide file tree
Showing 19 changed files with 793 additions and 1 deletion.
13 changes: 12 additions & 1 deletion docs/Camunda.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
:figure-caption!:
:source-highlighter: highlight.js
:source-language: java
:imagesdir: res
:toc2:

Expand Down Expand Up @@ -122,9 +124,18 @@ If you don't do that and use assertions about BPMN elements that are after the s

=== Testing

* How can we test that a task exited through the boundary event ?

.Not working
----
assertThat(processInstance)
.hasPassedElement("BoundaryEvent_InvalidCardExpiryDate")
----

==== Java

* How can we avoid redeploying the BPMN diagram before each test ?

[source, java]
----
@ZeebeProcessTest
public class ProcessTest {
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<modules>
<module>java</module>
<module>spring-boot</module>
</modules>

<properties>
Expand Down
3 changes: 3 additions & 0 deletions spring-boot/error-handling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Error Handling

This is the implementation for [Camunda 8 - Error Handling](https://academy.camunda.com/c8-error-handling).
107 changes: 107 additions & 0 deletions spring-boot/error-handling/docs/Details.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
:figure-caption!:
:source-highlighter: highlight.js
:source-language: java
:imagesdir: res
:toc2:

= Error Handling

.Process
image::paymentProcess.png[Process, role="thumb"]

This example shows:

* how to configure the client

[cols="2a,2a"]
|===
|Java |Spring Boot
|
.`Main.java`
----
String ZEEBE_ADDRESS = "...";
String ZEEBE_CLIENT_ID = "...";
String ZEEBE_CLIENT_SECRET = "...";
String ZEEBE_AUTHORIZATION_SERVER_URL = "...";
String ZEEBE_TOKEN_AUDIENCE = "...";
var credentialsProvider = new OAuthCredentialsProviderBuilder()
.authorizationServerUrl(ZEEBE_AUTHORIZATION_SERVER_URL)
.audience(ZEEBE_TOKEN_AUDIENCE)
.clientId(ZEEBE_CLIENT_ID)
.clientSecret(ZEEBE_CLIENT_SECRET)
.build();
ZeebeClient client = ZeebeClient.newClientBuilder()
.gatewayAddress(ZEEBE_ADDRESS)
.credentialsProvider(credentialsProvider)
.build())
----
|
[source, yaml]
.`application.yaml`
----
zeebe.client:
cloud:
region: ...
clusterId: ...
clientId: ...
clientSecret: ...
----
|===

* how to deploy process

@SpringBootApplication
@EnableZeebeClient
@Deployment(resources = "classpath*:*.bpmn")


* how to _wire_ a service task:

[cols="1, 2a,2a"]
|===
||Java |Spring Boot

|
.`Main.java`
----
final var credentialsProvider = new OAuthCredentialsProviderBuilder()
.authorizationServerUrl(ZEEBE_AUTHORIZATION_SERVER_URL)
.audience(ZEEBE_TOKEN_AUDIENCE)
.clientId(ZEEBE_CLIENT_ID)
.clientSecret(ZEEBE_CLIENT_SECRET)
.build();
final ZeebeClient client = ZeebeClient.newClientBuilder()
.gatewayAddress(ZEEBE_ADDRESS)
.credentialsProvider(credentialsProvider)
.build())
----
|===

* how to handle a task

.`CreditCardChargingHandler.java`
----
@Override
public void handle(JobClient client, ActivatedJob job) throws Exception { <1>
var reference = (String) job.getVariable("orderReference"); <2>
var confirmationNumber = creditCardService.chargeCreditCard(reference); <3>
var outputVariables = Map.of("confirmation", confirmationNumber); <4>
client.newCompleteCommand(job.getKey()) <5>
.variables(outputVariables) <4>
.send()
.join();
}
----
<1> Implement the ``JobHandler``'s `handle` method
<2> Get variables from the `ActivatedJob`
<3> Call the service
<4> Add job's output to the ``ActivatedJob``'s variables
<5> Notify the engine that the job completed successfully

@JobWorker(type = "send-rejection")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions spring-boot/error-handling/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.micasa.tutorial</groupId>
<artifactId>camunda-spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>camunda-spring-boot-error-handling</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>error-handling</name>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>io.camunda.spring</groupId>
<artifactId>spring-boot-starter-camunda-test</artifactId>
<version>${camunda.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.micasa.tutorial;

import io.camunda.zeebe.spring.client.EnableZeebeClient;
import io.camunda.zeebe.spring.client.annotation.Deployment;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableZeebeClient
@Deployment(resources = "classpath*:*.bpmn")
public class PaymentApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.micasa.tutorial.exceptions;

public class CreditCardServiceException extends RuntimeException {

public CreditCardServiceException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.micasa.tutorial.exceptions;

public class InvalidCreditCardException extends Exception {

public InvalidCreditCardException() {
this("Invalid credit card");
}

public InvalidCreditCardException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.micasa.tutorial.handlers;

import com.micasa.tutorial.exceptions.CreditCardServiceException;
import com.micasa.tutorial.exceptions.InvalidCreditCardException;
import com.micasa.tutorial.services.CreditCard;
import com.micasa.tutorial.services.CreditCardService;
import io.camunda.zeebe.client.api.response.ActivatedJob;
import io.camunda.zeebe.spring.client.annotation.JobWorker;
import io.camunda.zeebe.spring.client.exception.ZeebeBpmnError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Component
public class CreditCardChargingHandler {

@Autowired
private CreditCardService creditCardService;

@JobWorker(type = "chargeCreditCard", autoComplete = true)
public Map<String, Object> handle(ActivatedJob job) throws CreditCardServiceException {
System.out.println("charge credit card [ retries = " + job.getRetries() + " ]");
var reference = (String) job.getVariable("orderReference");
var amount = (Double) job.getVariable("orderAmount");
var creditCard = new CreditCard(
(String) job.getVariable("cardNumber"),
YearMonth.parse((String) job.getVariable("cardExpiry"), DateTimeFormatter.ofPattern("MM/yyyy")),
(String) job.getVariable("cardCVC")
);

try {
var confirmationNumber = creditCardService.chargeCreditCard(reference, amount, creditCard);
return Map.of("confirmation", confirmationNumber);
} catch (InvalidCreditCardException icce) {
throw new ZeebeBpmnError("invalidCreditCardException", icce.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.micasa.tutorial.services;

import java.time.YearMonth;

public record CreditCard(String cardNumber, YearMonth expiryDate, String CVC) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.micasa.tutorial.services;

import com.micasa.tutorial.exceptions.CreditCardServiceException;
import com.micasa.tutorial.exceptions.InvalidCreditCardException;
import org.springframework.stereotype.Component;

import java.time.YearMonth;
import java.util.UUID;

@Component
public class CreditCardService {

public String chargeCreditCard(String transactionNumber, double amount, CreditCard creditCard) throws InvalidCreditCardException, CreditCardServiceException {
System.out.println(STR. "Charging \{ amount } to credit card \{ creditCard } for transaction \{ transactionNumber }" );
if (creditCard.expiryDate().isBefore(YearMonth.now())) {
System.out.println("The credit card's expiry date is invalid: " + creditCard.expiryDate());
throw new InvalidCreditCardException();
}
if (transactionNumber.equalsIgnoreCase("invalid")) {
var message = "The transaction number is invalid: " + transactionNumber;
System.out.println(message);
throw new CreditCardServiceException(message);
}
return UUID.randomUUID().toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# https://github.com/camunda-community-hub/spring-zeebe#configuring-camunda-platform-8-saas-connection
# https://github.com/camunda-community-hub/spring-zeebe#additional-configuration-options

zeebe.client:
cloud:
region: ont-1
clusterId: 4784612d-1495-4f2a-951d-049c8b985f06
clientId: jMNcIFp.GJ1-ZYK~Av-8zLHM7xmYCTLs
clientSecret: dbz--V.YX9JzRzF_44iL9DNz.h0_1S-qpPbl~2xYt6f0ua5h3CWQ2wVulQ68b-Rm
76 changes: 76 additions & 0 deletions spring-boot/error-handling/src/main/resources/checkError.form
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"executionPlatform": "Camunda Cloud",
"executionPlatformVersion": "8.2.0",
"exporter": {
"name": "Camunda Web Modeler",
"version": "8516401"
},
"schemaVersion": 10,
"components": [
{
"text": "## Check Credit Card Details",
"type": "text",
"id": "Field_1y7r1ul",
"layout": {
"row": "Row_1yzjcyo"
}
},
{
"label": "Reference",
"type": "textfield",
"layout": {
"row": "Row_1ammndy",
"columns": null
},
"id": "Field_035kqla",
"key": "reference"
},
{
"label": "Amount",
"type": "textfield",
"id": "Field_0p8a9xa",
"key": "amount",
"layout": {
"row": "Row_0h6eq07"
}
},
{
"label": "Card Number",
"type": "textfield",
"id": "Field_1j2py1a",
"key": "cardNumber",
"layout": {
"row": "Row_15opzdy"
}
},
{
"label": "Card Expiry",
"type": "textfield",
"id": "Field_1l2tmgg",
"key": "cardExpiry",
"layout": {
"row": "Row_0a2rkgg"
}
},
{
"label": "Card CVC",
"type": "textfield",
"id": "Field_0pydzhj",
"key": "cardCVC",
"layout": {
"row": "Row_1nh9icr"
}
},
{
"label": "Valid Credit Card?",
"type": "checkbox",
"id": "Field_08wi408",
"key": "isValidCreditCard",
"layout": {
"row": "Row_0fw9363"
}
}
],
"type": "default",
"id": "checkError"
}
Loading

0 comments on commit 01b57c8

Please sign in to comment.