1 package org.sapia.ubik.rmi.server.transport.nio.tcp; 2 3 import java.rmi.RemoteException ; 4 import java.util.ArrayList ; 5 import java.util.Hashtable ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import org.sapia.taskman.PeriodicTaskDescriptor; 11 import org.sapia.taskman.Task; 12 import org.sapia.taskman.TaskContext; 13 import org.sapia.ubik.net.Connection; 14 import org.sapia.ubik.net.ConnectionPool; 15 import org.sapia.ubik.net.DefaultClientSocketFactory; 16 import org.sapia.ubik.net.ServerAddress; 17 import org.sapia.ubik.net.TCPAddress; 18 import org.sapia.ubik.rmi.server.Hub; 19 import org.sapia.ubik.rmi.server.transport.Connections; 20 import org.sapia.ubik.rmi.server.transport.RmiConnection; 21 22 40 public class NioTcpClientConnectionPool implements Connections { 41 private static Map _pools = new Hashtable (); 42 private static boolean _started; 43 ConnectionPool _pool; 44 45 53 NioTcpClientConnectionPool(String host, int port, int bufsize) { 54 _pool = new ConnectionPool(host, port, new NioRmiConnectionFactory(bufsize), 55 new DefaultClientSocketFactory()); 56 } 57 58 61 public RmiConnection acquire() throws RemoteException { 62 try { 63 return (RmiConnection) _pool.acquire(); 64 } catch(java.io.IOException e) { 65 throw new RemoteException ("Could not acquire a connection", e); 66 } 67 } 68 69 72 public void release(Connection conn) { 73 _pool.release(conn); 74 } 75 76 79 public void clear() { 80 _pool.shrinkTo(0); 81 } 82 83 86 public String getTransportType() { 87 return NioTcpTransportProvider.TRANSPORT_TYPE; 88 } 89 90 ConnectionPool internalPool() { 91 return _pool; 92 } 93 94 static synchronized void shutdown() { 95 NioTcpClientConnectionPool pool; 96 Iterator pools; 97 98 if(_started) { 99 synchronized(_pools) { 100 pools = _pools.values().iterator(); 101 102 while(pools.hasNext()) { 103 pool = (NioTcpClientConnectionPool) pools.next(); 104 pool.internalPool().shrinkTo(0); 105 } 106 } 107 } 108 } 109 110 static synchronized NioTcpClientConnectionPool getInstance( 111 ServerAddress address, int bufsize) { 112 if(!_started) { 113 _started = true; 114 115 List desc = new ArrayList (); 116 desc.add(new PeriodicTaskDescriptor(NioTcpClientConnectionPool.class 117 .getName(), PoolCleaner.INTERVAL, new PoolCleaner(_pools))); 118 Hub.taskMan.addTaskDescriptors(desc); 119 } 120 121 NioTcpClientConnectionPool pool = (NioTcpClientConnectionPool) _pools 122 .get(address); 123 124 if(pool == null) { 125 pool = new NioTcpClientConnectionPool(((NioAddress) address).getHost(), 126 ((NioAddress) address).getPort(), bufsize); 127 _pools.put(address, pool); 128 } 129 130 return pool; 131 } 132 133 static synchronized void invalidate(TCPAddress id) { 134 _pools.remove(id); 135 } 136 137 141 static final class PoolCleaner implements Task { 142 static final long INTERVAL = 5000; 143 Map _pools; 144 145 PoolCleaner(Map pools) { 146 _pools = pools; 147 } 148 149 152 public void exec(TaskContext context) { 153 NioTcpClientConnectionPool[] pools; 154 155 pools = (NioTcpClientConnectionPool[]) _pools.values().toArray( 156 new NioTcpClientConnectionPool[_pools.size()]); 157 158 for(int i = 0; i < pools.length; i++) { 159 if((System.currentTimeMillis() - pools[i].internalPool() 160 .getLastUsageTime()) > INTERVAL) { 161 context.getTaskOutput().debug( 162 "Shrinking nio socket client connection pool..."); 163 pools[i].internalPool().shrinkTo(0); 164 } 165 } 166 } 167 } 168 } 169 | Popular Tags |