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 1
1
# Cloudevents Restful WS Quarkus example
2
2
3
3
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 )
5
9
6
10
``` shell script
7
11
curl -v http://localhost:8080/users \
@@ -30,6 +34,29 @@ curl -v http://localhost:8080/users \
30
34
< Location: http://localhost:8080/users
31
35
```
32
36
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
+
33
60
In order to also show how to create and send CloudEvents, generated events will be periodically sent
34
61
each 2 seconds through HTTP to the same endpoint using a REST client.
35
62
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 1
1
package io .cloudevents .examples .quarkus .client ;
2
2
3
+ import javax .ws .rs .Consumes ;
3
4
import javax .ws .rs .POST ;
4
5
import javax .ws .rs .Path ;
5
- import javax .ws .rs .Produces ;
6
- import javax .ws .rs .core .MediaType ;
7
6
8
7
import io .cloudevents .CloudEvent ;
8
+ import io .cloudevents .jackson .JsonFormat ;
9
+
9
10
import org .eclipse .microprofile .rest .client .inject .RegisterRestClient ;
10
11
11
12
@ Path ("/users" )
12
13
@ RegisterRestClient
13
14
public interface UserClient {
14
15
15
16
// 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
+
17
21
@ POST
18
- @ Produces ( MediaType . APPLICATION_JSON )
19
- void emit (CloudEvent event );
22
+ @ Consumes ( JsonFormat . CONTENT_TYPE )
23
+ void emitStructured (CloudEvent event );
20
24
}
Original file line number Diff line number Diff line change 19
19
import io .cloudevents .core .data .PojoCloudEventData ;
20
20
import io .cloudevents .examples .quarkus .model .User ;
21
21
import io .quarkus .scheduler .Scheduled ;
22
- import io .smallrye .mutiny .Uni ;
23
22
24
23
@ ApplicationScoped
25
24
public class UserEventsGenerator {
@@ -38,8 +37,13 @@ public class UserEventsGenerator {
38
37
@ Scheduled (every ="2s" )
39
38
public void init () {
40
39
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
+ }
43
47
}
44
48
45
49
private CloudEvent createEvent (long id ) {
Original file line number Diff line number Diff line change 19
19
20
20
@ Path ("/users" )
21
21
@ Consumes ({MediaType .APPLICATION_JSON , JsonFormat .CONTENT_TYPE })
22
- @ Produces ({ MediaType .APPLICATION_JSON } )
22
+ @ Produces (MediaType .APPLICATION_JSON )
23
23
public class UserResource {
24
24
25
25
private static final Logger LOGGER = LoggerFactory .getLogger (UserResource .class );
You can’t perform that action at this time.
0 commit comments