Code Monkey home page Code Monkey logo

process-engine-worker's Introduction

Process Engine API Worker

incubating Development branches Maven Central

Purpose of the library

A small opinionated annotated-based SpringBoot worker implementation for creation of external task workers using Process-Engine-API.

How to use

Add the following dependency to your project's class path:

<dependency>
  <groupId>dev.bpm-crafters.process-engine-worker</groupId>
  <artifactId>process-engine-worker-spring-boot-starter</artifactId>
</dependency>

Provide a Spring Component and annotate its method as following and doing everything else manually:

@Component
@RequiredArgsConstructor
public class MyWorker {
  
  private final FetchGoodsInPort fetchGoodsInPort;
  
  @ProcessEngineWorker(topic = "fetchGoods", autoComplete = false)
  public void fetchGoods(
    TaskInformation taskInformation,
    ExternalTaskCompletionApi externalTaskCompletionApi,
    VariableConverter variableConverter,
    Map<String, Object> processPayload
  ) {
    var order = variableConverter.mapToType(payload.get("order"), Order.class);

    // execute some business code
    var fetched = fetchGoodsInPort.fetchGoods(order);

    // complete the task using process engine API
    externalTaskCompletionApi.completeTask(
      new CompleteTaskCmd(taskInformation.getTaskId(), () -> Map.of("shipped", fetched))
    ).get();
  }
}

You can get it even more convenient, if you want the worker to auto complete the task after the execution and use the automatic variable conversion:

@Component
@RequiredArgsConstructor
public class MySmartWorker {

  private final FetchGoodsInPort fetchGoodsInPort;

  @ProcessEngineWorker(topic = "fetchGoods")
  public Map<String, Object> fetchGoods(
    @Variable(name = "order") Order order
  ) {
    // execute some business code
    var fetched = fetchGoodsInPort.fetchGoods(order);
    
    return Map.of("shipped", fetched);
  }
}

Parameter resolution of the method annotated with ProcessEngineWorker is based on a set of strategies registered by the ParameterResolver bean. Currently, the following parameters are resolved:

Type Purpose
TaskInformation Helper abstracting all information about the external task.
ExternTaskCompletionApi API for completing the external task manually
VariableConverter Special utility to read the process variable map and deliver typed value
Map<String, Object> Payload object containing all variables.
Type annotated with @Variable("name) Marker for a process variable.

If the return type of the method is of type Map<String, Object> or compatible and the autoComplete flag is turned on the annotation is true (defaults to true ), the registrar will try to automatically complete the External Task using the returned map as completion variables. If autoComplete is true, but not return value is provided, the task will be completed without payload.

If you want to throw a BPMN error, please throw an instance of a BPMNErrorOccured.

You might want to register your own parameter resolution strategies. For this purpose, please construct the parameter resolver bean on your own and register your own strategies:

import java.beans.BeanProperty

@Configuration
class MyConfig {

  @Bean
  fun myParameterResolver(): ParameterResolver {
    return ParameterResolver.builder().addStrategy(
      ParameterResolutionStrategy(
        parameterMatcher = { param -> ... },
        parameterExtractor = { param, taskInformation, payload, variableConverter, taskCompletionApi -> ... },
      ),
    ).build()
  }

}

Examples

There is an Order fulfillment example, you can easily try out. It follows the approach of clean architecture and uses Process Engine API and Process Engine Worker libraries. To run it, you have several options:

Running locally using Camunda 7 embedded

  1. Start FulfillmentProcessApplication activating profile c7embedded Spring profile.

Running locally using self-managed Camunda 8

  1. Start docker-compose.yaml (this will start containerized Zeebe locally)
  2. Start FulfillmentProcessApplication activating Spring profile c8sm.

Running using Camunda SaaS

  1. Start FulfillmentProcessApplication activating Spring profile c8cloud and pass the following environment variables:
ZEEBE_REGION=..
ZEEBE_CLUSTER_ID=..
ZEEBE_CLIENT_ID=..
ZEEBE_CLIENT_SECRET=...

After starting application, you can either use Open API endpoints or just run the HTTP client tests using your IntelliJ, located in the example directory.

Don't forget to first deploy the process! Either manually via operate / modeler, or with the HTTP client script: c8-deploy-process.http

process-engine-worker's People

Contributors

zambrovski avatar dependabot[bot] avatar p-wunderlich avatar

Stargazers

philipz avatar Dominik Horn avatar Thomas Heinrichs avatar

Watchers

 avatar Jan Galinski avatar  avatar

Forkers

philipz

process-engine-worker's Issues

Support Camunda 8.5

Scenario

  • Library version: 0.0.2
  • Description of your use case: check the library works with Camunda 8.5

Current Behaviour

Tested with Camunda 8.4

Wanted Behaviour

Tested with Camunda 8.5

Possible Workarounds

Try and hope

Support return values

If we have a return value of type Map<String, Any>, the values should be written completing the task.

Control fetched variables by providing restrictions

  • if a worker method has no parameters, no variables should be fetched
  • if a worker method has exactly one parameter of type Map<String, Any>, all varaibles should be fetched
  • if a worker method has other parameters, those must be annotated with @ProcessVariable having a value the name of the process variable, then only those variables should be fetched.

Make it possible to switch the registrar off

Scenario

In I-Tests the registrar should look for process engine API, if it is not there.

Current Behaviour

  • not possible

Wanted Behaviour

  • should be switchable via properties or YAML

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.