From 0fefe07df002310ebda377268a7756b43e9920f1 Mon Sep 17 00:00:00 2001 From: Tim Middleton Date: Thu, 7 Nov 2024 14:22:27 +0800 Subject: [PATCH] Minor updates to SEE example (#151) --- .../demo/application/EventsResource.java | 42 ++++++++++++++++--- .../resources/web/javascripts/controllers.js | 2 +- src/main/resources/web/sse.html | 3 +- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/oracle/coherence/demo/application/EventsResource.java b/src/main/java/com/oracle/coherence/demo/application/EventsResource.java index c4f8405..b1849a2 100644 --- a/src/main/java/com/oracle/coherence/demo/application/EventsResource.java +++ b/src/main/java/com/oracle/coherence/demo/application/EventsResource.java @@ -1,7 +1,7 @@ /* * File: EventsResource.java * - * Copyright (c) 2015, 2020 Oracle and/or its affiliates. + * Copyright (c) 2015, 2024 Oracle and/or its affiliates. * * You may not use this file except in compliance with the Universal Permissive * License (UPL), Version 1.0 (the "License.") @@ -38,6 +38,9 @@ import jakarta.ws.rs.sse.Sse; import jakarta.ws.rs.sse.SseBroadcaster; import jakarta.ws.rs.sse.SseEventSink; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlRootElement; import java.util.Date; @@ -55,7 +58,8 @@ public class EventsResource { private NamedCache prices; @Context - private Sse sse; + private Sse sse; + private SseBroadcaster broadcaster; @PostConstruct @@ -64,13 +68,14 @@ void createBroadcaster() { this.prices = Utilities.getPricesCache(); prices.addMapListener(new SimpleMapListener() - .addUpdateHandler(e->broadcaster.broadcast(createEvent("priceUpdate", e.getNewValue())))); + .addUpdateHandler(e->broadcaster.broadcast(createEvent("priceUpdate", + e.getNewValue().getSymbol(), e.getOldValue().getPrice(), e.getNewValue().getPrice())))); } - private OutboundSseEvent createEvent(String name, Price price) { + private OutboundSseEvent createEvent(String name, String symbol, double oldPrice, double newPrice) { return sse.newEventBuilder() .name(name) - .data(Price.class, price) + .data(Price.class, new PriceUpdate(symbol, oldPrice, newPrice)) .mediaType(APPLICATION_JSON_TYPE) .build(); } @@ -85,7 +90,32 @@ private OutboundSseEvent createEvent(String name, Price price) { @Produces(MediaType.SERVER_SENT_EVENTS) public void registerEventListener(@Context SseEventSink eventSink) { broadcaster.register(eventSink); - eventSink.send(sse.newEvent("begin", new Date().toString())); } + + @XmlRootElement(name = "price") + @XmlAccessorType(XmlAccessType.PROPERTY) + public static class PriceUpdate { + private final String symbol; + private final double oldPrice; + private final double newPrice; + + public PriceUpdate(String symbol, double oldPrice, double newPrice) { + this.symbol = symbol; + this.oldPrice = oldPrice; + this.newPrice = newPrice; + } + + public String getSymbol() { + return symbol; + } + + public double getOldPrice() { + return oldPrice; + } + + public double getNewPrice() { + return newPrice; + } + } } diff --git a/src/main/resources/web/javascripts/controllers.js b/src/main/resources/web/javascripts/controllers.js index 95c22a4..4094c9b 100644 --- a/src/main/resources/web/javascripts/controllers.js +++ b/src/main/resources/web/javascripts/controllers.js @@ -942,7 +942,7 @@ demoApp.controller('DemoController', ['$scope', '$http', '$interval', '$location let windowName = "new" + new Date().toISOString() let left = (screen.width/2)-250; let top = (screen.height/2)-200; - $window.open("/application/sse.html", windowName, "height=400,width=500,top=" + top + ",left=" + left); + $window.open("/application/sse.html", windowName, "height=400,width=640,top=" + top + ",left=" + left); } self.addSymbolTrades = function(symbol) { diff --git a/src/main/resources/web/sse.html b/src/main/resources/web/sse.html index 919c56d..386812c 100644 --- a/src/main/resources/web/sse.html +++ b/src/main/resources/web/sse.html @@ -75,7 +75,8 @@ eventSource.addEventListener('priceUpdate', function (event) { const messageContainer = document.getElementById('messages'); let price = JSON.parse(event.data) - let output = new Date().toISOString() + ": Symbol=" + price.symbol + ", Price=" + formatter.format(price.price) + let output = new Date().toISOString() + ": Symbol=" + price.symbol + ", new=" + formatter.format(price.newPrice) + + ", old=" + formatter.format(price.oldPrice) + ", change=" + formatter.format(price.newPrice - price.oldPrice) const message = document.createElement('p'); message.textContent = output