我使用Spring boot的WebSocketConfigurer来创建websocket,按照以下代码创建一个在线系统。我的用户将连接到它,它将继续跟踪他们的存在。
@Configuration
@EnableWebSocket
public class WebSocketServerConfiguration implements WebSocketConfigurer {
private final PresenceListener presenceListener;
Logger logger = Logger.getLogger(WebSocketServerConfiguration.class.getName());
@Autowired
public WebSocketServerConfiguration(StringRedisTemplate stringRedisTemplate){
this.presenceListener = new PresenceListener(stringRedisTemplate);
}
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new WebSocketHandler() {
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
logger.info("afterConnectionEstablished:"+session.toString());
}
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
//Some code inside
}
@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
logger.info("handleTransportError:" +session.toString());
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
logger.info("afterConnectionClosed:"+session.toString());
}
@Override
public boolean supportsPartialMessages() {
return false;
}
}, "/presence").setAllowedOrigins("*");
}
}
部署后一段时间后,我开始关注例外:
java.io.IOException: Too many open files
at sun.nio.ch.ServerSocketChannelImpl.accept0(java.base@9-internal/Native Method) ~[na:na]
at sun.nio.ch.ServerSocketChannelImpl.accept(java.base@9-internal/ServerSocketChannelImpl.java:424) ~[na:na]
at sun.nio.ch.ServerSocketChannelImpl.accept(java.base@9-internal/ServerSocketChannelImpl.java:252) ~[na:na]
at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:443) ~[tomcat-embed-core-8.5.11.jar!/:8.5.11]
at java.lang.Thread.run(java.base@9-internal/Thread.java:804) [na:na]
请帮助解决这个问题。