Integrating with Camel routes
Kogito Serverless Workflow can integrate with Apache Camel Routes by adding the Kogito Quarkus Camel Add-on to your project. It enables the workflow engine to identify and call Camel routes declared in YAML or XML in the same workflow project context.
Enabling Quarkus Camel in Kogito Serverless Workflow
You can enable Quarkus Camel in your project.
-
A workflow application is created.
For more information about creating a workflow, see Creating your first workflow service.
-
To add the Quarkus Camel to your workflow application, add the
org.kie.kogito:kogito-addons-quarkus-camel
dependency to thepom.xml
file of your project as follows:Dependency to be added to thepom.xml
file to enable metrics<dependency> <groupId>org.kie.kogito</groupId> <artifactId>kogito-addons-quarkus-camel</artifactId> </dependency>
Creating Camel routes in Kogito Serverless Workflow
You can add YAML or XML Camel routes to your workflow project.
-
Create a YAML or XML Camel Routes using your IDE or the Kaoto VSCode Editor and place them in the
src/main/resources/routes
directory. -
The route
from
endpoint must be adirect
component. That’s the endpoint producer expected by the workflow engine. -
The route response must be in a valid format that the workflow context can understand:
-
A string that contains a valid JSON object
-
A valid Java bean that can be serialized to JSON
-
A Jackson’s
JsonNode
object -
Any primitive type (Integer, Float, Decimal, String, etc)
-
The response will be merged into the workflow state context. If it is an array or a complex object, the response will be added to the special attribute response
.
direct
endpoint returning a valid JSON string representation- from:
uri: direct:logRouteReplaceHeader (1)
steps: (2)
- setBody:
simple: '{ "id": "${header.WorkflowID}", "arg1": { "arg2": "value1" } }' (3)
- log:
message: We received the ${body} (4)
1 | Camel route producer definition using the direct component |
2 | Definition of the Camel route steps |
3 | Replace the Camel message body with a valid JSON object containing the header WorkflowID from the workflow context |
4 | Log the Camel message body in the console for debugging purposes. |
Defining and referencing Camel functions in the Workflow DSL
You can define and reference your Camel functions in the workflow definition.
-
You have created Camel routes in the workflow Maven project.
-
In the
functions
definition section of your workflow DSL, declare the Camel route as exemplified below:Example of a Camel Route function definition{ "functions": [ { "name": "logRoute", "type": "custom", "operation": "camel:direct:logRouteReplaceHeader" } ] }
The operation description must have the prefix
camel:direct:
, indicating that you want to produce a message to this route via the Camel Direct Component. Direct is the only component supported by Kogito Serverless Workflow at the moment.The
operation
suffix contains the name of the route endpoint. In the case of this example,logRouteReplaceHeader
.The Camel route defined in the workflow must be available in your project during runtime, otherwise, an
IllegalArgumentException
will be thrown. -
To use the Camel function definition in a workflow action, you can simply reference it as you normally would with any other Kogito Serverless Workflow function definitions. For example:
Example of a workflow state action referencing{ "states": [ { "name": "sendToLog", "type": "operation", "actions": [ { "functionRef": { "refName": "logRoute", (1) "arguments": { "body": "${ . }", (2) "headers": { "WorkflowID": "$WORKFLOW.instanceId" (3) } } } } ], "end": true } ] }
1 The function reference name, as defined in the function attribute. 2 The body definition. A jq
expression to be evaluated in runtime resulting in the body payload.3 The headers
definition that must be a key/value pair or a validjq
expression evaluated in runtime.Once a message is received back from the Camel route, the data is merged into the workflow payload:
Message payload example returned by the Camel route{ "id": "777adb97-d297-45fd-9969-efafe4dfb3e7", "arg1": { "arg2": "value1" } }
Example project
There is an example project available on GitHub using this new feature. You can use it as a reference to have a better understanding of the Camel integration with Kogito Serverless Workflow.
Found an issue?
If you find an issue or any misleading information, please feel free to report it here. We really appreciate it!