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

Minor updates to SEE example #151

Merged
merged 1 commit into from
Nov 7, 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
Original file line number Diff line number Diff line change
@@ -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.")
Expand Down Expand Up @@ -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;

Expand All @@ -55,7 +58,8 @@ public class EventsResource {
private NamedCache<String, Price> prices;

@Context
private Sse sse;
private Sse sse;

private SseBroadcaster broadcaster;

@PostConstruct
Expand All @@ -64,13 +68,14 @@ void createBroadcaster() {
this.prices = Utilities.getPricesCache();

prices.addMapListener(new SimpleMapListener<String, Price>()
.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();
}
Expand All @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/web/javascripts/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/web/sse.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading