Skip to content

Commit d2a89a4

Browse files
Examples readme (#298)
* Added readme Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Improved the quarkus example Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Added amqp proton Fixed and added stuff to the root readme Added link to examples readme to the main readme Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
1 parent de78632 commit d2a89a4

File tree

4 files changed

+104
-55
lines changed

4 files changed

+104
-55
lines changed

README.md

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,95 @@
44
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.cloudevents/cloudevents-parent/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.cloudevents/cloudevents-parent)
55
[![Javadocs](http://www.javadoc.io/badge/io.cloudevents/cloudevents-core.svg?color=green)](http://www.javadoc.io/doc/io.cloudevents/cloudevents-core)
66

7-
A Java API for the [CloudEvents specification](https://github.com/cloudevents/spec)
7+
A Java API for the
8+
[CloudEvents specification](https://github.com/cloudevents/spec)
89

910
Look at https://cloudevents.github.io/sdk-java/ for more documentation.
1011

1112
## Status
1213

13-
This SDK is considered **work in progress**. The community is working hard to bring you a new major version of the SDK with major enhancements both to APIs and to implementation.
14+
This SDK is considered **work in progress**. The community is working hard to
15+
bring you a new major version of the SDK with major enhancements both to APIs
16+
and to implementation.
1417

15-
If you want to know more about v1 of this SDK, check out the [v1 readme](https://github.com/cloudevents/sdk-java/tree/1.x)
18+
If you want to know more about v1 of this SDK, check out the
19+
[v1 readme](https://github.com/cloudevents/sdk-java/tree/1.x)
1620

1721
Stay tuned!
1822

1923
Supported features of the specification:
2024

21-
| | [v0.3](https://github.com/cloudevents/spec/tree/v0.3) | [v1.0](https://github.com/cloudevents/spec/tree/v1.0) |
22-
| :---------------------------: | :----------------------------------------------------------------------------: | :---------------------------------------------------------------------------------: |
23-
| CloudEvents Core | :heavy_check_mark: | :heavy_check_mark: |
24-
| AMQP Protocol Binding | :x: | :x: |
25-
| AVRO Event Format | :x: | :x: |
26-
| HTTP Protocol Binding | :heavy_check_mark: | :heavy_check_mark: |
27-
| - [Vert.x](http/vertx) | :heavy_check_mark: | :heavy_check_mark: |
28-
| - [Jakarta Restful WS](http/restful-ws) | :heavy_check_mark: | :heavy_check_mark: |
29-
| JSON Event Format | :heavy_check_mark: | :heavy_check_mark: |
30-
| - [Jackson](formats/json-jackson) | :heavy_check_mark: | :heavy_check_mark: |
31-
| [Kafka Protocol Binding](kafka) | :heavy_check_mark: | :heavy_check_mark: |
32-
| MQTT Protocol Binding | :x: | :x: |
33-
| NATS Protocol Binding | :x: | :x: |
34-
| Web hook | :x: | :x: |
25+
| | [v0.3](https://github.com/cloudevents/spec/tree/v0.3) | [v1.0](https://github.com/cloudevents/spec/tree/v1.0) |
26+
| :-------------------------------------: | :---------------------------------------------------: | :---------------------------------------------------: |
27+
| CloudEvents Core | :heavy_check_mark: | :heavy_check_mark: |
28+
| AMQP Protocol Binding | :x: | :x: |
29+
| - [Proton](amqp) | :heavy_check_mark: | :heavy_check_mark: |
30+
| AVRO Event Format | :x: | :x: |
31+
| HTTP Protocol Binding | :heavy_check_mark: | :heavy_check_mark: |
32+
| - [Vert.x](http/vertx) | :heavy_check_mark: | :heavy_check_mark: |
33+
| - [Jakarta Restful WS](http/restful-ws) | :heavy_check_mark: | :heavy_check_mark: |
34+
| - [Basic](http/basic) | :heavy_check_mark: | :heavy_check_mark: |
35+
| JSON Event Format | :heavy_check_mark: | :heavy_check_mark: |
36+
| - [Jackson](formats/json-jackson) | :heavy_check_mark: | :heavy_check_mark: |
37+
| [Kafka Protocol Binding](kafka) | :heavy_check_mark: | :heavy_check_mark: |
38+
| MQTT Protocol Binding | :x: | :x: |
39+
| NATS Protocol Binding | :x: | :x: |
40+
| Web hook | :x: | :x: |
3541

3642
## Motivation
3743

38-
The [CloudEvents specification](https://github.com/cloudevents/spec) is a vendor-neutral specification for defining the format of event data that is being exchanged between different cloud systems. The specification basically defines an abstract envelope for any event data payload, without knowing specific implementation details of the actual underlying event.
39-
The current version of the spec is at `1.0` and it describes a simple event format, which was demonstrated at [KubeCon 2018](https://youtu.be/TZPPjAv12KU) using different _Serverless platforms_, such as [Apache Openwhisk](https://github.com/apache/incubator-openwhisk).
44+
The [CloudEvents specification](https://github.com/cloudevents/spec) is a
45+
vendor-neutral specification for defining the format of event data that is being
46+
exchanged between different cloud systems. The specification basically defines
47+
an abstract envelope for any event data payload, without knowing specific
48+
implementation details of the actual underlying event. The current version of
49+
the spec is at `1.0` and it describes a simple event format, which was
50+
demonstrated at [KubeCon 2018](https://youtu.be/TZPPjAv12KU) using different
51+
_Serverless platforms_, such as
52+
[Apache Openwhisk](https://github.com/apache/incubator-openwhisk).
4053

4154
## Documentation
4255

4356
Documentation is available at https://cloudevents.github.io/sdk-java/
4457

4558
Javadocs are available on [javadoc.io](https://www.javadoc.io):
4659

47-
* [cloudevents-api](https://www.javadoc.io/doc/io.cloudevents/cloudevents-api)
48-
* [cloudevents-core](https://www.javadoc.io/doc/io.cloudevents/cloudevents-core)
49-
* [cloudevents-json-jackson](https://www.javadoc.io/doc/io.cloudevents/cloudevents-json-jackson)
50-
* [cloudevents-http-basic](https://www.javadoc.io/doc/io.cloudevents/cloudevents-http-basic)
51-
* [cloudevents-http-restful-ws](https://www.javadoc.io/doc/io.cloudevents/cloudevents-http-restful-ws)
52-
* [cloudevents-http-vertx](https://www.javadoc.io/doc/io.cloudevents/cloudevents-http-vertx)
53-
* [cloudevents-kafka](https://www.javadoc.io/doc/io.cloudevents/cloudevents-kafka)
60+
- [cloudevents-api](https://www.javadoc.io/doc/io.cloudevents/cloudevents-api)
61+
- [cloudevents-core](https://www.javadoc.io/doc/io.cloudevents/cloudevents-core)
62+
- [cloudevents-json-jackson](https://www.javadoc.io/doc/io.cloudevents/cloudevents-json-jackson)
63+
- [cloudevents-http-basic](https://www.javadoc.io/doc/io.cloudevents/cloudevents-http-basic)
64+
- [cloudevents-http-restful-ws](https://www.javadoc.io/doc/io.cloudevents/cloudevents-http-restful-ws)
65+
- [cloudevents-http-vertx](https://www.javadoc.io/doc/io.cloudevents/cloudevents-http-vertx)
66+
- [cloudevents-kafka](https://www.javadoc.io/doc/io.cloudevents/cloudevents-kafka)
67+
68+
You can check out the examples in the [examples](examples) directory.
5469

5570
## Used By
5671

57-
| [Occurrent](https://occurrent.org) | [Knative Eventing](https://github.com/knative-sandbox/eventing-kafka-broker) |
58-
|-------------|-------------|
72+
| [Occurrent](https://occurrent.org) | [Knative Eventing](https://github.com/knative-sandbox/eventing-kafka-broker) |
73+
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
5974
| <a href="https://occurrent.org"><img src="https://raw.githubusercontent.com/johanhaleby/occurrent/master/occurrent-logo-196x196.png" width="98" height="98" alt="Occurrent" title="Occurrent - Event Sourcing Utilities for the JVM"></img></a> | <a href="https://github.com/knative-sandbox/eventing-kafka-broker"><img src="https://cloudevents.io/img/logos/integrations/knative.png" height="98"></img></a> |
6075

6176
## Community
6277

63-
- There are bi-weekly calls immediately following the [Serverless/CloudEvents
64-
call](https://github.com/cloudevents/spec#meeting-time) at
65-
9am PT (US Pacific). Which means they will typically start at 10am PT, but
66-
if the other call ends early then the SDK call will start early as well.
67-
See the [CloudEvents meeting minutes](https://docs.google.com/document/d/1OVF68rpuPK5shIHILK9JOqlZBbfe91RNzQ7u_P7YCDE/edit#)
68-
to determine which week will have the call.
69-
- Slack: #cloudeventssdk channel under
70-
[CNCF's Slack workspace](https://slack.cncf.io/).
71-
- Email: https://lists.cncf.io/g/cncf-cloudevents-sdk
72-
- Contact for additional information: Francesco Guardiani (`@slinkydeveloper` on slack), Fabio José (`@fabiojose` on slack).
78+
- There are bi-weekly calls immediately following the
79+
[Serverless/CloudEvents call](https://github.com/cloudevents/spec#meeting-time)
80+
at 9am PT (US Pacific). Which means they will typically start at 10am PT,
81+
but if the other call ends early then the SDK call will start early as well.
82+
See the
83+
[CloudEvents meeting minutes](https://docs.google.com/document/d/1OVF68rpuPK5shIHILK9JOqlZBbfe91RNzQ7u_P7YCDE/edit#)
84+
to determine which week will have the call.
85+
- Slack: #cloudeventssdk channel under
86+
[CNCF's Slack workspace](https://slack.cncf.io/).
87+
- Email: https://lists.cncf.io/g/cncf-cloudevents-sdk
88+
- Contact for additional information: Francesco Guardiani (`@slinkydeveloper`
89+
on slack), Fabio José (`@fabiojose` on slack).
7390

7491
Each SDK may have its own unique processes, tooling and guidelines, common
7592
governance related material can be found in the
7693
[CloudEvents `community`](https://github.com/cloudevents/spec/tree/master/community)
77-
directory. In particular, in there you will find information concerning
78-
how SDK projects are
94+
directory. In particular, in there you will find information concerning how SDK
95+
projects are
7996
[managed](https://github.com/cloudevents/spec/blob/master/community/SDK-GOVERNANCE.md),
8097
[guidelines](https://github.com/cloudevents/spec/blob/master/community/SDK-maintainer-guidelines.md)
8198
for how PR reviews and approval, and our

examples/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# CloudEvents sdk-java examples
2+
3+
This directory includes some examples on how to use CloudEvents sdk-java:
4+
5+
- [amqp-proton](amqp-proton) shows how to use the module
6+
`cloudevents-amqp-proton` to send and receive CloudEvents using AMQP 1.0.
7+
- [kafka](kafka) shows how to use the module `cloudevents-kafka` to produce
8+
and consume CloudEvents on Kafka topics.
9+
- [restful-ws-quarkus](restful-ws-quarkus) shows how to use the module
10+
`cloudevents-http-restful-ws` with Quarkus to receive and send CloudEvents
11+
through HTTP.
12+
- [restful-ws-spring-boot](restful-ws-spring-boot) shows how to use the module
13+
`cloudevents-http-restful-ws` with Spring Boot and Jersey to receive and
14+
send CloudEvents through HTTP.
15+
- [vertx](vertx) shows how to use the module `cloudevents-http-vertx` to
16+
receive and send CloudEvents through HTTP using `vertx-web-client` and
17+
`vertx-core`.
18+
- [basic-http](basic-http) shows how to use the module
19+
`cloudevents-http-basic` to send and receive CloudEvents using `JDK`'s
20+
`HttpServer`, `HttpURLConnection` and Jetty.

examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/client/UserEventsGenerator.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
package io.cloudevents.examples.quarkus.client;
22

3-
import java.net.URI;
4-
import java.time.Duration;
5-
import java.util.UUID;
6-
import javax.enterprise.context.ApplicationScoped;
7-
import javax.enterprise.event.Observes;
8-
import javax.inject.Inject;
9-
import javax.ws.rs.core.MediaType;
10-
3+
import com.fasterxml.jackson.databind.ObjectMapper;
114
import io.cloudevents.CloudEvent;
5+
import io.cloudevents.CloudEventData;
126
import io.cloudevents.core.builder.CloudEventBuilder;
7+
import io.cloudevents.core.data.PojoCloudEventData;
138
import io.cloudevents.examples.quarkus.model.User;
149
import io.quarkus.runtime.StartupEvent;
1510
import io.smallrye.mutiny.Multi;
16-
import io.vertx.core.json.Json;
1711
import org.eclipse.microprofile.rest.client.inject.RestClient;
1812
import org.slf4j.Logger;
1913
import org.slf4j.LoggerFactory;
2014

15+
import javax.enterprise.context.ApplicationScoped;
16+
import javax.enterprise.event.Observes;
17+
import javax.inject.Inject;
18+
import javax.ws.rs.core.MediaType;
19+
import java.net.URI;
20+
import java.time.Duration;
21+
import java.util.UUID;
22+
2123
@ApplicationScoped
2224
public class UserEventsGenerator {
2325

2426
private static final Logger LOGGER = LoggerFactory.getLogger(UserEventsGenerator.class);
2527

28+
@Inject
29+
ObjectMapper mapper;
30+
2631
@Inject
2732
@RestClient
2833
UserClient userClient;
@@ -44,16 +49,16 @@ private CloudEvent createEvent(long id) {
4449
.withType("io.cloudevents.examples.quarkus.user")
4550
.withId(UUID.randomUUID().toString())
4651
.withDataContentType(MediaType.APPLICATION_JSON)
47-
.withData(createUserAsByteArray(id))
52+
.withData(createUser(id))
4853
.build();
4954
}
5055

51-
private byte[] createUserAsByteArray(Long id) {
56+
private CloudEventData createUser(Long id) {
5257
User user = new User()
5358
.setAge(id.intValue())
5459
.setUsername("user" + id)
5560
.setFirstName("firstName" + id)
5661
.setLastName("lastName" + id);
57-
return Json.encode(user).getBytes();
62+
return PojoCloudEventData.wrap(user, mapper::writeValueAsBytes);
5863
}
5964
}

examples/restful-ws-quarkus/src/main/java/io/cloudevents/examples/quarkus/resources/UserResource.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package io.cloudevents.examples.quarkus.resources;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import io.cloudevents.CloudEvent;
45
import io.cloudevents.examples.quarkus.model.User;
5-
import io.vertx.core.buffer.Buffer;
6-
import io.vertx.core.json.Json;
6+
import io.cloudevents.jackson.PojoCloudEventDataMapper;
77
import org.slf4j.Logger;
88
import org.slf4j.LoggerFactory;
99

10+
import javax.inject.Inject;
1011
import javax.ws.rs.*;
1112
import javax.ws.rs.core.Context;
1213
import javax.ws.rs.core.MediaType;
@@ -22,6 +23,9 @@ public class UserResource {
2223

2324
private static final Logger LOGGER = LoggerFactory.getLogger(UserResource.class);
2425

26+
@Inject
27+
ObjectMapper mapper;
28+
2529
@Context
2630
UriInfo uriInfo;
2731

@@ -46,7 +50,10 @@ public Response create(CloudEvent event) {
4650
if (event == null || event.getData() == null) {
4751
throw new BadRequestException("Invalid data received. Null or empty event");
4852
}
49-
User user = Json.decodeValue(Buffer.buffer(event.getData().toBytes()), User.class);
53+
User user = PojoCloudEventDataMapper
54+
.from(mapper, User.class)
55+
.map(event.getData())
56+
.getValue();
5057
if (users.containsKey(user.getUsername())) {
5158
throw new BadRequestException("Username already exists: " + user.getUsername());
5259
}

0 commit comments

Comments
 (0)