Skip to content

Commit

Permalink
Moved Zeebe client example from Zeebe repository (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
pihme committed Jun 14, 2022
1 parent 849f110 commit 06cb6a2
Show file tree
Hide file tree
Showing 14 changed files with 885 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
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
8 changes: 8 additions & 0 deletions zeebe-client/README.md
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)
;
77 changes: 77 additions & 0 deletions zeebe-client/pom.xml
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>
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.");
}
}
}
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();
}
}
}
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();
}
}
}
Loading

0 comments on commit 06cb6a2

Please sign in to comment.