HTTP Protocol Binding for Eclipse Vert.x
For Maven based projects, use the following to configure the CloudEvents Vertx HTTP Transport:
<dependency>
<groupId>io.cloudevents</groupId>
<artifactId>cloudevents-http-vertx</artifactId>
<version>4.0.1</version>
</dependency>
Receiving CloudEvents
Assuming you have in classpath cloudevents-json-jackson
, below is a sample on how to read and write CloudEvents:
import io.cloudevents.http.vertx.VertxMessageFactory;
import io.cloudevents.core.message.StructuredMessageReader;
import io.cloudevents.CloudEvent;
import io.vertx.core.AbstractVerticle;
public class CloudEventServerVerticle extends AbstractVerticle {
public void start() {
vertx.createHttpServer()
.requestHandler(req -> {
VertxMessageFactory.createReader(req)
.onSuccess(messageReader -> {
CloudEvent event = messageReader.toEvent();
// Echo the message, as structured mode
VertxMessageFactory
.createWriter(req.response())
.writeStructured(event, "application/cloudevents+json");
})
.onFailure(t -> req.response().setStatusCode(500).end());
})
.listen(8080)
.onSuccess(server ->
System.out.println("Server started on port " + server.actualPort())
).onFailure(t -> {
System.out.println("Error starting the server");
serverResult.cause().printStackTrace();
});
}
}
Sending CloudEvents
Below is a sample on how to use the client to send and receive a CloudEvent:
import io.cloudevents.CloudEvent;
import io.cloudevents.core.builder.CloudEventBuilder;
import io.cloudevents.http.vertx.VertxMessageFactory;
import io.vertx.core.AbstractVerticle;
import io.vertx.ext.web.client.WebClient;
import java.net.URI;
public class CloudEventClientVerticle extends AbstractVerticle {
public void start() {
WebClient client = WebClient.create(vertx);
CloudEvent reqEvent = CloudEventBuilder.v1()
.withId("hello")
.withType("example.vertx")
.withSource(URI.create("http://localhost"))
.build();
VertxMessageFactory
.createWriter(client.postAbs("http://localhost:8080"))
.writeBinary(reqEvent)
.onSuccess(response -> {
CloudEvent responseEvent = VertxMessageFactory
.createReader(response)
.toEvent();
})
.onFailure(Throwable::printStackTrace);
}
}