package com.megatim.fdxconsultation.service.impl.sockets.helpers; import com.megatim.fdxconsultation.model.supervision.StandaloneServerStateEntity; import com.megatim.fdxconsultation.service.impl.sockets.StandaloneServerStateEndpoint; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; import javax.enterprise.context.ApplicationScoped; import javax.websocket.EncodeException; import javax.websocket.Session; /** * * @author Gabuntu */ @ApplicationScoped public class StandaloneServerStateSessionHandler { private final Map idtoSessions = new ConcurrentHashMap<>(); public void publishMessage(StandaloneServerStateEntity standaloneServerStateEntity) { getSessions().forEach(s -> { try { s.getBasicRemote().sendObject(standaloneServerStateEntity); } catch (IOException | EncodeException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } }); } public void addSession(Session session) { idtoSessions.put(session.getId(), session); } public void removeSession(Session session) { idtoSessions.remove(session.getId()); } private List getSessions() { return idtoSessions .entrySet() .stream() .map(e -> e.getValue()) .collect(Collectors.toList()); } }