Commit 8bc84195 authored by Jens Ehlers's avatar Jens Ehlers
Browse files

Added a Java EventSource (SSE) client example based on okhttp-eventsource

parent 28b2c6b8
Pipeline #24778 passed with stage
in 1 minute
......@@ -35,6 +35,18 @@
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-core</artifactId>
</dependency>
<!-- required only for Java SSE client -->
<dependency>
<groupId>com.launchdarkly</groupId>
<artifactId>okhttp-eventsource</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.3.70</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package counter.client;
import com.launchdarkly.eventsource.EventHandler;
import com.launchdarkly.eventsource.MessageEvent;
public class CountEventHandler implements EventHandler {
@Override
public void onMessage(String event, MessageEvent messageEvent) {
System.out.println(messageEvent.getData());
}
@Override
public void onOpen() {
System.out.println("onOpen");
}
@Override
public void onClosed() {
System.out.println("onClosed");
}
@Override
public void onComment(String comment) {
System.out.println("onComment");
}
@Override
public void onError(Throwable t) {
System.out.println("onError: " + t);
}
}
package counter.client;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import com.launchdarkly.eventsource.EventSource;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
public class CounterClient {
public static void main(String[] args) throws InterruptedException {
String url = "http://localhost:8080/count/10";
EventSource.Builder b = new EventSource.Builder(new CountEventHandler(), URI.create(url));
b.reconnectTime(Duration.ofSeconds(2)); // reconnect after 2 seconds
EventSource eventSource = b.build();
eventSource.start();
// disable logging
Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.OFF);
TimeUnit.MINUTES.sleep(1);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment