tree: 21250ec82693ed1d646d26768f63a03576476d6b [path history] [tgz]
  1. operator/
  2. src/
  3. pom.xml
  4. README.md
serverless-workflow-examples/serverless-workflow-consuming-events-over-http-quarkus/README.md

Kogito Serverless Workflow - Consuming Events Over HTTP Example

Description

This example contains a simple workflow service that consumes events over HTTP. The service is described using JSON format as defined in the CNCF Serverless Workflow specification. It's a simple workflow that, after starting, waits for an event to be published over HTTP. Then the workflow prints the event content to the console when the event is received.

To go further on using HTTP with Reactive Messaging, take a look at this article.

This is the infrastructure required to integrate with Knative Eventing.

Knative Eventing uses standard HTTP POST requests to send and receive events between event producers and sinks. These events conform to the CloudEvents specifications, which enables creating, parsing, sending, and receiving events in any programming language.

Installing and Running

Prerequisites

You will need:

When using native image compilation, you will also need:

  • GraalVm 19.3.1+ installed
  • Environment variable GRAALVM_HOME set accordingly
  • Note that GraalVM native image compilation typically requires other packages (glibc-devel, zlib-devel and gcc) to be installed too. You also need ‘native-image’ installed in GraalVM (using ‘gu install native-image’). Please refer to GraalVM installation documentation for more details.

Compile and Run in Local Dev Mode

mvn clean package quarkus:dev

Compile and Run in JVM mode

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

Compile and Run using Local Native Image

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-compensation-quarkus-{version}-runner

Submit a request

The service based on the JSON workflow definition can be started by publishing an HTTP event to ‘http://localhost:8080/startevent’ with following content:

{
  "specversion": "1.0",
  "id": "dad76364-1cf1-48ca-bf95-485a511f8707",
  "source": "",
  "type": "start",
  "kogitobusinesskey": "cloud-event-test",
  "time": "2023-01-17T15:35:29.967831-03:00",
  "data": {
    "message": "Hello!"
  }
}

Complete curl command can be found below:

curl -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' -d  '{"specversion":"1.0","id": "dad76364-1cf1-48ca-bf95-485a511f8707","source":"","type":"start","kogitobusinesskey": "cloud-event-test","time":"2023-01-17T15:35:29.967831-03:00","data":{"message":"Hello!"}}' http://localhost:8080/startevent

After executing the command, in the Quarkus console log may appear some messages printed. The first one notifying that an instance of the ‘start’ workflow has been started with a given id (notice that the id might change), for example like:

Starting workflow 'start' (4a254d46-0cbf-41f8-8e27-15b3da625561)

You may also find another one printing the message we send in the event data.

[ "Hello" ]

At this point, your workflow is waiting for an event to be published over HTTP. Send the following event: (You should use in “kogitoprocrefid” field the id returned by the previous request)

{
  "specversion":"1.0",
  "source":"",
  "type":"move",
  "time":"2023-01-17T15:35:29.967831-03:00",
  "kogitoprocrefid":"4a254d46-0cbf-41f8-8e27-15b3da625561",
  "data":{
    "move":"This has been injected by the event"
  }
}

Complete curl command can be found below:

curl -X POST -H 'Content-Type:application/json' -H 'Accept:application/json' -d '{"specversion":"1.0","id":"e4604756-f58e-440e-9619-484b92408308","source":"","type":"move","time":"2023-01-17T15:35:29.967831-03:00","kogitoprocrefid":"4a254d46-0cbf-41f8-8e27-15b3da625561","data":{"move":"This has been injected by the event"}}' http://localhost:8080/move

The workflow will consume the event and print the message you sent to the console.

[ "Hello", "This has been injected by the event" ]

Building and Deploying Workflow using CLI + Kogito Serverless Workflow Operator

For this prepare your environment by following the instructions from here.

Refer to Serverless Workflow Guide, to know how to build and deploy workflows using CLI + Kogito Serverless Workflow Operator. Refer to Serverless Workflow Guide, to know more about Kogito Serverless Workflow Operator.