日期:2014-05-20 浏览次数:20863 次
//创建链接 for ( int j = 0; j < initConn; j++ ) { SockIO socket = createSocket( servers[i] ); if ( socket == null ) { log.error( "++++ failed to create connection to: " + servers[i] + " -- only " + j + " created." ); break; } //加入socket到连接池 这里慢慢谈 addSocketToPool( availPool, servers[i], socket ); if ( log.isDebugEnabled() ) log.debug( "++++ created and added socket: " + socket.toString() + " for host " + servers[i] ); }
SockIO socket = createSocket( servers[i] );
protected static Socket getSocket( String host, int port, int timeout ) throws IOException { SocketChannel sock = SocketChannel.open(); sock.socket().connect( new InetSocketAddress( host, port ), timeout ); return sock.socket(); }
protected void addSocketToPool( Map<String,Map<SockIO,Long>> pool, String host, SockIO socket ) { if ( pool.containsKey( host ) ) { Map<SockIO,Long> sockets = pool.get( host ); if ( sockets != null ) { sockets.put( socket, new Long( System.currentTimeMillis() ) ); return; } } Map<SockIO,Long> sockets = new IdentityHashMap<SockIO,Long>(); sockets.put( socket, new Long( System.currentTimeMillis() ) ); pool.put( host, sockets ); }