Skip to content

Commit

Permalink
[cloudevents#465] Upgrade Quarkus examples with binary and structured…
Browse files Browse the repository at this point in the history
… events
  • Loading branch information
ruromero committed Jan 24, 2023
1 parent f71303b commit 3d38ec3
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 16 deletions.
29 changes: 28 additions & 1 deletion examples/restful-ws-quarkus/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Cloudevents Restful WS Quarkus example

This sample application has a `/users` REST endpoint in which you can manage the different users.
The way to create users is through CloudEvents. Here is an example POST:
The way to create users is through CloudEvents.

## Example requests

### [Binary Content mode](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/bindings/http-protocol-binding.md#31-binary-content-mode)

```shell script
curl -v http://localhost:8080/users \
Expand Down Expand Up @@ -30,6 +34,29 @@ curl -v http://localhost:8080/users \
< Location: http://localhost:8080/users
```

### [Structured Content mode](https://github.com/cloudevents/spec/blob/v1.0.2/cloudevents/bindings/http-protocol-binding.md#32-structured-content-mode)

```shell script
curl -v http://localhost:8080/users \
-H "Content-Type: application/cloudevents+json" \
-d @examples/user_structured.json

> POST /users HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.82.0
> Accept: */*
> Content-Type: application/cloudevents+json
> Content-Length: 290
>

< HTTP/1.1 201 Created
< Location: http://localhost:8081/users
< content-length: 0
<
```

### Generated events

In order to also show how to create and send CloudEvents, generated events will be periodically sent
each 2 seconds through HTTP to the same endpoint using a REST client.

Expand Down
13 changes: 13 additions & 0 deletions examples/restful-ws-quarkus/examples/user_structured.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"specversion" : "1.0",
"type" : "User",
"source": "io.cloudevents.examples/user",
"id": "536808d3-88be-4077-9d7a-a3f162705f78",
"subject": "SUBJ-0001",
"data" : {
"username": "jsmith",
"firstName": "John",
"lastName": "Smith",
"age": 37
}
}
10 changes: 4 additions & 6 deletions examples/restful-ws-quarkus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>cloudevents-restful-ws-quarkus-example</artifactId>
<properties>
<quarkus-plugin.version>1.10.3.Final</quarkus-plugin.version>
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>1.10.3.Final</quarkus.platform.version>
<quarkus.platform.version>2.15.3.Final</quarkus.platform.version>
<quarkus-plugin.version>${quarkus.platform.version}</quarkus-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>${quarkus.platform.group-id}</groupId>
<artifactId>${quarkus.platform.artifact-id}</artifactId>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bom</artifactId>
<version>${quarkus.platform.version}</version>
<type>pom</type>
<scope>import</scope>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package io.cloudevents.examples.quarkus.client;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import io.cloudevents.CloudEvent;
import io.cloudevents.jackson.JsonFormat;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@Path("/users")
@RegisterRestClient
public interface UserClient {

// This will emit binary encoded events.
// To use structured JSON encoding use @Produces(JsonFormat.CONTENT_TYPE).
// To use structured JSON encoding use @Consumes(JsonFormat.CONTENT_TYPE).
@POST
void emitBinary(CloudEvent event);

@POST
@Produces(MediaType.APPLICATION_JSON)
void emit(CloudEvent event);
@Consumes(JsonFormat.CONTENT_TYPE)
void emitStructured(CloudEvent event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.cloudevents.core.data.PojoCloudEventData;
import io.cloudevents.examples.quarkus.model.User;
import io.quarkus.scheduler.Scheduled;
import io.smallrye.mutiny.Uni;

@ApplicationScoped
public class UserEventsGenerator {
Expand All @@ -38,8 +37,13 @@ public class UserEventsGenerator {
@Scheduled(every="2s")
public void init() {
CloudEvent event = createEvent(userCount++);
LOGGER.info("try to emit user: {}", event.getId());
userClient.emit(event);
if(userCount % 2 == 0) {
LOGGER.info("try to emit binary event for user: {}", event.getId());
userClient.emitBinary(event);
} else {
LOGGER.info("try to emit structured event for user: {}", event.getId());
userClient.emitStructured(event);
}
}

private CloudEvent createEvent(long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

@Path("/users")
@Consumes({MediaType.APPLICATION_JSON, JsonFormat.CONTENT_TYPE})
@Produces({MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public class UserResource {

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

0 comments on commit 3d38ec3

Please sign in to comment.