1 22 23 package org.continuent.sequoia.driver; 24 25 import java.util.ArrayList ; 26 27 35 public class ConnectionClosingThread extends Thread 36 { 37 38 private static final int WAIT_TIME = 5000; 39 40 private Driver driver; 41 private ArrayList pendingConnectionClosing; 42 43 48 public ConnectionClosingThread(Driver driver) 49 { 50 super("ConnectionClosingThread"); 51 this.driver = driver; 52 this.pendingConnectionClosing = driver.pendingConnectionClosing; 53 driver.connectionClosingThreadisAlive = true; 54 } 55 56 61 public void run() 62 { 63 try 64 { 65 Connection firstConnectionToClose = null; 66 Connection lastConnectionToClose = null; 67 int pendingConnectionSize; 68 ArrayList closingList = new ArrayList (); 69 boolean killed = false; 70 71 while (!killed) 72 { 73 synchronized (pendingConnectionClosing) 74 { 75 pendingConnectionSize = pendingConnectionClosing.size(); 76 if (pendingConnectionSize == 0) 77 break; 78 79 try 80 { 81 firstConnectionToClose = (Connection) pendingConnectionClosing 83 .get(0); 84 lastConnectionToClose = (Connection) pendingConnectionClosing 85 .get(pendingConnectionSize - 1); 86 87 pendingConnectionClosing.wait(WAIT_TIME); 89 } 90 catch (InterruptedException ignore) 91 { 92 } 93 94 pendingConnectionSize = pendingConnectionClosing.size(); 95 if (pendingConnectionSize == 0) 97 break; 98 99 if (firstConnectionToClose == pendingConnectionClosing.get(0)) 101 { if (lastConnectionToClose == (Connection) pendingConnectionClosing 103 .get(pendingConnectionSize - 1)) 104 { closingList.addAll(pendingConnectionClosing); 106 pendingConnectionClosing.clear(); 107 killed = true; } 109 else 110 closingList.add(pendingConnectionClosing.remove(0)); 112 } 113 } 114 115 while (!closingList.isEmpty()) 117 closeConnection((Connection) closingList.remove(0)); 118 } 119 } 120 catch (RuntimeException e) 121 { 122 e.printStackTrace(); 123 } 124 finally 125 { 126 synchronized (pendingConnectionClosing) 127 { 128 driver.connectionClosingThreadisAlive = false; 129 } 130 } 131 } 132 133 138 private void closeConnection(Connection c) 139 { 140 try 141 { 142 if (c.socketOutput != null) 144 { 145 c.reallyClose(); 146 c.socketOutput.flush(); 150 if (c.socketInput != null) 151 { c.socketInput.readBoolean(); 158 c.socketInput.close(); 159 } 160 c.socketOutput.close(); 161 } 162 163 if (c.socket != null) 164 c.socket.close(); 165 } 166 catch (Exception ignore) 167 { 168 } 169 } 170 171 } | Popular Tags |