Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
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