我使用 Tyrus 实现制作了一个 websocket 服务器,它占用了 50% 的 cpu 使用率,但我不知道为什么。一开始我以为是进程,后来我写了一个非常简单的ws服务器,还是一样...
这是资源监视器 (Windows Server 2008 R2) 的屏幕截图:
服务器在 Intel(R) Xeon(R) CPU E5-2650 0 @ 2.00 Ghz 上运行,4GB 内存我不知道确切的架构...
简化ws服务器的代码:
package wstest;
import javax.websocket.DeploymentException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.glassfish.tyrus.server.Server;
public class Wstest {
private static final Logger logger = LogManager.getLogger();
public static void main(String[] args) {
runServer();
}
private static void runServer() {
Server server = new Server("localhost", 8025, "/tp", null, wsendpoint.class);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
logger.info("Server stopped.");
}
});
try {
server.start();
logger.info("Server started.");
while (true) { }
} catch (DeploymentException ex) {
logger.fatal(ex);
}
}
}
package wstest;
import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ServerEndpoint(
value = "/test"
)
public class wsendpoint {
private static final Logger logger = LogManager.getLogger();
@OnOpen
public void onOpen(Session session) {
logger.info("Client " + session.getId() + " connected");
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
logger.info("Client " + session.getId() + " disconnected.");
}
@OnMessage
public void onMessage(String request, Session session) {
logger.info(session.getId() + " sent message : " + request);
}
@OnError
public void onError(Session session, Throwable t) {
logger.error("Error !", t);
}
}
这是我使用的库: