File tree Expand file tree Collapse file tree 5 files changed +58
-10
lines changed
examples/restful-ws-quarkus
src/main/java/io/cloudevents/examples/quarkus Expand file tree Collapse file tree 5 files changed +58
-10
lines changed Original file line number Diff line number Diff line change 11# Cloudevents Restful WS Quarkus example
22
33This 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
711curl -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+
3360In order to also show how to create and send CloudEvents, generated events will be periodically sent
3461each 2 seconds through HTTP to the same endpoint using a REST client.
3562
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 11package io .cloudevents .examples .quarkus .client ;
22
3+ import javax .ws .rs .Consumes ;
34import javax .ws .rs .POST ;
45import javax .ws .rs .Path ;
5- import javax .ws .rs .Produces ;
6- import javax .ws .rs .core .MediaType ;
76
87import io .cloudevents .CloudEvent ;
8+ import io .cloudevents .jackson .JsonFormat ;
9+
910import org .eclipse .microprofile .rest .client .inject .RegisterRestClient ;
1011
1112@ Path ("/users" )
1213@ RegisterRestClient
1314public 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}
Original file line number Diff line number Diff line change 1919import io .cloudevents .core .data .PojoCloudEventData ;
2020import io .cloudevents .examples .quarkus .model .User ;
2121import io .quarkus .scheduler .Scheduled ;
22- import io .smallrye .mutiny .Uni ;
2322
2423@ ApplicationScoped
2524public 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 ) {
Original file line number Diff line number Diff line change 1919
2020@ Path ("/users" )
2121@ Consumes ({MediaType .APPLICATION_JSON , JsonFormat .CONTENT_TYPE })
22- @ Produces ({ MediaType .APPLICATION_JSON } )
22+ @ Produces (MediaType .APPLICATION_JSON )
2323public class UserResource {
2424
2525 private static final Logger LOGGER = LoggerFactory .getLogger (UserResource .class );
You can’t perform that action at this time.
0 commit comments