-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved Zeebe client example from Zeebe repository (#3)
- Loading branch information
Showing
14 changed files
with
885 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 |
---|---|---|
@@ -1 +1,4 @@ | ||
.idea | ||
/zeebe-client/target/ | ||
/zeebe-client/.flattened-pom.xml | ||
/zeebe-client/zeebe-samples.iml |
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,8 @@ | ||
# Zeebe Java Client Examples | ||
|
||
This Maven project contains a number of examples each performing a specific task using the Zeebe | ||
Java client. | ||
|
||
For an overview and context have a look at | ||
the [Zeebe documentation](https://docs.camunda.io/docs/product-manuals/clients/java-client-examples/index) | ||
; |
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,77 @@ | ||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>zeebe-client</artifactId> | ||
<groupId>org.camunda.community.examples</groupId> | ||
<packaging>jar</packaging> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<name>Zeebe Client Examples</name> | ||
|
||
<properties> | ||
<java.version>17</java.version> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>io.camunda</groupId> | ||
<artifactId>zeebe-client-java</artifactId> | ||
<version>8.0.3</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<version>2.17.2</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-slf4j-impl</artifactId> | ||
<version>2.17.2</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>3.23.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<configuration> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>make-assembly</id> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
<phase>package</phase> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
74 changes: 74 additions & 0 deletions
74
zeebe-client/src/main/java/io/camunda/zeebe/example/cluster/TopologyViewer.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,74 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under | ||
* one or more contributor license agreements. See the NOTICE file distributed | ||
* with this work for additional information regarding copyright ownership. | ||
* Licensed under the Zeebe Community License 1.1. You may not use this file | ||
* except in compliance with the Zeebe Community License 1.1. | ||
*/ | ||
package io.camunda.zeebe.example.cluster; | ||
|
||
import io.camunda.zeebe.client.ZeebeClient; | ||
import io.camunda.zeebe.client.ZeebeClientBuilder; | ||
import io.camunda.zeebe.client.api.response.Topology; | ||
|
||
/** | ||
* Example application that connects to a cluster on Camunda Cloud, or a locally deployed cluster. | ||
* | ||
* <p>When connecting to a cluster in Camunda Cloud, this application assumes that the following | ||
* environment variables are set: | ||
* | ||
* <ul> | ||
* <li>ZEEBE_ADDRESS | ||
* <li>ZEEBE_CLIENT_ID | ||
* <li>ZEEBE_CLIENT_SECRET | ||
* <li>ZEEBE_AUTHORIZATION_SERVER_URL | ||
* </ul> | ||
* | ||
* <p><strong>Hint:</strong> When you create client credentials in Camunda Cloud you have the option | ||
* to download a file with above lines filled out for you. | ||
* | ||
* <p>When {@code ZEEBE_ADDRESS} is not set, it connects to a broker running on localhost with | ||
* default ports | ||
*/ | ||
public final class TopologyViewer { | ||
|
||
public static void main(final String[] args) { | ||
final String defaultAddress = "localhost:26500"; | ||
final String envVarAddress = System.getenv("ZEEBE_ADDRESS"); | ||
|
||
final ZeebeClientBuilder clientBuilder; | ||
final String contactPoint; | ||
if (envVarAddress != null) { | ||
/* Connect to Camunda Cloud Cluster, assumes that credentials are set in environment variables. | ||
* See JavaDoc on class level for details | ||
*/ | ||
contactPoint = envVarAddress; | ||
clientBuilder = ZeebeClient.newClientBuilder().gatewayAddress(envVarAddress); | ||
} else { | ||
// connect to local deployment; assumes that authentication is disabled | ||
contactPoint = defaultAddress; | ||
clientBuilder = ZeebeClient.newClientBuilder().gatewayAddress(defaultAddress).usePlaintext(); | ||
} | ||
|
||
try (final ZeebeClient client = clientBuilder.build()) { | ||
System.out.println("Requesting topology with initial contact point " + contactPoint); | ||
|
||
final Topology topology = client.newTopologyRequest().send().join(); | ||
|
||
System.out.println("Topology:"); | ||
topology | ||
.getBrokers() | ||
.forEach( | ||
b -> { | ||
System.out.println(" " + b.getAddress()); | ||
b.getPartitions() | ||
.forEach( | ||
p -> | ||
System.out.println( | ||
" " + p.getPartitionId() + " - " + p.getRole())); | ||
}); | ||
|
||
System.out.println("Done."); | ||
} | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
zeebe-client/src/main/java/io/camunda/zeebe/example/data/HandleVariablesAsPojo.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,116 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under | ||
* one or more contributor license agreements. See the NOTICE file distributed | ||
* with this work for additional information regarding copyright ownership. | ||
* Licensed under the Zeebe Community License 1.1. You may not use this file | ||
* except in compliance with the Zeebe Community License 1.1. | ||
*/ | ||
package io.camunda.zeebe.example.data; | ||
|
||
import io.camunda.zeebe.client.ZeebeClient; | ||
import io.camunda.zeebe.client.ZeebeClientBuilder; | ||
import io.camunda.zeebe.client.api.response.ActivatedJob; | ||
import io.camunda.zeebe.client.api.worker.JobClient; | ||
import io.camunda.zeebe.client.api.worker.JobHandler; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Example application that connects to a cluster on Camunda Cloud, or a locally deployed cluster. | ||
* | ||
* <p>When connecting to a cluster in Camunda Cloud, this application assumes that the following | ||
* environment variables are set: | ||
* | ||
* <ul> | ||
* <li>ZEEBE_ADDRESS | ||
* <li>ZEEBE_CLIENT_ID | ||
* <li>ZEEBE_CLIENT_SECRET | ||
* <li>ZEEBE_AUTHORIZATION_SERVER_URL | ||
* </ul> | ||
* | ||
* <p><strong>Hint:</strong> When you create client credentials in Camunda Cloud you have the option | ||
* to download a file with above lines filled out for you. | ||
* | ||
* <p>When {@code ZEEBE_ADDRESS} is not set, it connects to a broker running on localhost with | ||
* default ports | ||
*/ | ||
public final class HandleVariablesAsPojo { | ||
public static void main(final String[] args) { | ||
final String defaultAddress = "localhost:26500"; | ||
final String envVarAddress = System.getenv("ZEEBE_ADDRESS"); | ||
|
||
final ZeebeClientBuilder clientBuilder; | ||
if (envVarAddress != null) { | ||
/* Connect to Camunda Cloud Cluster, assumes that credentials are set in environment variables. | ||
* See JavaDoc on class level for details | ||
*/ | ||
clientBuilder = ZeebeClient.newClientBuilder().gatewayAddress(envVarAddress); | ||
} else { | ||
// connect to local deployment; assumes that authentication is disabled | ||
clientBuilder = ZeebeClient.newClientBuilder().gatewayAddress(defaultAddress).usePlaintext(); | ||
} | ||
|
||
try (final ZeebeClient client = clientBuilder.build()) { | ||
final Order order = new Order(); | ||
order.setOrderId(31243); | ||
|
||
client | ||
.newCreateInstanceCommand() | ||
.bpmnProcessId("demoProcess") | ||
.latestVersion() | ||
.variables(order) | ||
.send() | ||
.join(); | ||
|
||
client.newWorker().jobType("foo").handler(new DemoJobHandler()).open(); | ||
|
||
// run until System.in receives exit command | ||
waitUntilSystemInput("exit"); | ||
} | ||
} | ||
|
||
private static void waitUntilSystemInput(final String exitCode) { | ||
try (final Scanner scanner = new Scanner(System.in)) { | ||
while (scanner.hasNextLine()) { | ||
final String nextLine = scanner.nextLine(); | ||
if (nextLine.contains(exitCode)) { | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static class Order { | ||
private long orderId; | ||
private double totalPrice; | ||
|
||
public long getOrderId() { | ||
return orderId; | ||
} | ||
|
||
public void setOrderId(final long orderId) { | ||
this.orderId = orderId; | ||
} | ||
|
||
public double getTotalPrice() { | ||
return totalPrice; | ||
} | ||
|
||
public void setTotalPrice(final double totalPrice) { | ||
this.totalPrice = totalPrice; | ||
} | ||
} | ||
|
||
private static class DemoJobHandler implements JobHandler { | ||
@Override | ||
public void handle(final JobClient client, final ActivatedJob job) { | ||
// read the variables of the job | ||
final Order order = job.getVariablesAsType(Order.class); | ||
System.out.println("new job with orderId: " + order.getOrderId()); | ||
|
||
// update the variables and complete the job | ||
order.setTotalPrice(46.50); | ||
|
||
client.newCompleteCommand(job.getKey()).variables(order).send(); | ||
} | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
zeebe-client/src/main/java/io/camunda/zeebe/example/job/JobWorkerCreator.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,94 @@ | ||
/* | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under | ||
* one or more contributor license agreements. See the NOTICE file distributed | ||
* with this work for additional information regarding copyright ownership. | ||
* Licensed under the Zeebe Community License 1.1. You may not use this file | ||
* except in compliance with the Zeebe Community License 1.1. | ||
*/ | ||
package io.camunda.zeebe.example.job; | ||
|
||
import io.camunda.zeebe.client.ZeebeClient; | ||
import io.camunda.zeebe.client.ZeebeClientBuilder; | ||
import io.camunda.zeebe.client.api.response.ActivatedJob; | ||
import io.camunda.zeebe.client.api.worker.JobClient; | ||
import io.camunda.zeebe.client.api.worker.JobHandler; | ||
import io.camunda.zeebe.client.api.worker.JobWorker; | ||
import java.time.Duration; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Example application that connects to a cluster on Camunda Cloud, or a locally deployed cluster. | ||
* | ||
* <p>When connecting to a cluster in Camunda Cloud, this application assumes that the following | ||
* environment variables are set: | ||
* | ||
* <ul> | ||
* <li>ZEEBE_ADDRESS | ||
* <li>ZEEBE_CLIENT_ID | ||
* <li>ZEEBE_CLIENT_SECRET | ||
* <li>ZEEBE_AUTHORIZATION_SERVER_URL | ||
* </ul> | ||
* | ||
* <p><strong>Hint:</strong> When you create client credentials in Camunda Cloud you have the option | ||
* to download a file with above lines filled out for you. | ||
* | ||
* <p>When {@code ZEEBE_ADDRESS} is not set, it connects to a broker running on localhost with | ||
* default ports | ||
*/ | ||
public final class JobWorkerCreator { | ||
public static void main(final String[] args) { | ||
final String defaultAddress = "localhost:26500"; | ||
final String envVarAddress = System.getenv("ZEEBE_ADDRESS"); | ||
|
||
final ZeebeClientBuilder clientBuilder; | ||
if (envVarAddress != null) { | ||
/* Connect to Camunda Cloud Cluster, assumes that credentials are set in environment variables. | ||
* See JavaDoc on class level for details | ||
*/ | ||
clientBuilder = ZeebeClient.newClientBuilder().gatewayAddress(envVarAddress); | ||
} else { | ||
// connect to local deployment; assumes that authentication is disabled | ||
clientBuilder = ZeebeClient.newClientBuilder().gatewayAddress(defaultAddress).usePlaintext(); | ||
} | ||
|
||
final String jobType = "foo"; | ||
|
||
try (final ZeebeClient client = clientBuilder.build()) { | ||
|
||
System.out.println("Opening job worker."); | ||
|
||
try (final JobWorker workerRegistration = | ||
client | ||
.newWorker() | ||
.jobType(jobType) | ||
.handler(new ExampleJobHandler()) | ||
.timeout(Duration.ofSeconds(10)) | ||
.open()) { | ||
System.out.println("Job worker opened and receiving jobs."); | ||
|
||
// run until System.in receives exit command | ||
waitUntilSystemInput("exit"); | ||
} | ||
} | ||
} | ||
|
||
private static void waitUntilSystemInput(final String exitCode) { | ||
try (final Scanner scanner = new Scanner(System.in)) { | ||
while (scanner.hasNextLine()) { | ||
final String nextLine = scanner.nextLine(); | ||
if (nextLine.contains(exitCode)) { | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static class ExampleJobHandler implements JobHandler { | ||
@Override | ||
public void handle(final JobClient client, final ActivatedJob job) { | ||
// here: business logic that is executed with every job | ||
System.out.println(job); | ||
client.newCompleteCommand(job.getKey()).send().join(); | ||
} | ||
} | ||
} |
Oops, something went wrong.