sdk-java/http/vertx
Fabio José efd60454ee Use of marshallers and unmarshallers for HTTP
Signed-off-by: Fabio José <fabiojose@gmail.com>
2019-09-16 21:43:40 -03:00
..
src Use of marshallers and unmarshallers for HTTP 2019-09-16 21:43:40 -03:00
README.md 📝 updating to 0.2.0 libraries for usage 2018-12-10 17:17:00 +01:00
pom.xml Set version to 0.3.0 2019-09-03 21:02:40 -03:00

README.md

HTTP Transport Util for Eclipse Vert.x

Receiving CloudEvents

For Maven based projects, use the following to configure the CloudEvents Vertx HTTP Transport:

<dependency>
    <groupId>io.cloudevents</groupId>
    <artifactId>http-vertx</artifactId>
    <version>0.2.0</version>
</dependency>

Below is a sample on how to use Vert.x API for RxJava 2 for reading CloudEvents from an HttpServerRequest:

import io.cloudevents.http.reactivex.vertx.VertxCloudEvents;
import io.vertx.core.http.HttpHeaders;
import io.vertx.reactivex.core.AbstractVerticle;

public class CloudEventVerticle extends AbstractVerticle {

  public void start() {

    vertx.createHttpServer()
      .requestHandler(req -> VertxCloudEvents.create().rxReadFromRequest(req)
      .subscribe((receivedEvent, throwable) -> {
        if (receivedEvent != null) {
          // I got a CloudEvent object:
          System.out.println("The event type: " + receivedEvent.getEventType())
        }
      }))
      .rxListen(8080)
      .subscribe(server -> {
        System.out.println("Server running!");
    });
  }
}

Sending CloudEvents

Below is a sample on how to use the client to send a CloudEvent:

final HttpClientRequest request = vertx.createHttpClient().post(8080, "localhost", "/");

// add a client response handler
request.handler(resp -> {
    // react on the server response
});

// write the CloudEvent to the given HTTP Post request object
VertxCloudEvents.create().writeToHttpClientRequest(cloudEvent, request);
request.end();