Signed-off-by: GitHub <noreply@github.com> Co-authored-by: slinkydeveloper <slinkydeveloper@users.noreply.github.com> |
||
|---|---|---|
| .. | ||
| src | ||
| README.md | ||
| pom.xml | ||
README.md
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>2.0.0.RC1</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)
.onComplete(result -> {
// If decoding succeeded, we should write the event back
if (result.succeeded()) {
CloudEvent event = result.result().toEvent();
// Echo the message, as structured mode
VertxMessageFactory
.createWriter(req.response())
.writeStructured(event, "application/cloudevents+json");
}
req.response().setStatusCode(500).end();
});
})
.listen(8080, serverResult -> {
if (serverResult.succeeded()) {
System.out.println("Server started on port " + serverResult.result().actualPort());
} else {
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(res -> {
CloudEvent resEvent = VertxMessageFactory
.createReader(res)
.toEvent();
})
.onFailure(Throwable::printStackTrace);
}
}