Knative Services are HTTP servers, that can receive input values, and their availablity and scaling is controlled by the autoscaler
of Knative Serving.
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);
}
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