public class HumanAgentSocket extends TextWebSocketHandler {
@Value("${human.agents.available}")
private int availableHumanAgents;
private List<WebSocketSession> sessions = new ArrayList<>();
private List<String> sessionIds = new ArrayList<>();
private HashMap<WebSocketSession,WebSocketSession> socketConnections = new HashMap<WebSocketSession,WebSocketSession>();
private int noOfHumanAgents = 1;
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public void afterConnectionEstablished(WebSocketSession session) throws IOException {
if (noOfHumanAgents<=5){
System.out.println("Agents "+availableHumanAgents);
sessions.add(session);
sessionIds.add(session.getId());
System.out.println("List "+sessionIds);
if(sessions.size() == 2){
socketConnections.put(sessions.get(0),sessions.get(1));
socketConnections.put(sessions.get(1),sessions.get(0));
System.out.println("SocketConnections "+socketConnections);
sessions.clear();
noOfHumanAgents++;
System.out.println("Sessios "+sessions.size());
}
}else {
throw new HumanAgentException("agents are busy");
}
}
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
WebSocketSession webSocketSession = socketConnections.get(session);
String payload = message.getPayload();
JsonNode jsonNode = objectMapper.readTree(payload);
String content = jsonNode.get("message").asText();
if (content.equalsIgnoreCase("CLOSE")){
afterConnectionClosed(session,null);
}
if (webSocketSession.isOpen()){
webSocketSession.sendMessage(message);
}
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
WebSocketSession humanAgentSession = socketConnections.get(session);
if(humanAgentSession!=null){
sessions.add(humanAgentSession);
}
socketConnections.remove(session);
socketConnections.remove(humanAgentSession);
}
public String getWebSocketSession(String sessionId){
System.out.println("List "+sessionIds);
String webSocketSession = sessionIds.get(Integer.parseInt(sessionId));
return webSocketSession;
}
}
Here I am trying to access the configurable value from application.properties and Even I tried from environment variables also but always it’s value coming as 0 for ‘availableHumanAgents’ variable but the excepted will be the value which I specified or configured in application.properties file.
10