sdk-java/core
Francesco Guardiani 42a732623b
Improvements to CloudEventReader (#263)
* Extracted readAttributes and readExtensions from CloudEventReader
Added CloudEventUtils#toContextReader to create a context reader starting from a CloudEvent
Improved documentation of *Reader interfaces
Renamed MessageReader#visit to the proper name MessageReader#read

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Typo

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
2020-11-13 09:54:07 +01:00
..
src Improvements to CloudEventReader (#263) 2020-11-13 09:54:07 +01:00
README.md Bump to 2.0.0-milestone4 (#254) 2020-11-09 14:06:44 +01:00
pom.xml Bump to 2.0.0-SNAPSHOT (#255) 2020-11-09 14:13:56 +01:00

README.md

CloudEvents Core

Javadocs

The base classes, interfaces and low-level APIs to use CloudEvents.

How to Use

For Maven based projects, use the following dependency:

<dependency>
    <groupId>io.cloudevents</groupId>
    <artifactId>cloudevents-api</artifactId>
    <version>2.0.0-milestone4</version>
</dependency>

Create an Event

import io.cloudevents.CloudEvent;
import io.cloudevents.core.builder.CloudEventBuilder;
import java.net.URI;

final CloudEvent event = CloudEventBuilder.v1()
    .withId("000")
    .withType("example.demo")
    .withSource(URI.create("http://example.com"))
    .withData("application/json", "{}".getBytes())
    .build();

Materialize an Extension

CloudEvent extensions can be materialized in their respective POJOs:

import io.cloudevents.core.extensions.DistributedTracingExtension;
import io.cloudevents.core.extensions.ExtensionsParser;

DistributedTracingExtension dte = ExtensionsParser.getInstance()
    .parseExtension(DistributedTracingExtension.class, event);

Using Event Formats

The SDK implements Event Formats in submodules. To use them, you just need to add them as dependencies to your project and the SDK, through the ServiceLoader mechanism, will load them into the classpath. For example, to use the JSON event format with Jackson, add cloudevents-json-jackson as a dependency and then:

import io.cloudevents.core.format.EventFormatProvider;
import io.cloudevents.jackson.JsonFormat;

EventFormat format = EventFormatProvider
  .getInstance()
  .resolveFormat(JsonFormat.CONTENT_TYPE);

// Serialize event
byte[] serialized = format.serialize(event);

// Deserialize event
CloudEvent event = format.deserialize(bytes);