-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
793 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
|
||
<modules> | ||
<module>java</module> | ||
<module>spring-boot</module> | ||
</modules> | ||
|
||
<properties> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
15 changes: 15 additions & 0 deletions
15
spring-boot/error-handling/src/main/java/com/micasa/tutorial/PaymentApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ror-handling/src/main/java/com/micasa/tutorial/exceptions/CreditCardServiceException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...ror-handling/src/main/java/com/micasa/tutorial/exceptions/InvalidCreditCardException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
.../error-handling/src/main/java/com/micasa/tutorial/handlers/CreditCardChargingHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
spring-boot/error-handling/src/main/java/com/micasa/tutorial/services/CreditCard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
27 changes: 27 additions & 0 deletions
27
spring-boot/error-handling/src/main/java/com/micasa/tutorial/services/CreditCardService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
spring-boot/error-handling/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
76
spring-boot/error-handling/src/main/resources/checkError.form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
Oops, something went wrong.