|
| 1 | +--- |
| 2 | +title: Process Engine Worker |
| 3 | +--- |
| 4 | + |
| 5 | +Process Engine Worker is an independent component built on top of Process Engine API in order to accelerate the development of agnostic workers |
| 6 | +for any process engine supported by Process Engine API in Spring Boot ecosystem. By doing so, it abstracts from specific worker clients and |
| 7 | +API and allows to build universal workers. |
| 8 | + |
| 9 | +First of all add the Process Engine Worker dependency to you project classpath. In Maven add the following to you `pom.xml`: |
| 10 | + |
| 11 | +```xml |
| 12 | +<dependency> |
| 13 | + <groupId>dev.bpm-crafters.process-engine-worker</groupId> |
| 14 | + <artifactId>process-engine-worker-spring-boot-starter</artifactId> |
| 15 | + <version>${process-engine-worker.version}</version> |
| 16 | +</dependency> |
| 17 | +``` |
| 18 | + |
| 19 | +Now create a simple Spring component and annotate a method with a special annotation `@ProcessEngineWorker`: |
| 20 | + |
| 21 | +```java |
| 22 | +@Component |
| 23 | +@RequiredArgsConstructor |
| 24 | +public class MySmartWorker { |
| 25 | + |
| 26 | + private final FetchGoodsInPort fetchGoodsInPort; |
| 27 | + |
| 28 | + @ProcessEngineWorker(topic = "fetchGoods") |
| 29 | + public Map<String, Object> fetchGoods( |
| 30 | + @Variable(name = "order") Order order |
| 31 | + ) { |
| 32 | + // execute some business code |
| 33 | + var fetched = fetchGoodsInPort.fetchGoods(order); |
| 34 | + |
| 35 | + return Map.of("shipped", fetched); |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## Method parameter resolution |
| 41 | + |
| 42 | +Parameter resolution of the method annotated with `ProcessEngineWorker` is based on a set of strategies |
| 43 | +registered by the `ParameterResolver` bean. Currently, the following parameters are resolved: |
| 44 | + |
| 45 | +| Type | Purpose | |
| 46 | +|----------------------------------------|---------------------------------------------------------------------------| |
| 47 | +| TaskInformation | Helper abstracting all information about the external task. | |
| 48 | +| ExternTaskCompletionApi | API for completing the external task manually | |
| 49 | +| VariableConverter | Special utility to read the process variable map and deliver typed value | |
| 50 | +| Map<String, Object> | Payload object containing all variables. | |
| 51 | +| Type annotated with @Variable("name") | Marker for a process variable. | |
| 52 | + |
| 53 | +Usually, the requested variable is mandatory and the parameter resolver reports an error, if the requested variable is not |
| 54 | +available in the process payload. If you want to inject the variable only if it exists in the payload you have two options. |
| 55 | +Either you set the parameter `@Variable(name = "...", mandatory = false)` or you use `Optional<T>` instead of `T` as a variable |
| 56 | +type. If you are using Kotlin and don't like `Optional`, make sure to declare variable type as nullable (`T?` instead of `T`) and |
| 57 | +set the mandatory flag to `false`. |
| 58 | + |
| 59 | +## Method return type |
| 60 | + |
| 61 | +If the return type of the method is of type `Map<String, Object>` or compatible and the `autoComplete` flag is turned |
| 62 | +on the annotation (defaults to `true`), the library will try to automatically complete the External Task |
| 63 | +using the returned map as completion variables. If `autoComplete` is `true`, but no return value is provided, the task |
| 64 | +will be completed without empty payload. This functionality is provided by the `ResultResolver` based on registered strategies. |
| 65 | + |
| 66 | +## Throwing a BPMN Error |
| 67 | + |
| 68 | +If you want to throw a BPMN error, please throw an instance of a `BPMNErrorOccured` exception from the method body. The exception |
| 69 | +is a checked exception, in order to comply with Spring behavior of not rolling back transaction on checked exceptions. |
| 70 | + |
| 71 | +## Transactional support |
| 72 | + |
| 73 | +The worker method can be marked transactional by adding Spring or Jakarta EE transactional annotations to the method or to the worker class. |
| 74 | +If the annotation `@org.springframework.transaction.annotation.Transactional` with propagation `REQUIRES`, `REQUIRES_NEW`, `SUPPORTS` and `MANDATORY` |
| 75 | +is used, the library will execute the worker method and the completion of the external task via API in the same transaction. This will lead to |
| 76 | +a transaction rollback, if the external task can't be completed (e.g. due to a network error). |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
0 commit comments