@Override public boolean awaitUninterruptibly(long timeoutMillis) { return connectFuture.awaitUninterruptibly(timeoutMillis); }
@Override public boolean awaitUninterruptibly(long timeoutMillis) { return connectFuture.awaitUninterruptibly(timeoutMillis); }
connector.setHandler(this); ConnectFuture future = connector.connect(forward); future.awaitUninterruptibly(); // wait for connect, or timeout if (future.isConnected()) { if (log.isDebugEnabled()) {
@Override public boolean awaitUninterruptibly(long timeoutMillis) { return connectFuture.awaitUninterruptibly(timeoutMillis); }
/** * 获取连接 * * @author 唐延波 * @date 2015-1-15 * @param result * @return * @throws InterruptedException */ @Override public ConnectFuture getConnection() throws InterruptedException { // 连接服务端 ConnectFuture connection = connector.connect(new InetSocketAddress(host, port)); // 等待建立连接 connection.awaitUninterruptibly(); return connection; }
/** * 初始化连接 * @param size * @author 唐延波 * @date 2015-6-24 */ private void initConnection(int size) { for (int i = 0; i < size; i++) { // 连接服务端 ConnectFuture connection = connector.connect(new InetSocketAddress( host, port)); // 等待建立连接 connection.awaitUninterruptibly(); try { idlePool.put(connection); } catch (InterruptedException e) { e.printStackTrace(); } } }
private MinaQueueStorageEndpoint connect() { while (isRunning() && !connected) { try { ConnectFuture future = connector.connect(address); future.awaitUninterruptibly(); session = future.getSession(); connected = true; logger.info("Successfully connect to " + address); } catch (Throwable t) { connected = false; logger.error("Couldn't connect to " + address + " : "+ t.getMessage() + ", retrying in " + recoveryInterval + " ms", t); } sleepInbetweenRecoveryAttempts(); } return this; }
/** * 链接服务器 * * @return */ public boolean connect() { //停止重连 if (mConnection == null) { return true; } try { ConnectFuture future = mConnection.connect(); future.awaitUninterruptibly(); mSession = future.getSession(); } catch (Exception e) { return false; } return mSession == null ? false : true; }
/** * 初始化连接 * @param size * @author 唐延波 * @date 2015-6-24 */ private void initConnection(int size) { for (int i = 0; i < size; i++) { // 连接服务端 ConnectFuture connection = connector.connect(new InetSocketAddress( host, port)); // 等待建立连接 connection.awaitUninterruptibly(); IoSession session = connection.getSession(); //放入counter session.setAttribute(Result.COUNTER, new AtomicLong()); //放入map ConcurrentHashMap<Long, Result> resultMap = new ConcurrentHashMap<Long, Result>(100,0.75f,16); session.setAttribute(Result.RESULT_MAP, resultMap); connectionPool.add(connection); } }
private void pollConnectFuture() { try { connectFuture.awaitUninterruptibly(CONNECT_POLL_TIMEOUT); if (connectFuture.getSession() != null) { ioSession = connectFuture.getSession(); connectionFailureCount = 0; lastConnectTime = System.currentTimeMillis(); connectFuture = null; } else { fixSession.getLog().onEvent( "Pending connection not established after " + (System.currentTimeMillis() - lastReconnectAttemptTime) + " ms."); } } catch (Throwable e) { handleConnectException(e); } }
private void pollConnectFuture() { try { connectFuture.awaitUninterruptibly(CONNECT_POLL_TIMEOUT); if (connectFuture.getSession() != null) { ioSession = connectFuture.getSession(); connectionFailureCount = 0; nextSocketAddressIndex = 0; lastConnectTime = System.currentTimeMillis(); connectFuture = null; } else { fixSession.getLog().onEvent( "Pending connection not established after " + (System.currentTimeMillis() - lastReconnectAttemptTime) + " ms."); } } catch (Throwable e) { handleConnectException(e); } }
private void pollConnectFuture() { try { connectFuture.awaitUninterruptibly(CONNECT_POLL_TIMEOUT); if (connectFuture.getSession() != null) { ioSession = connectFuture.getSession(); connectionFailureCount = 0; nextSocketAddressIndex = 0; lastConnectTime = System.currentTimeMillis(); connectFuture = null; } else { fixSession.getLog().onEvent( "Pending connection not established after " + (System.currentTimeMillis() - lastReconnectAttemptTime) + " ms."); } } catch (Throwable e) { handleConnectException(e); } }
/** * 连接 * @return */ public boolean connect() { if (connector!=null && connector.isActive() && connectFuture!=null && connectFuture.isConnected() && ioSession!=null && ioSession.isConnected()) { return true; } try { connectFuture=connector.connect(new InetSocketAddress(Params.HOSTNAME, Params.PORT)); //等待是否连接成功,相当于是转异步执行为同步执行。 connectFuture.awaitUninterruptibly(); //连接成功后获取会话对象。如果没有上面的等待,由于connect()方法是异步的,session 可能会无法获取。 ioSession=connectFuture.getSession(); sendMessage("Hello Client"); return true; } catch (Exception e) { e.printStackTrace(); Log.d("PushManager", "服务器与客户端连接异常..."); } return false; }
/** {@inheritDoc} */ @Override protected void startConnector(String server, int port) { socketConnector = new NioSocketConnector(); socketConnector.setHandler(ioHandler); future = socketConnector.connect(new InetSocketAddress(server, port)); future.addListener(new IoFutureListener<ConnectFuture>() { @Override public void operationComplete(ConnectFuture future) { try { // will throw RuntimeException after connection error session = future.getSession(); } catch (Throwable e) { socketConnector.dispose(false); // if there isn't an ClientExceptionHandler set, a RuntimeException may be thrown in handleException handleException(e); } } }); // Now wait for the connect to be completed future.awaitUninterruptibly(CONNECTOR_WORKER_TIMEOUT); }
public void connect() { ConnectFuture connectFuture = connector.connect(new InetSocketAddress(host, port)); boolean successed = connectFuture.awaitUninterruptibly().isConnected(); if(!successed ) throw new RuntimeIoException("connected failed:unreachable " + host + " " + port); try { session = connectFuture.getSession(); if(session == null) throw new RuntimeIoException("create session failed:unreachable " + host + " " + port); } catch (RuntimeIoException e) { throw e; // } }
/** * Signals that the handshake has finished. */ protected final void setHandshakeComplete() { synchronized (this) { handshakeComplete = true; } ProxyIoSession proxyIoSession = getProxyIoSession(); proxyIoSession.getConnector().fireConnected(proxyIoSession.getSession()).awaitUninterruptibly(); LOGGER.debug(" handshake completed"); // Connected OK try { proxyIoSession.getEventQueue().flushPendingSessionEvents(); flushPendingWriteRequests(); } catch (Exception ex) { LOGGER.error("Unable to flush pending write requests", ex); } }
/** * Signals that the handshake has finished. */ protected final void setHandshakeComplete() { synchronized (this) { handshakeComplete = true; } ProxyIoSession proxyIoSession = getProxyIoSession(); proxyIoSession.getConnector().fireConnected(proxyIoSession.getSession()).awaitUninterruptibly(); LOGGER.debug(" handshake completed"); // Connected OK try { proxyIoSession.getEventQueue().flushPendingSessionEvents(); flushPendingWriteRequests(); } catch (Exception ex) { LOGGER.error("Unable to flush pending write requests", ex); } }
/** * 连接服务器 */ public void connect() { if (getMinaClientConfig() != null) { log.warn("开始链接其他服务器,共 [" + getMinaClientConfig().getMaxConnectCount() + "] 个"); MinaClientConfig.MinaClienConnToConfig connTo = getMinaClientConfig().getConnTo(); if (connTo == null) { log.warn("没有连接配置"); return; } for (int i = 0; i < getMinaClientConfig().getMaxConnectCount(); i++) { ConnectFuture connect = connector.connect(new InetSocketAddress(connTo.getHost(), connTo.getPort())); connect.awaitUninterruptibly(10000L); if (!connect.isConnected()) { log.warn("失败!连接到服务器:" + connTo); break; } else { log.warn("成功!连接到服务器:" + connTo); if (sessionCreateCallBack != null) { sessionCreateCallBack.accept(getMinaClientConfig()); } } } } else { log.warn("连接配置为null"); } }
/** * Signals that the handshake has finished. */ protected final void setHandshakeComplete() { synchronized (this) { handshakeComplete = true; } ProxyIoSession proxyIoSession = getProxyIoSession(); proxyIoSession.getConnector() .fireConnected(proxyIoSession.getSession()) .awaitUninterruptibly(); LOGGER.debug(" handshake completed"); // Connected OK try { proxyIoSession.getEventQueue().flushPendingSessionEvents(); flushPendingWriteRequests(); } catch (Exception ex) { LOGGER.error("Unable to flush pending write requests", ex); } }
/** * Send message to server. */ public boolean send(Packet packet) { ConnectFuture future = this.connector.connect(new InetSocketAddress( this.ipAddress, this.port)); try { byte[] msg = SerializationUtils.serialize(packet); future.awaitUninterruptibly(); future.getSession().write(msg); return true; } catch (RuntimeIoException e) { e.printStackTrace(); if (e.getCause() instanceof ConnectException) { try { if (future.isConnected()) { future.getSession().close(); } } catch (RuntimeIoException e1) { e1.printStackTrace(); } } } return false; }