Created
February 21, 2021 07:44
-
-
Save dunithd/e6d558aecf852762b4dccf015f179222 to your computer and use it in GitHub Desktop.
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
package com.edu.retail.ws; | |
import io.quarkus.scheduler.Scheduled; | |
import javax.enterprise.context.ApplicationScoped; | |
import javax.websocket.*; | |
import javax.websocket.server.PathParam; | |
import javax.websocket.server.ServerEndpoint; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.atomic.AtomicInteger; | |
@ServerEndpoint("/dashboard/{clientId}") | |
@ApplicationScoped | |
public class DashboardWebSocket { | |
private Map<String, Session> sessions = new ConcurrentHashMap<>(); | |
private AtomicInteger totalOrders = new AtomicInteger(); | |
@OnOpen | |
public void onOpen(Session session, @PathParam("clientId") String clientId) { | |
sessions.put(clientId, session); | |
} | |
@OnClose | |
public void onClose(Session session, @PathParam("clientId") String clientId) { | |
sessions.remove(clientId); | |
} | |
@OnError | |
public void onError(Session session, @PathParam("clientId") String clientId, Throwable throwable) { | |
sessions.remove(clientId); | |
} | |
@Scheduled(every="5s") | |
void increment() { | |
if (sessions != null) { | |
totalOrders.incrementAndGet(); | |
broadcast(String.valueOf(totalOrders)); | |
} | |
} | |
private void broadcast(String message) { | |
sessions.values().forEach(s -> { | |
s.getAsyncRemote().sendObject(message, result -> { | |
if (result.getException() != null) { | |
System.out.println("Unable to send message: " + result.getException()); | |
} | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment