-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
...d-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/SseEventUpdateResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.locks.LockSupport; | ||
|
||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.client.ClientBuilder; | ||
import jakarta.ws.rs.client.WebTarget; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.sse.OutboundSseEvent; | ||
import jakarta.ws.rs.sse.Sse; | ||
import jakarta.ws.rs.sse.SseEventSink; | ||
import jakarta.ws.rs.sse.SseEventSource; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
|
||
@Path("sse") | ||
public class SseEventUpdateResource { | ||
public static final String DATA_VALUE = "random data value"; | ||
@Context | ||
Sse sse; | ||
|
||
@GET | ||
@Path("client-update") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public Response clientUpdate() { | ||
String host = ConfigProvider.getConfig().getValue("quarkus.http.host", String.class); | ||
int port = ConfigProvider.getConfig().getValue("quarkus.http.port", Integer.class); | ||
StringBuilder eventCapture = new StringBuilder(); | ||
|
||
WebTarget target = ClientBuilder.newClient().target("http://" + host + ":" + port + "/api/sse/server-update"); | ||
SseEventSource eventSource = SseEventSource.target(target).build(); | ||
eventSource.register(ev -> { | ||
eventCapture.append("event: name=").append(ev.getName()).append(" data={").append(ev.readData()) | ||
.append("} and is empty: ").append(ev.isEmpty()).append("\n"); | ||
}, thr -> { | ||
eventCapture.append("Error: ").append(thr.getMessage()).append("\n") | ||
.append(Arrays.toString(thr.getStackTrace())); | ||
}); | ||
|
||
eventSource.open(); | ||
LockSupport.parkNanos(5_000_000_000L); | ||
eventSource.close(); | ||
|
||
return Response.ok(eventCapture).build(); | ||
} | ||
|
||
@GET | ||
@Path("server-update") | ||
@Produces(MediaType.SERVER_SENT_EVENTS) | ||
public void updates(@Context SseEventSink eventSink) { | ||
eventSink.send(createEvent("NON EMPTY", DATA_VALUE)); | ||
LockSupport.parkNanos(1_000_000_000L); | ||
eventSink.send(createEvent("EMPTY", "")); | ||
} | ||
|
||
private OutboundSseEvent createEvent(String name, String data) { | ||
return sse.newEventBuilder() | ||
.name(name) | ||
.data(data) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters