Skip to content

Instantly share code, notes, and snippets.

@matzew
Created February 1, 2019 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matzew/85efffeebd10a94c815e60c38cb29dbc to your computer and use it in GitHub Desktop.
Save matzew/85efffeebd10a94c815e60c38cb29dbc to your computer and use it in GitHub Desktop.

Reactive Messaging as Knative Reply Services

Knative Services are HTTP servers, that can receive input values, and their availablity and scaling is controlled by the autoscaler of Knative Serving.

Receiving CloudEvents

In Eventing such a HTTP application, can be subscribed to a channel, to receive message from that channel, as CloudEvents, over http. The yaml for that would look like:

apiVersion: eventing.knative.dev/v1alpha1
kind: Subscription
metadata:
  name: my-subscription
  namespace: myproject
spec:
  channel:
    apiVersion: eventing.knative.dev/v1alpha1
    kind: Channel
    name: datachannel
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1alpha1
      kind: Service
      name: data-sink

The code for the data-sink Service can be very simple, using SmallRye:

@ApplicationScoped
public class MyCloudEventProcessor {

  @Incoming("from-http")
  public CompletionStage<Void> receive(Message m) {
    System.out.println("da -> " + m);
    return CompletableFuture.completedFuture(null);
  }

Processing CloudEvents

While it's very nice and handy to have a HTTP-server with a little bit of code and the @Incoming annotation, it would be nice if that HTTP server also could return a response.

This would be useful to implement some smarter data processors, that would work on the imcoming data, transform it and return that value (e.g. Transformer EIP).

In Knative this return value can be configure to be sent to a different channel, so that others can subscribe to that transformed or new event. Here is how the yaml would look for that:

apiVersion: eventing.knative.dev/v1alpha1
kind: Subscription
metadata:
  name: my-subscription
  namespace: myproject
spec:
  channel:
    apiVersion: eventing.knative.dev/v1alpha1
    kind: Channel
    name: datachannel
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1alpha1
      kind: Service
      name: data-processor
  reply:
    channel:
      apiVersion: eventing.knative.dev/v1alpha1
      kind: Channel
      name: replychannel

Knative's Controller (behind the scenes) goes ahead and routes the response from the data-processor to the referenced reply Channel.

Now, one can go ahead and register a different Knative Service (e.g. implemented with MP Reactive), to receive events from that replychannel:

apiVersion: eventing.knative.dev/v1alpha1
kind: Subscription
metadata:
  name: reply-subscription
  namespace: myproject
spec:
  channel:
    apiVersion: eventing.knative.dev/v1alpha1
    kind: Channel
    name: replychannel
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1alpha1
      kind: Service
      name: data-sink
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment