[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[jetty-users] Memory leak with websockets
|
Hi
I have the same problem.
Please have a look at https://github.com/softappeal/yass/blob/master/src/test/java/ch/softappeal/yass/transport/ws/test/JettyWebSocketLeak.java .
It shows the leak.
Regards,
Angelo
---------------------------------------------------------------------------------------------------------------------------------------------
package ch.softappeal.yass.transport.ws.test;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.websocket.jsr356.ClientContainer;
import org.eclipse.jetty.websocket.jsr356.JsrSession;
import org.eclipse.jetty.websocket.jsr356.server.ServerContainer;
import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer;
import org.eclipse.jetty.websocket.server.WebSocketServerFactory;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.Extension;
import javax.websocket.HandshakeResponse;
import javax.websocket.Session;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
public class JettyWebSocketLeak {
public static class WsConfigurator extends ServerEndpointConfig.Configurator {
@SuppressWarnings("unchecked")
@Override public <T> T getEndpointInstance(Class<T> endpointClass) {
return (T)new Endpoint() {
@Override public void onOpen(Session session, EndpointConfig config) {
try {
System.out.println("closing " + session);
session.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override public void onClose(Session session, CloseReason closeReason) {
}
@Override public void onError(Session session, Throwable throwable) {
}
};
}
public Endpoint getEndpointInstance() {
return getEndpointInstance(null);
}
@Override public String getNegotiatedSubprotocol(List<String> supported, List<String> requested) {
for (String r : requested) {
if (supported.contains(r)) {
return r;
}
}
return "";
}
@Override public List<Extension> getNegotiatedExtensions(List<Extension> installed, List<Extension> requested) {
List<Extension> extensions = new ArrayList<>(requested.size());
for (Extension r : requested) {
for (Extension i : installed) {
if (i.getName().equals(r.getName())) {
extensions.add(r);
break;
}
}
}
return extensions;
}
@Override public boolean checkOrigin(String originHeaderValue) {
return true;
}
@Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
}
}
private static int PORT = 9090;
private static String PATH = "/test";
private static URI THE_URI = URI.create("ws://localhost:" + PORT + PATH);
private static void connectClient() throws Exception {
ClientContainer clientContainer = new ClientContainer();
clientContainer.start();
clientContainer.connectToServer(new WsConfigurator().getEndpointInstance(), ClientEndpointConfig.Builder.create().build(), THE_URI);
clientContainer.stop();
}
public static void main(String... args) throws Exception {
Server server = new Server();
ServerConnector serverConnector = new ServerConnector(server);
serverConnector.setPort(PORT);
server.addConnector(serverConnector);
ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
servletContextHandler.setContextPath("/");
server.setHandler(servletContextHandler);
ServerContainer serverContainer = WebSocketServerContainerInitializer.configureContext(servletContextHandler);
serverContainer.addEndpoint(ServerEndpointConfig.Builder.create(Endpoint.class, PATH).configurator(new WsConfigurator()).build());
server.start();
connectClient();
connectClient();
System.out.println("printing the leaked server side sessions ...");
serverContainer.getBeans(WebSocketServerFactory.class).forEach(factory -> factory.getBeans(JsrSession.class).forEach(System.out::println));
}
}
________________________________
Disclaimer:
Aufgrund der bisherigen E-Mail-Korrespondenz bzw. der getroffenen Absprache, betrachtet sich die Zürcher Kantonalbank als berechtigt, mit Ihnen per E-Mail zu kommunizieren. Die Zürcher Kantonalbank geht davon aus, dass Sie die Risiken von E-Mails kennen und in Kauf nehmen (insbesondere fehlende Vertraulichkeit, Manipulation oder Missbrauch durch Dritte, Fehlleitung, verzögerte Übermittlung oder Bearbeitung, Viren, etc.). Die Zürcher Kantonalbank lehnt jede Haftung für Schäden im Zusammenhang mit der Verwendung von E-Mails ab, sofern die Bank die geschäftsübliche Sorgfalt nicht verletzt hat.
E-Mails werden nur während den üblichen Geschäftszeiten der Bank bearbeitet. Sie können nicht von der sofortigen Kenntnisnahme Ihrer E-Mails ausgehen. E-Mail eignet sich daher nicht für die Übermittlung von Handelsaufträgen und wertverschiebenden oder zeitkritischen Aufträgen (z.B. Zahlungsaufträge).
Sollten Sie dieses E-Mail irrtümlicherweise erhalten haben, bitten wir Sie, das E-Mail an die Zürcher Kantonalbank zurückzusenden und das E-Mail anschliessend mitsamt allen Anhängen von ihrem System zu löschen. Der Gebrauch der im E-Mail enthaltenen Information ist verboten.
Based on previous e-mail correspondence or an arrangement we have reached with you, Zürcher Kantonalbank considers itself to be entitled to communicate with you via e-mail. Zürcher Kantonalbank assumes that you know the risks associated with e-mails and that you accept them (in particular, the lack of confidentiality, manipulation or misuse by third parties, misdirection, delayed transmission or processing, viruses, etc.). Zürcher Kantonalbank accepts no liability whatsoever for damage caused in connection with the use of e-mail, provided that the Bank has not failed to exercise customary due care.
E-mails are processed only during the Bank’s normal business hours. You cannot assume that your e-mails will be read immediately. E-mails are therefore not suitable for sending trading orders and orders that change the value of an account or are time-critical (e.g. payment orders).
If you have received this e-mail in error, please respond to Zürcher Kantonalbank and then delete this e-mail and your response together with all attachments from your system. The use of the information contained in the e-mail is prohibited.