From a46bbb58dd39cf2a0548ab16599c1cb77eecbeb0 Mon Sep 17 00:00:00 2001 From: dshurt Date: Mon, 26 Oct 2015 09:56:04 -0600 Subject: [PATCH] #211 - Socket IO Module - Hanging on Shutdown Fix for not releasing resources causing hang --- .../atmosphere/socketio/SocketIOSession.java | 1 - .../socketio/SocketIOSessionManager.java | 110 +++++++++--------- .../cpr/SocketIOAtmosphereInterceptor.java | 13 +-- .../transport/SocketIOSessionManagerImpl.java | 11 ++ 4 files changed, 75 insertions(+), 60 deletions(-) diff --git a/socketio/modules/src/main/java/org/atmosphere/socketio/SocketIOSession.java b/socketio/modules/src/main/java/org/atmosphere/socketio/SocketIOSession.java index 8aee41ab3..bafea2a30 100755 --- a/socketio/modules/src/main/java/org/atmosphere/socketio/SocketIOSession.java +++ b/socketio/modules/src/main/java/org/atmosphere/socketio/SocketIOSession.java @@ -17,7 +17,6 @@ import org.atmosphere.cpr.AtmosphereHandler; import org.atmosphere.cpr.AtmosphereResourceImpl; -import org.atmosphere.socketio.cpr.SocketIOAtmosphereHandler; import org.atmosphere.socketio.transport.DisconnectReason; /** diff --git a/socketio/modules/src/main/java/org/atmosphere/socketio/SocketIOSessionManager.java b/socketio/modules/src/main/java/org/atmosphere/socketio/SocketIOSessionManager.java index c2002b7ae..147dfdd4a 100755 --- a/socketio/modules/src/main/java/org/atmosphere/socketio/SocketIOSessionManager.java +++ b/socketio/modules/src/main/java/org/atmosphere/socketio/SocketIOSessionManager.java @@ -1,52 +1,58 @@ -/* - * Copyright 2014 Sebastien Dionne - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ -package org.atmosphere.socketio; - -/** - * @author Sebastien Dionne : sebastien.dionne@gmail.com - */ -public interface SocketIOSessionManager extends SocketIOSessionFactory { - - /** - * @param timeout - */ - void setTimeout(long timeout); - - /** - * @return - */ - long getTimeout(); - - /** - * @param interval - */ - void setHeartbeatInterval(long interval); - - /** - * @return - */ - long getHeartbeatInterval(); - - /** - * @param suspendTime - */ - void setRequestSuspendTime(long suspendTime); - - /** - * @return - */ - long getRequestSuspendTime(); -} +/* + * Copyright 2014 Sebastien Dionne + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package org.atmosphere.socketio; + +/** + * @author Sebastien Dionne : sebastien.dionne@gmail.com + */ +public interface SocketIOSessionManager extends SocketIOSessionFactory { + + /** + * @param timeout + */ + void setTimeout(long timeout); + + /** + * @return + */ + long getTimeout(); + + /** + * @param interval + */ + void setHeartbeatInterval(long interval); + + /** + * @return + */ + long getHeartbeatInterval(); + + /** + * @param suspendTime + */ + void setRequestSuspendTime(long suspendTime); + + /** + * @return + */ + long getRequestSuspendTime(); + + /** + * This will destroy the session manager and release resources. + */ + void destory(); + +} diff --git a/socketio/modules/src/main/java/org/atmosphere/socketio/cpr/SocketIOAtmosphereInterceptor.java b/socketio/modules/src/main/java/org/atmosphere/socketio/cpr/SocketIOAtmosphereInterceptor.java index 2708aa9a1..452938e1e 100755 --- a/socketio/modules/src/main/java/org/atmosphere/socketio/cpr/SocketIOAtmosphereInterceptor.java +++ b/socketio/modules/src/main/java/org/atmosphere/socketio/cpr/SocketIOAtmosphereInterceptor.java @@ -15,6 +15,10 @@ */ package org.atmosphere.socketio.cpr; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import javax.servlet.http.HttpServletResponse; import org.atmosphere.config.service.AtmosphereInterceptorService; import org.atmosphere.cpr.Action; import org.atmosphere.cpr.AsyncIOWriter; @@ -32,19 +36,13 @@ import org.atmosphere.socketio.transport.JSONPPollingTransport; import org.atmosphere.socketio.transport.SocketIOPacketImpl; import org.atmosphere.socketio.transport.SocketIOSessionManagerImpl; +import static org.atmosphere.socketio.transport.SocketIOSessionManagerImpl.mapper; import org.atmosphere.socketio.transport.Transport; import org.atmosphere.socketio.transport.WebSocketTransport; import org.atmosphere.socketio.transport.XHRPollingTransport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import static org.atmosphere.socketio.transport.SocketIOSessionManagerImpl.mapper; - /** * SocketIO implementation. * @@ -250,6 +248,7 @@ public void postInspect(AtmosphereResource r) { @Override public void destroy() { + sessionManager.destory(); } @Override diff --git a/socketio/modules/src/main/java/org/atmosphere/socketio/transport/SocketIOSessionManagerImpl.java b/socketio/modules/src/main/java/org/atmosphere/socketio/transport/SocketIOSessionManagerImpl.java index 51a49de70..7c7836dba 100755 --- a/socketio/modules/src/main/java/org/atmosphere/socketio/transport/SocketIOSessionManagerImpl.java +++ b/socketio/modules/src/main/java/org/atmosphere/socketio/transport/SocketIOSessionManagerImpl.java @@ -27,6 +27,7 @@ import java.util.concurrent.atomic.AtomicLong; import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.concurrent.TimeUnit; import org.atmosphere.cpr.AtmosphereHandler; import org.atmosphere.cpr.AtmosphereRequest; import org.atmosphere.cpr.AtmosphereResourceEventImpl; @@ -151,6 +152,16 @@ public long getRequestSuspendTime() { return requestSuspendTime; } + @Override + public void destory() { + executor.shutdownNow(); + try { + executor.awaitTermination(1, TimeUnit.SECONDS); + } catch (InterruptedException ex) { + logger.debug("Interrupted while waiting for executor to terminate. (Continuing)", ex); + } + } + private class SessionImpl implements SocketIOSession { private final String sessionId;