Custom functions for your Serverless Workflow service

The Cloud Native Computing Foundation (CNCF) specification supports the custom function type, which enables the implementations to extend the function definition capability.

Kogito supports the java and sysout custom types.

The CNCF specification does not support java and sysout functions. Therefore, these functions might not be portable across other implementations.

sysout custom function

You can use the sysout function for debugging or for quick demonstrations as shown in the following example:

Example of sysout function definition
{
  "functions": [
    {
      "name": "printMessage",
      "type": "custom",
      "operation": "sysout"
    }
  ]
}

In the state definition, you can call the same sysout function as shown in the following example:

Example of a sysout function reference within a state
{
  "states": [
    {
      "name": "myState",
      "type": "operation",
      "actions": [
        {
          "name": "printAction",
          "functionRef": {
            "refName": "printMessage",
            "arguments": {
              "message": "."
            }
          }
        }
      ]
    }
  ]
}

You must avoid using the sysout function in a production environment since it can spam the unnecessary data in the application log.

java custom function

Kogito supports the java functions within an Apache Maven project, in which you define your workflow service.

The following example shows the declaration of a java function:

Example of a java function declaration
{
  "functions": [
    {
      "name": "myFunction", (1)
      "type": "custom", (2)
      "operation": "service:java:com.acme.MyInterfaceOrClass::myMethod" (3)
    }
  ]
}
1 myFunction is the function name
2 custom is the function type
3 service:java:com.acme.MyInterfaceOrClass::myMethod is the custom operation definition. In the custom operation definition, service is the reserved operation keyword followed by the java keyword. com.acme.MyInterfaceOrClass is the FQCN (Fully Qualified Class Name) of the interface or implementation class followed by the method name (myMethod).

Your method interface must receive a Jackson’s JsonNode object and return either void or another JsonNode as shown in the following example:

Example of a java function implementation
public class MyInterfaceOrClass {

    public void myMethod(JsonNode workflowData) {
        // do whatever I want with the JsonNode:
        // { "workflowdata": {} }
    }

    public JsonNode myMethod(JsonNode workflowData) {
        // do whatever I want with the JsonNode:
        // { "workflowdata": {} }
        // return the modified content:
        return workflowData;
    }
}

To call the function shown in the previous example within your workflow service, you can extract the required JSON value using jq expression or pass the JSON value without using arguments.

In the following example, the caller sends the entire payload:

Example of a java function reference within a state
{
  "states": [
    {
      "name": "myState",
      "type": "operation",
      "actions": [
        {
          "name": "callJavaFunctionAction",
          "functionRef": {
            "refName": "myFunction"
          }
        }
      ]
    }
  ]
}

Alternatively, you can pass the necessary data as shown in the following example:

Example of a java function reference within a state passing the necessary data
{
  "states": [
    {
      "name": "myState",
      "type": "operation",
      "actions": [
        {
          "name": "callJavaFunctionAction",
          "functionRef": {
            "refName": "myFunction",
            "arguments": {
              "data": "${ .my.expression.to.data }"
            }
          }
        }
      ]
    }
  ]
}

In the previous example, the JsonNode that the java function returns is the new payload in the workflow execution.

Avoid using java functions to call the external services, instead you can use the services orchestration features.

Found an issue?

If you find an issue or any misleading information, please feel free to report it here. We really appreciate it!