Skip to content

How to write delegates

bespaltovyj edited this page Jan 3, 2022 · 5 revisions

Define the delegate

Delegate is a method with an annotaion @DelegateExecute in class marked with an annotation @CamundaDelegate.

Example:

@CamundaDelegate
class SimpleDelegate {

    @DelegateExecute
    fun doNothing() {}
}

This delegate can be accessed by name simpleDelegate_doNothing.

By delault the name consists of 2 parts: the name of the class and the name of the method separated by _. Each part can be overrideв via a parameter in the annotation.

Example:

@CamundaDelegate("nonSimpleDelegate")
class SimpleDelegate {

    @DelegateExecute
    fun doNothing() {}
}

Delegate name is nonSimpleDelegate_doNothing.

If necessary, you can add aliases for the delegate using the annotation DelegateAliases.

Each delegate name must be unique

Delegate inputs

Each required method parameter must be resolved by DelegateArgumentResolver.

Common resolvers:

Example:

@CamundaDelegate
class SimpleDelegate {

    @DelegateExecute
    fun doNothing(
      @Variable("local") localVariable: String,
      @Variable("optional") optionalVariable: String?,
      @Variable("global", isLocal = false) globalVariable: String,
      @DelegateExecutionAnn execution: DelegateExecution
    ) {}
}

In Kotlin method argumets with ? is optional. In Java optional parameter should be marked with any @Nullable annotation

You can write custom resolvers

Delegate outputs

Return value from delegate method can be processed by DelegateInterceptor(by default return value is ignored).

Common interceptors:

  • SingleResultExecutionWriter - this interceptor write single variable to delegate execustion. To use it mark method an annotaion @SingleResultVariable and set variable name
  • MultipleResultExecutionWriter - this interceptor write multiple variables to delegate execustion. To use it mark method an annotaion @MultipleResults and all field from return class marked an annotation @ResultVariable will be recorded into delegate execution

Example:

@CamundaDelegate
class SimpleDelegate {

    @DelegateExecute
    @SingleResultVariable("result")
    fun doNothing(
    ): String {
      return "string";
    }
}

You can write custom delegate interceptors with your logic.