-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New simple SSE test that shows how to use the API in Helidon SE (#98)
* New simple SSE test that shows how to use the API in Helidon SE and also shows how to interact with an SSE endpoint using WebClient. * - Add a simple UI to the WebServer SSE example. * Gracefully handles client connection close before count is reached. Signed-off-by: Santiago Pericas-Geertsen <[email protected]> Co-authored-by: Romain Grecourt <[email protected]>
- Loading branch information
1 parent
0e76cfa
commit 6a54406
Showing
9 changed files
with
503 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Helidon SE SSE Example | ||
|
||
This example demonstrates how to use Server Sent Events (SSE) with both the WebServer and WebClient APIs. | ||
|
||
This project implements a couple of SSE endpoints that send either text or JSON messages. | ||
The unit test uses the WebClient API to test the endpoints. | ||
|
||
## Build, run and test | ||
|
||
Build and start the server: | ||
```shell | ||
mvn package | ||
java -jar target/helidon-examples-webserver-sse.jar | ||
``` | ||
|
||
Then open http://localhost:8080 in your browser. |
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,96 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2024 Oracle and/or its affiliates. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.helidon.applications</groupId> | ||
<artifactId>helidon-se</artifactId> | ||
<version>4.2.0-SNAPSHOT</version> | ||
<relativePath/> | ||
</parent> | ||
<groupId>io.helidon.examples.webserver</groupId> | ||
<artifactId>helidon-examples-webserver-sse</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<name>Helidon Examples WebServer SSE</name> | ||
|
||
<properties> | ||
<mainClass>io.helidon.examples.webserver.sse.Main</mainClass> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.helidon.webserver</groupId> | ||
<artifactId>helidon-webserver</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.webserver</groupId> | ||
<artifactId>helidon-webserver-sse</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.webserver</groupId> | ||
<artifactId>helidon-webserver-static-content</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>jakarta.json</groupId> | ||
<artifactId>jakarta.json-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.http.media</groupId> | ||
<artifactId>helidon-http-media-jsonp</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.logging</groupId> | ||
<artifactId>helidon-logging-jul</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.webclient</groupId> | ||
<artifactId>helidon-webclient-sse</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.webserver.testing.junit5</groupId> | ||
<artifactId>helidon-webserver-testing-junit5</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-all</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-dependency-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>copy-libs</id> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
60 changes: 60 additions & 0 deletions
60
examples/webserver/sse/src/main/java/io/helidon/examples/webserver/sse/Main.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,60 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.examples.webserver.sse; | ||
|
||
import io.helidon.logging.common.LogConfig; | ||
import io.helidon.webserver.WebServer; | ||
import io.helidon.webserver.http.HttpRules; | ||
import io.helidon.webserver.staticcontent.StaticContentService; | ||
|
||
/** | ||
* This application provides a simple service with a UI to exercise Server Sent Events (SSE). | ||
*/ | ||
class Main { | ||
|
||
private Main() { | ||
} | ||
|
||
/** | ||
* Executes the example. | ||
* | ||
* @param args command line arguments, ignored | ||
*/ | ||
public static void main(String[] args) { | ||
// load logging configuration | ||
LogConfig.configureRuntime(); | ||
|
||
WebServer server = WebServer.builder() | ||
.routing(Main::routing) | ||
.port(8080) | ||
.build() | ||
.start(); | ||
|
||
System.out.println("WEB server is up! http://localhost:" + server.port()); | ||
} | ||
|
||
/** | ||
* Updates the routing rules. | ||
* | ||
* @param rules routing rules | ||
*/ | ||
static void routing(HttpRules rules) { | ||
rules.register("/", StaticContentService.builder("WEB") | ||
.welcomeFileName("index.html") | ||
.build()) | ||
.register("/api", new SseService()); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
examples/webserver/sse/src/main/java/io/helidon/examples/webserver/sse/SseService.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,88 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.helidon.examples.webserver.sse; | ||
|
||
import java.io.UncheckedIOException; | ||
import java.lang.System.Logger.Level; | ||
import java.time.Duration; | ||
|
||
import io.helidon.http.sse.SseEvent; | ||
import io.helidon.webserver.http.HttpRules; | ||
import io.helidon.webserver.http.HttpService; | ||
import io.helidon.webserver.http.ServerRequest; | ||
import io.helidon.webserver.http.ServerResponse; | ||
import io.helidon.webserver.sse.SseSink; | ||
|
||
import jakarta.json.spi.JsonProvider; | ||
|
||
/** | ||
* SSE service. | ||
*/ | ||
class SseService implements HttpService { | ||
|
||
private static final System.Logger LOGGER = System.getLogger(SseService.class.getName()); | ||
private static final JsonProvider JSON_PROVIDER = JsonProvider.provider(); | ||
|
||
@Override | ||
public void routing(HttpRules httpRules) { | ||
httpRules.get("/sse_text", this::sseText) | ||
.get("/sse_json", this::sseJson); | ||
} | ||
|
||
void sseText(ServerRequest req, ServerResponse res) throws InterruptedException { | ||
int count = req.query().first("count").asInt().orElse(1); | ||
int delay = req.query().first("delay").asInt().orElse(0); | ||
try (SseSink sseSink = res.sink(SseSink.TYPE)) { | ||
for (int i = 0; i < count; i++) { | ||
try { | ||
sseSink.emit(SseEvent.builder() | ||
.comment("comment#" + i) | ||
.name("my-event") | ||
.data("data#" + i) | ||
.build()); | ||
} catch (UncheckedIOException e) { | ||
LOGGER.log(Level.DEBUG, e.getMessage()); // connection close? | ||
return; | ||
} | ||
|
||
if (delay > 0) { | ||
Thread.sleep(Duration.ofSeconds(delay)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void sseJson(ServerRequest req, ServerResponse res) throws InterruptedException { | ||
int count = req.query().first("count").asInt().orElse(1); | ||
int delay = req.query().first("delay").asInt().orElse(0); | ||
try (SseSink sseSink = res.sink(SseSink.TYPE)) { | ||
for (int i = 0; i < count; i++) { | ||
try { | ||
sseSink.emit(SseEvent.create(JSON_PROVIDER.createObjectBuilder() | ||
.add("data", "data#" + i) | ||
.build())); | ||
} catch (UncheckedIOException e) { | ||
LOGGER.log(Level.DEBUG, e.getMessage()); // connection close? | ||
return; | ||
} | ||
|
||
if (delay > 0) { | ||
Thread.sleep(Duration.ofSeconds(delay)); | ||
} | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/webserver/sse/src/main/java/io/helidon/examples/webserver/sse/package-info.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,20 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Helidon Examples WebServer SSE. | ||
*/ | ||
package io.helidon.examples.webserver.sse; |
Oops, something went wrong.