Commit ed92cfa5 authored by Alex Wenckus's avatar Alex Wenckus Committed by alex

Inactivity timer is now a timer that runs every thirty seconds and the last...

Inactivity timer is now a timer that runs every thirty seconds and the last time a session was active is set on the session itself.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@5921 b35dd754-fafc-0310-a699-88a17e54d16e
parent 1d1a49b0
...@@ -26,6 +26,7 @@ import javax.servlet.http.HttpServlet; ...@@ -26,6 +26,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
...@@ -52,6 +53,18 @@ public class HttpBindServlet extends HttpServlet { ...@@ -52,6 +53,18 @@ public class HttpBindServlet extends HttpServlet {
this.sessionManager = sessionManager; this.sessionManager = sessionManager;
} }
@Override public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
sessionManager.start();
}
@Override public void destroy() {
super.destroy();
sessionManager.stop();
}
@Override @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException throws ServletException, IOException
......
...@@ -48,10 +48,12 @@ public class HttpSession extends ClientSession { ...@@ -48,10 +48,12 @@ public class HttpSession extends ClientSession {
private Set<SessionListener> listeners = new HashSet<SessionListener>(); private Set<SessionListener> listeners = new HashSet<SessionListener>();
private boolean isClosed; private boolean isClosed;
private int inactivityTimeout; private int inactivityTimeout;
private long lastActivity;
public HttpSession(String serverName, InetAddress address, StreamID streamID) { public HttpSession(String serverName, InetAddress address, StreamID streamID) {
super(serverName, null, streamID); super(serverName, null, streamID);
conn = new HttpVirtualConnection(address); conn = new HttpVirtualConnection(address);
this.lastActivity = System.currentTimeMillis();
} }
void addConnection(HttpConnection connection, boolean isPoll) throws HttpBindException, void addConnection(HttpConnection connection, boolean isPoll) throws HttpBindException,
...@@ -92,6 +94,7 @@ public class HttpSession extends ClientSession { ...@@ -92,6 +94,7 @@ public class HttpSession extends ClientSession {
} }
private void fireConnectionOpened(HttpConnection connection) { private void fireConnectionOpened(HttpConnection connection) {
lastActivity = System.currentTimeMillis();
Collection<SessionListener> listeners = Collection<SessionListener> listeners =
new HashSet<SessionListener>(this.listeners); new HashSet<SessionListener>(this.listeners);
for(SessionListener listener : listeners) { for(SessionListener listener : listeners) {
...@@ -215,6 +218,7 @@ public class HttpSession extends ClientSession { ...@@ -215,6 +218,7 @@ public class HttpSession extends ClientSession {
} }
private void fireConnectionClosed(HttpConnection connection) { private void fireConnectionClosed(HttpConnection connection) {
lastActivity = System.currentTimeMillis();
Collection<SessionListener> listeners = Collection<SessionListener> listeners =
new HashSet<SessionListener>(this.listeners); new HashSet<SessionListener>(this.listeners);
for(SessionListener listener : listeners) { for(SessionListener listener : listeners) {
...@@ -340,6 +344,10 @@ public class HttpSession extends ClientSession { ...@@ -340,6 +344,10 @@ public class HttpSession extends ClientSession {
return connectionQueue.size(); return connectionQueue.size();
} }
public synchronized long getLastActivity() {
return lastActivity;
}
/** /**
* A virtual server connection relates to a http session which its self can relate to many * A virtual server connection relates to a http session which its self can relate to many
* http connections. * http connections.
......
...@@ -12,6 +12,7 @@ package org.jivesoftware.wildfire.http; ...@@ -12,6 +12,7 @@ package org.jivesoftware.wildfire.http;
import org.jivesoftware.util.JiveGlobals; import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log; import org.jivesoftware.util.Log;
import org.jivesoftware.util.JiveConstants;
import org.jivesoftware.wildfire.SessionManager; import org.jivesoftware.wildfire.SessionManager;
import org.jivesoftware.wildfire.StreamID; import org.jivesoftware.wildfire.StreamID;
import org.jivesoftware.wildfire.multiplex.MultiplexerPacketRouter; import org.jivesoftware.wildfire.multiplex.MultiplexerPacketRouter;
...@@ -20,6 +21,7 @@ import org.jivesoftware.wildfire.auth.UnauthorizedException; ...@@ -20,6 +21,7 @@ import org.jivesoftware.wildfire.auth.UnauthorizedException;
import org.dom4j.*; import org.dom4j.*;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.InetAddress; import java.net.InetAddress;
...@@ -57,9 +59,10 @@ public class HttpSessionManager { ...@@ -57,9 +59,10 @@ public class HttpSessionManager {
*/ */
private static int pollingInterval; private static int pollingInterval;
private InactivityTimer timer = new InactivityTimer();
private SessionManager sessionManager; private SessionManager sessionManager;
private Map<String, HttpSession> sessionMap = new HashMap<String, HttpSession>(); private Map<String, HttpSession> sessionMap = new ConcurrentHashMap<String, HttpSession>();
private Timer inactivityTimer = new Timer("HttpSession Inactivity Timer");
private TimerTask inactivityThread = new HttpSessionReaper();
static { static {
// Set the default read idle timeout. If none was set then assume 30 minutes // Set the default read idle timeout. If none was set then assume 30 minutes
...@@ -72,6 +75,19 @@ public class HttpSessionManager { ...@@ -72,6 +75,19 @@ public class HttpSessionManager {
this.sessionManager = SessionManager.getInstance(); this.sessionManager = SessionManager.getInstance();
} }
public void start() {
inactivityTimer.schedule(inactivityThread, 30 * JiveConstants.SECOND,
30 * JiveConstants.SECOND);
}
public void stop() {
inactivityTimer.cancel();
for(HttpSession session : sessionMap.values()) {
session.close();
}
sessionMap.clear();
}
public HttpSession getSession(String streamID) { public HttpSession getSession(String streamID) {
return sessionMap.get(streamID); return sessionMap.get(streamID);
} }
...@@ -109,8 +125,6 @@ public class HttpSessionManager { ...@@ -109,8 +125,6 @@ public class HttpSessionManager {
Log.error("Error creating document", e); Log.error("Error creating document", e);
throw new HttpBindException("Internal server error", true, 500); throw new HttpBindException("Internal server error", true, 500);
} }
timer.reset(session);
return session; return session;
} }
...@@ -123,19 +137,14 @@ public class HttpSessionManager { ...@@ -123,19 +137,14 @@ public class HttpSessionManager {
sessionMap.put(streamID.getID(), session); sessionMap.put(streamID.getID(), session);
session.addSessionCloseListener(new SessionListener() { session.addSessionCloseListener(new SessionListener() {
public void connectionOpened(HttpSession session, HttpConnection connection) { public void connectionOpened(HttpSession session, HttpConnection connection) {
timer.stop(session);
} }
public void connectionClosed(HttpSession session, HttpConnection connection) { public void connectionClosed(HttpSession session, HttpConnection connection) {
if (session.getConnectionCount() <= 0) {
timer.reset(session);
}
} }
public void sessionClosed(HttpSession session) { public void sessionClosed(HttpSession session) {
sessionMap.remove(session.getStreamID().getID()); sessionMap.remove(session.getStreamID().getID());
sessionManager.removeSession(session); sessionManager.removeSession(session);
timer.stop(session);
} }
}); });
return session; return session;
...@@ -200,38 +209,15 @@ public class HttpSessionManager { ...@@ -200,38 +209,15 @@ public class HttpSessionManager {
return connection; return connection;
} }
private class InactivityTimer extends Timer { private class HttpSessionReaper extends TimerTask {
private Map<String, InactivityTimeoutTask> sessionMap
= new HashMap<String, InactivityTimeoutTask>();
public void stop(HttpSession session) {
InactivityTimeoutTask task = sessionMap.remove(session.getStreamID().getID());
if(task != null) {
task.cancel();
}
}
public void reset(HttpSession session) {
stop(session);
if(session.isClosed()) {
return;
}
InactivityTimeoutTask task = new InactivityTimeoutTask(session);
schedule(task, session.getInactivityTimeout() * 1000);
sessionMap.put(session.getStreamID().getID(), task);
}
}
private class InactivityTimeoutTask extends TimerTask {
private HttpSession session;
public InactivityTimeoutTask(HttpSession session) {
this.session = session;
}
public void run() { public void run() {
for(HttpSession session : sessionMap.values()) {
long lastActive = (System.currentTimeMillis() - session.getLastActivity()) / 1000;
if(lastActive > session.getInactivityTimeout()) {
session.close(); session.close();
timer.sessionMap.remove(session.getStreamID().getID()); }
}
} }
} }
} }
...@@ -20,9 +20,7 @@ import org.jivesoftware.wildfire.http.HttpBindServlet; ...@@ -20,9 +20,7 @@ import org.jivesoftware.wildfire.http.HttpBindServlet;
import org.jivesoftware.wildfire.container.BasicModule; import org.jivesoftware.wildfire.container.BasicModule;
import org.jivesoftware.wildfire.multiplex.MultiplexerPacketDeliverer; import org.jivesoftware.wildfire.multiplex.MultiplexerPacketDeliverer;
import org.jivesoftware.wildfire.net.*; import org.jivesoftware.wildfire.net.*;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.servlet.ServletHolder; import org.mortbay.jetty.servlet.ServletHolder;
import org.mortbay.jetty.servlet.ServletHandler;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment