... | ... | @@ -6,7 +6,7 @@ to have Java clients to receive those events. |
|
|
Interface:
|
|
|
|
|
|
public interface SseInterface {
|
|
|
@SSE(autoReconnect=20)
|
|
|
@SSE
|
|
|
EventStream<TestPojo> status(String jobName);
|
|
|
}
|
|
|
|
... | ... | @@ -21,9 +21,15 @@ Implementation: |
|
|
@Override
|
|
|
public EventStream<TestPojo> status(String jobName) {
|
|
|
return (EventStream<TestPojo> stream) -> {
|
|
|
try {
|
|
|
//compute values, loop or wait
|
|
|
//don't forget to check Thread.interrupted() when looping
|
|
|
|
|
|
//the stream may have been closed by the client
|
|
|
if (stream.isClosed()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
TestPojo event = new TestPojo(...);
|
|
|
stream.pushEvent(event);
|
|
|
|
... | ... | @@ -37,9 +43,10 @@ Implementation: |
|
|
|
|
|
* The implementation returns a `Consumer<EventStream<Payload>>`. This will automatically converted by Java into an `EventStream<Payload>` using FunctionalInterface mechanics.
|
|
|
* The provided stream is the transport for the generated events. _Just push your events into the stream_.
|
|
|
* The consumer may be a long running task. It should check for the interrupted flag, tough
|
|
|
* The consumer may be a long running task. It should check for the `Thread.interrupted()` and `stream.isClosed()` flags, tough.
|
|
|
* When the consumer finishes the connection from the EvenSource is being closed and (possibly) will be re-established after some time.
|
|
|
|
|
|
Client:
|
|
|
Client (asynchronous):
|
|
|
|
|
|
...
|
|
|
DelightConfig cfg = DelightConfigFinder.getDefaultConfigBuilder()
|
... | ... | @@ -53,7 +60,7 @@ Client: |
|
|
.usingApi(SseInterface.class);
|
|
|
|
|
|
try (EventStream<TestPojo> events = sseService.status("send me events ...")) {
|
|
|
events.register(eventPayload -> {
|
|
|
events.register(eventPayload -> { //elements of type TestPojo
|
|
|
System.out.printf("(SseClient)Received: '%s'\n", eventPayload);
|
|
|
},
|
|
|
ex -> { //error handler
|
... | ... | |