Skip to content

Instantly share code, notes, and snippets.

@akeller
Last active November 12, 2020 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akeller/c0d5a76b138c1bc7036d2c72730b0744 to your computer and use it in GitHub Desktop.
Save akeller/c0d5a76b138c1bc7036d2c72730b0744 to your computer and use it in GitHub Desktop.

Ensure Camunda is running. Hint -

$ cd camunda-bpm-tomcat-7.14.0
$ ./start-camunda.sh

Additionally make sure the NodeJS version of the external task worker is not running.

Using Eclipse

Download & Install Eclipse For Java Developers for your machine.

If this is your first time using Eclipse, setup your workspace.

Create a new Maven Project

Start by creating a new Maven project in your IDE. If you’re using Eclipse, you can follow these steps:

In Eclipse, go to File > New > Other …. This opens the New Project Wizard. In the New Project Wizard, select Maven / Maven Project. Click Next.

On the first page of the New Maven Project Wizard, select Create a simple project (you can leave everything else to default). Click Next.

On the second page, configure the Maven coordinates for the project. Since we are setting up a JAR Project, make sure to select Packaging: jar. This should be default along with the version.

  • Group Id: org.camunda.bpm.getstarted
  • Artifact Id: charge-card-worker

When you’re done, click Finish. Eclipse will set up a new Maven project. The project appears in the Project Explorer View.

Add Camunda External Task Client Dependency

The next step consists of setting up the Maven dependency to the external task client for your new process application.

In your Package Explorer, click the arrow to expand your charge-card-worker project. In the bottom of the file tree you should see your pom.xml file.

Delete any autogenerate code.

Your pom.xml file of your project should look like this:

<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>

	<groupId>org.camunda.bpm.getstarted</groupId>
	<artifactId>charge-card-worker</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.camunda.bpm</groupId>
			<artifactId>camunda-external-task-client</artifactId>
			<version>1.4.0</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-simple</artifactId>
			<version>1.6.1</version>
		</dependency>
		<dependency>
			<groupId>javax.xml.bind</groupId>
			<artifactId>jaxb-api</artifactId>
			<version>2.3.1</version>
		</dependency>
	</dependencies>
</project>

Save your project.

Add the Java class

Next, e will create a new ExternalTaskClient which subscribes to the charge-card topic.

You need to create a package and add a Java class, e.g. ChargeCardWorker, to it.

Ctrl + click on your charge-card-worker project in the Package Explorer. Then New > Package

Name your package chargecard. Click Finish. You should see an empty package in our src/main/java folder.

Ctrl + click on your chargecard package. Then New > Class

Make sure your source folder and package values reflect the correct location:

  • Source folder: charge-card-worker/src/main/java
  • Package: chargecard

Name your Class ChargeCardWorker and click Finish.

Delete any autogenerate code. Use the following code in your new class.

package org.camunda.bpm.getstarted.chargecard;

import java.util.logging.Logger;
import java.awt.Desktop;
import java.net.URI;

import org.camunda.bpm.client.ExternalTaskClient;

public class ChargeCardWorker {
  private final static Logger LOGGER = Logger.getLogger(ChargeCardWorker.class.getName());

  public static void main(String[] args) {
    ExternalTaskClient client = ExternalTaskClient.create()
        .baseUrl("http://localhost:8080/engine-rest")
        .asyncResponseTimeout(10000) // long polling timeout
        .build();

    // subscribe to an external task topic as specified in the process
    client.subscribe("charge-card")
        .lockDuration(1000) // the default lock duration is 20 seconds, but you can override this
        .handler((externalTask, externalTaskService) -> {
          // Put your business logic here

          // Get a process variable
          String item = (String) externalTask.getVariable("item");
          Long amount = (Long) externalTask.getVariable("amount");

          LOGGER.info("Charging credit card with an amount of '" + amount + "'€ for the item '" + item + "'...");

          try {
              Desktop.getDesktop().browse(new URI("https://docs.camunda.org/get-started/quick-start/complete"));
          } catch (Exception e) {
              e.printStackTrace();
          }

          // Complete the task
          externalTaskService.complete(externalTask);
        })
        .open();
  }
}

Run the worker

You can run the Java application by right clicking on the class ChargeCardWorker and choosing Run as Java Application.

You will see INFO message in red if your external task worker has been corrected implemented.

Note that the worker should remain running throughout the entirety of this quick start guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment