-
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.
add example with one worker per class implementing JobHandler
- Loading branch information
1 parent
73518e5
commit f94e093
Showing
8 changed files
with
448 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<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> | ||
<groupId>com.camunda.consulting</groupId> | ||
<artifactId>zeebe-ejb-example-worker-interface</artifactId> | ||
<version>0.0.2-SNAPSHOT</version> | ||
<packaging>war</packaging> | ||
<name>Zeebe EJB Example Application</name> | ||
<description>Zeebe Client running in an Java EE Server</description> | ||
|
||
<properties> | ||
<zeebe.version>8.1.0</zeebe.version> | ||
|
||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
|
||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<failOnMissingWebXml>false</failOnMissingWebXml> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.camunda.community.client</groupId> | ||
<artifactId>zeebe-ejb-client</artifactId> | ||
<version>0.0.2-SNAPSHOT</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-annotations</artifactId> | ||
</exclusion> | ||
<exclusion> | ||
<groupId>com.fasterxml.jackson.dataformat</groupId> | ||
<artifactId>jackson-dataformat-yaml</artifactId> | ||
</exclusion> | ||
<exclusion> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-core</artifactId> | ||
</exclusion> | ||
<exclusion> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.dataformat</groupId> | ||
<artifactId>jackson-dataformat-yaml</artifactId> | ||
<version>2.12.3</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>javax</groupId> | ||
<artifactId>javaee-api</artifactId> | ||
<version>8.0.1</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<finalName>${project.artifactId}</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-war-plugin</artifactId> | ||
<version>3.3.2</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.10.1</version> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.22.2</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.wildfly.plugins</groupId> | ||
<artifactId>wildfly-maven-plugin</artifactId> | ||
<version>2.0.1.Final</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<version>3.1.0</version> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-release-plugin</artifactId> | ||
<version>3.0.0-M7</version> | ||
<configuration> | ||
<updateDependencies>false</updateDependencies> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>com.diffplug.spotless</groupId> | ||
<artifactId>spotless-maven-plugin</artifactId> | ||
<version>2.34.0</version> | ||
<configuration> | ||
<java> | ||
<googleJavaFormat> | ||
</googleJavaFormat> | ||
</java> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
49 changes: 49 additions & 0 deletions
49
...er-interface-example/src/main/java/org/camunda/consulting/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,49 @@ | ||
package org.camunda.consulting.services; | ||
|
||
import java.time.LocalDate; | ||
import javax.ejb.Stateless; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Stateless | ||
public class CreditCardService { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(CreditCardService.class); | ||
|
||
public void chargeAmount(String cardNumber, String cvc, String expiryDate, Double amount) { | ||
if (!validateExpiryDate(expiryDate)) { | ||
throw new RuntimeException( | ||
"Invalid expiry date: " + expiryDate + "\nExpiry date must be in the future."); | ||
} | ||
|
||
LOG.info( | ||
"charging card {} that expires on {} and has cvc {} with amount of {}", | ||
cardNumber, | ||
expiryDate, | ||
cvc, | ||
amount); | ||
|
||
LOG.info("payment completed"); | ||
} | ||
|
||
boolean validateExpiryDate(String expiryDate) { | ||
if (expiryDate.length() != 5) { | ||
return false; | ||
} | ||
try { | ||
int month = Integer.parseInt(expiryDate.substring(0, 2)); | ||
int year = Integer.parseInt(expiryDate.substring(3, 5)) + 2000; | ||
LocalDate now = LocalDate.now(); | ||
if (month < 1 || month > 12 || year < now.getYear()) { | ||
return false; | ||
} | ||
if (year > now.getYear() || (year == now.getYear() && month >= now.getMonthValue())) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} catch (NumberFormatException | IndexOutOfBoundsException e) { | ||
return false; | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...rker-interface-example/src/main/java/org/camunda/consulting/services/CustomerService.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,54 @@ | ||
package org.camunda.consulting.services; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import javax.ejb.Stateless; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Stateless | ||
public class CustomerService { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(CustomerService.class); | ||
|
||
/** The customer credit are the last digits of the customer id */ | ||
private Pattern pattern = Pattern.compile("(.*?)(\\d*)"); | ||
|
||
/** | ||
* Deduct the credit for the given customer and the given amount | ||
* | ||
* @param customerId | ||
* @param amount | ||
* @return the open order amount | ||
*/ | ||
public Double deductCredit(String customerId, Double amount) { | ||
Double credit = getCustomerCredit(customerId); | ||
Double openAmount; | ||
Double deductedCredit; | ||
if (credit > amount) { | ||
deductedCredit = amount; | ||
openAmount = 0.0; | ||
} else { | ||
openAmount = amount - credit; | ||
deductedCredit = credit; | ||
} | ||
LOG.info("charged {} from the credit, open amount is {}", deductedCredit, openAmount); | ||
return openAmount; | ||
} | ||
|
||
/** | ||
* Get the current customer credit | ||
* | ||
* @param customerId | ||
* @return the current credit of the given customer | ||
*/ | ||
public Double getCustomerCredit(String customerId) { | ||
Double credit = 0.0; | ||
Matcher matcher = pattern.matcher(customerId); | ||
if (matcher.matches() && matcher.group(2) != null && matcher.group(2).length() > 0) { | ||
credit = Double.valueOf(matcher.group(2)); | ||
} | ||
LOG.info("customer {} has credit of {}", customerId, credit); | ||
return credit; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...rker-interface-example/src/main/java/org/camunda/consulting/workers/CreditCardWorker.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,55 @@ | ||
package org.camunda.consulting.workers; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.camunda.consulting.services.CreditCardService; | ||
|
||
import com.camunda.consulting.zeebe_ejb.JobWorker; | ||
|
||
import io.camunda.zeebe.client.api.response.ActivatedJob; | ||
import io.camunda.zeebe.client.api.worker.JobClient; | ||
import io.camunda.zeebe.client.api.worker.JobHandler; | ||
|
||
@ApplicationScoped | ||
@JobWorker(type = "creditCardCharging" /*, autoComplete = true */) | ||
public class CreditCardWorker implements JobHandler { | ||
|
||
private CreditCardService creditCardService; | ||
|
||
@Inject | ||
public CreditCardWorker(CreditCardService creditCardService) { | ||
super(); | ||
this.creditCardService = creditCardService; | ||
} | ||
|
||
public CreditCardWorker() { | ||
|
||
} | ||
|
||
@Override | ||
public void handle(JobClient client, ActivatedJob job) throws Exception { | ||
// extract variables from process instance | ||
String cardNumber = (String) job.getVariablesAsMap().get("cardNumber"); | ||
String cvc = (String) job.getVariablesAsMap().get("cvc"); | ||
String expiryData = (String) job.getVariablesAsMap().get("expiryDate"); | ||
Double amount = (Double) job.getVariablesAsMap().get("openAmount"); | ||
|
||
// execute business logic using the variables | ||
if (cvc.equals("789")) { | ||
throw new RuntimeException("CVC invalid!"); | ||
} | ||
|
||
try { | ||
creditCardService.chargeAmount(cardNumber, cvc, expiryData, amount); | ||
} catch (Exception exc) { | ||
client | ||
.newThrowErrorCommand(job) | ||
.errorCode("chargingError") | ||
.errorMessage("We failed to charge credit card with card number " + cardNumber) | ||
.send() | ||
.join(); | ||
} | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
...-interface-example/src/main/java/org/camunda/consulting/workers/CustomerCreditWorker.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,62 @@ | ||
package org.camunda.consulting.workers; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.camunda.consulting.services.CustomerService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.camunda.consulting.zeebe_ejb.JobWorker; | ||
|
||
import io.camunda.zeebe.client.api.response.ActivatedJob; | ||
import io.camunda.zeebe.client.api.worker.JobClient; | ||
import io.camunda.zeebe.client.api.worker.JobHandler; | ||
|
||
@ApplicationScoped | ||
@JobWorker(type = "creditDeduction", autoComplete = false) | ||
public class CustomerCreditWorker implements JobHandler { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(CustomerCreditWorker.class); | ||
|
||
private CustomerService customerService; | ||
|
||
@Inject | ||
public CustomerCreditWorker(CustomerService customerService) { | ||
super(); | ||
this.customerService = customerService; | ||
} | ||
|
||
public CustomerCreditWorker() { | ||
|
||
} | ||
|
||
@Override | ||
public void handle(JobClient client, ActivatedJob job) throws Exception { | ||
LOG.info("handler invoked for job {}", job); | ||
|
||
Map<String, Object> resultVariables = creditDeduction(job); | ||
|
||
client.newCompleteCommand(job).variables(resultVariables).send().join(); | ||
LOG.info("handler completed job {}", job); | ||
} | ||
|
||
private Map<String, Object> creditDeduction(ActivatedJob job) { | ||
Map<String, Object> variables = job.getVariablesAsMap(); | ||
String customerId = (String) variables.get("customerId"); | ||
Double amount = (Double) variables.get("orderTotal"); | ||
|
||
// execute business logic using the variables | ||
Double openAmount = customerService.deductCredit(customerId, amount); | ||
Double customerCredit = customerService.getCustomerCredit(customerId); | ||
|
||
// return the results | ||
Map<String, Object> resultVariables = new HashMap<>(); | ||
resultVariables.put("openAmount", openAmount); | ||
resultVariables.put("customerCredit", customerCredit); | ||
return resultVariables; | ||
} | ||
} |
Oops, something went wrong.