1 package org.jivesoftware.messenger.net; 2 3 import org.jivesoftware.util.JiveGlobals; 4 import org.jivesoftware.util.Log; 5 6 import java.io.IOException ; 7 import java.net.Socket ; 8 import java.util.Date ; 9 import java.util.Map ; 10 import java.util.concurrent.ConcurrentHashMap ; 11 12 30 public class SocketSendingTracker { 31 32 33 private static SocketSendingTracker instance = new SocketSendingTracker(); 34 38 private Map <Socket , Date > sockets = new ConcurrentHashMap <Socket , Date >(); 39 40 43 private boolean shutdown = false; 44 45 49 private Thread checkingThread; 50 51 56 public static SocketSendingTracker getInstance() { 57 return instance; 58 } 59 60 63 private SocketSendingTracker() { 64 } 65 66 73 public void socketStartedSending(Socket socket) { 74 sockets.put(socket, new Date ()); 75 } 76 77 83 public void socketFinishedSending(Socket socket) { 84 sockets.remove(socket); 85 } 86 87 91 public void start() { 92 shutdown = false; 93 checkingThread = new Thread ("SocketSendingTracker") { 94 public void run() { 95 while (!shutdown) { 96 checkHealth(); 97 synchronized (this) { 98 try { 99 wait(10000); 100 } 101 catch (InterruptedException e) { 102 } 103 } 104 } 105 } 106 }; 107 checkingThread.setDaemon(true); 108 checkingThread.start(); 109 } 110 111 115 public void shutdown() { 116 shutdown = true; 117 synchronized (checkingThread) { 120 checkingThread.notify(); 121 } 122 } 123 124 133 private void checkHealth() { 134 for (Socket socket : sockets.keySet()) { 135 Date startDate = sockets.get(socket); 136 if (startDate != null && 137 System.currentTimeMillis() - startDate.getTime() > 138 JiveGlobals.getIntProperty("xmpp.session.sending-limit", 60000)) { 139 if (sockets.get(socket) != null) { 141 try { 143 Log.debug("Closing socket: " + socket + " that started sending data at: " + 144 startDate); 145 socket.close(); 146 } 147 catch (IOException e) { 148 Log.error("Error closing socket", e); 149 } 150 finally { 151 sockets.remove(socket); 153 } 154 } 155 } 156 157 } 158 } 159 } 160 | Popular Tags |