Skip to content

Commit 1591cb3

Browse files
authored
[#465] Upgrade Quarkus examples with binary and structured events (#515)
Signed-off-by: ruromero <[email protected]>
1 parent f71303b commit 1591cb3

File tree

5 files changed

+58
-10
lines changed

5 files changed

+58
-10
lines changed

examples/restful-ws-quarkus/README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Cloudevents Restful WS Quarkus example
22

33
This sample application has a `/users` REST endpoint in which you can manage the different users.
4-
The way to create users is through CloudEvents. Here is an example POST:
4+
The way to create users is through CloudEvents.
5+
6+
## Example requests
7+
8+
### [Binary Content mode](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/bindings/http-protocol-binding.md#31-binary-content-mode)
59

610
```shell script
711
curl -v http://localhost:8080/users \
@@ -30,6 +34,29 @@ curl -v http://localhost:8080/users \
3034
< Location: http://localhost:8080/users
3135
```
3236

37+
### [Structured Content mode](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/bindings/http-protocol-binding.md#32-structured-content-mode)
38+
39+
```shell script
40+
curl -v http://localhost:8080/users \
41+
-H "Content-Type: application/cloudevents+json" \
42+
-d @examples/user_structured.json
43+
44+
> POST /users HTTP/1.1
45+
> Host: localhost:8080
46+
> User-Agent: curl/7.82.0
47+
> Accept: */*
48+
> Content-Type: application/cloudevents+json
49+
> Content-Length: 290
50+
>
51+
52+
< HTTP/1.1 201 Created
53+
< Location: http://localhost:8081/users
54+
< content-length: 0
55+
<
56+
```
57+
58+
### Generated events
59+
3360
In order to also show how to create and send CloudEvents, generated events will be periodically sent
3461
each 2 seconds through HTTP to the same endpoint using a REST client.
3562

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"specversion" : "1.0",
3+
"type" : "User",
4+
"source": "io.cloudevents.examples/user",
5+
"id": "536808d3-88be-4077-9d7a-a3f162705f78",
6+
"subject": "SUBJ-0001",
7+
"data" : {
8+
"username": "jsmith",
9+
"firstName": "John",
10+
"lastName": "Smith",
11+
"age": 37
12+
}
13+
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package io.cloudevents.examples.quarkus.client;
22

3+
import javax.ws.rs.Consumes;
34
import javax.ws.rs.POST;
45
import javax.ws.rs.Path;
5-
import javax.ws.rs.Produces;
6-
import javax.ws.rs.core.MediaType;
76

87
import io.cloudevents.CloudEvent;
8+
import io.cloudevents.jackson.JsonFormat;
9+
910
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
1011

1112
@Path("/users")
1213
@RegisterRestClient
1314
public interface UserClient {
1415

1516
// This will emit binary encoded events.
16-
// To use structured JSON encoding use @Produces(JsonFormat.CONTENT_TYPE).
17+
// To use structured JSON encoding use @Consumes(JsonFormat.CONTENT_TYPE).
18+
@POST
19+
void emitBinary(CloudEvent event);
20+
1721
@POST
18-
@Produces(MediaType.APPLICATION_JSON)
19-
void emit(CloudEvent event);
22+
@Consumes(JsonFormat.CONTENT_TYPE)
23+
void emitStructured(CloudEvent event);
2024
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import io.cloudevents.core.data.PojoCloudEventData;
2020
import io.cloudevents.examples.quarkus.model.User;
2121
import io.quarkus.scheduler.Scheduled;
22-
import io.smallrye.mutiny.Uni;
2322

2423
@ApplicationScoped
2524
public class UserEventsGenerator {
@@ -38,8 +37,13 @@ public class UserEventsGenerator {
3837
@Scheduled(every="2s")
3938
public void init() {
4039
CloudEvent event = createEvent(userCount++);
41-
LOGGER.info("try to emit user: {}", event.getId());
42-
userClient.emit(event);
40+
if(userCount % 2 == 0) {
41+
LOGGER.info("try to emit binary event for user: {}", event.getId());
42+
userClient.emitBinary(event);
43+
} else {
44+
LOGGER.info("try to emit structured event for user: {}", event.getId());
45+
userClient.emitStructured(event);
46+
}
4347
}
4448

4549
private CloudEvent createEvent(long id) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
@Path("/users")
2121
@Consumes({MediaType.APPLICATION_JSON, JsonFormat.CONTENT_TYPE})
22-
@Produces({MediaType.APPLICATION_JSON})
22+
@Produces(MediaType.APPLICATION_JSON)
2323
public class UserResource {
2424

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

0 commit comments

Comments
 (0)