This example contains a simple workflow service that illustrate error handling. The service is described using JSON format as defined in the CNCF Serverless Workflow specification.
The workflow check if the number is odd or even and print a message indicating that. The main feature of this demo is that if the number is odd, an exception is thrown, and it is the exception error handling the one that sets the odd message.
Hence, this workflow expects JSON input containing a natural number. This number is passed using a service operation to EvenService
java class. If the number is even, the workflow moves to the next defined state, injecting “even” numberType
. But if the number is odd, the class throws an IllegalArgumentException
. This exception is handled and redirected to odd inject node by using inline workflow error handling. This basically consists on adding onErrors
field, where the expected exception is specified in code
and the target state (a node injecting “odd” numberType
) in transition
. Finally, both execution paths finish on the same node, which prints the calculated eventType
.
You will need:
When using native image compilation, you will also need:
mvn clean package quarkus:dev
mvn clean package java -jar target/quarkus-app/quarkus-run.jar
or on windows
mvn clean package java -jar target\quarkus-app\quarkus-run.jar
Note that this requires GRAALVM_HOME to point to a valid GraalVM installation
mvn clean package -Pnative
To run the generated native executable, generated in target/
, execute
./target/serverless-workflow-error-quarkus-{version}-runner
The service based on the JSON workflow definition can be access by sending a request to http://localhost:8080/error' with following content
{ "workflowdata": { "number" : 2 } }
Complete curl command can be found below:
curl -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' -d '{"workflowdata" : {"number": 2}}' http://localhost:8080/error
In Quarkus you should see the log message printed:
even
If you would like to check odd number
{ "workflowdata": { "number" : 1 } }
Complete curl command can be found below:
curl -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' -d '{"workflowdata" : {"number": 1}}' http://localhost:8080/error
In Quarkus you should see the log message printed:
odd