Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New simple SSE test that shows how to use the API in Helidon SE #98

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/webserver/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@
<module>tracing</module>
<module>tutorial</module>
<module>websocket</module>
<module>sse</module>
</modules>
</project>
16 changes: 16 additions & 0 deletions examples/webserver/sse/README.md
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.
96 changes: 96 additions & 0 deletions examples/webserver/sse/pom.xml
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>
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());
}
}
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));
}
}
}
}
}
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;
Loading