Search in sources :

Example 1 with ConnectFuture

use of org.apache.mina.common.ConnectFuture in project camel by apache.

the class MinaProducer method openConnection.

private void openConnection() {
    SocketAddress address = getEndpoint().getAddress();
    connector = getEndpoint().getConnector();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating connector to address: {} using connector: {} timeout: {} millis.", new Object[] { address, connector, timeout });
    }
    IoHandler ioHandler = new ResponseHandler(getEndpoint());
    // connect and wait until the connection is established
    ConnectFuture future = connector.connect(address, ioHandler, getEndpoint().getConnectorConfig());
    future.join();
    session = future.getSession();
}
Also used : ConnectFuture(org.apache.mina.common.ConnectFuture) SocketAddress(java.net.SocketAddress) IoHandler(org.apache.mina.common.IoHandler)

Example 2 with ConnectFuture

use of org.apache.mina.common.ConnectFuture in project camel by apache.

the class MinaConsumer method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    LOG.info("Binding to server address: {} using acceptor: {}", address, acceptor);
    IoHandler handler = new ReceiveHandler();
    if (protocol.equals("tcp") && clientMode) {
        ConnectFuture future = connector.connect(address, handler, getEndpoint().getConnectorConfig());
        future.join();
        session = future.getSession();
    } else {
        acceptor.bind(address, handler, getEndpoint().getAcceptorConfig());
    }
}
Also used : ConnectFuture(org.apache.mina.common.ConnectFuture) IoHandler(org.apache.mina.common.IoHandler)

Example 3 with ConnectFuture

use of org.apache.mina.common.ConnectFuture in project dubbo by alibaba.

the class MinaClient method doConnect.

@Override
protected void doConnect() throws Throwable {
    ConnectFuture future = connector.connect(getConnectAddress(), new MinaHandler(getUrl(), this));
    long start = System.currentTimeMillis();
    final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
    // resolve future.awaitUninterruptibly() dead lock
    final CountDownLatch finish = new CountDownLatch(1);
    future.addListener(new IoFutureListener() {

        public void operationComplete(IoFuture future) {
            try {
                if (future.isReady()) {
                    IoSession newSession = future.getSession();
                    try {
                        // 关闭旧的连接
                        // copy reference
                        IoSession oldSession = MinaClient.this.session;
                        if (oldSession != null) {
                            try {
                                if (logger.isInfoEnabled()) {
                                    logger.info("Close old mina channel " + oldSession + " on create new mina channel " + newSession);
                                }
                                oldSession.close();
                            } finally {
                                MinaChannel.removeChannelIfDisconnectd(oldSession);
                            }
                        }
                    } finally {
                        if (MinaClient.this.isClosed()) {
                            try {
                                if (logger.isInfoEnabled()) {
                                    logger.info("Close new mina channel " + newSession + ", because the client closed.");
                                }
                                newSession.close();
                            } finally {
                                MinaClient.this.session = null;
                                MinaChannel.removeChannelIfDisconnectd(newSession);
                            }
                        } else {
                            MinaClient.this.session = newSession;
                        }
                    }
                }
            } catch (Exception e) {
                exception.set(e);
            } finally {
                finish.countDown();
            }
        }
    });
    try {
        finish.await(getTimeout(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server " + getRemoteAddress() + " client-side timeout " + getTimeout() + "ms (elapsed: " + (System.currentTimeMillis() - start) + "ms) from netty client " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", cause: " + e.getMessage(), e);
    }
    Throwable e = exception.get();
    if (e != null) {
        throw e;
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IoFuture(org.apache.mina.common.IoFuture) ConnectFuture(org.apache.mina.common.ConnectFuture) CountDownLatch(java.util.concurrent.CountDownLatch) RemotingException(com.alibaba.dubbo.remoting.RemotingException) IoFutureListener(org.apache.mina.common.IoFutureListener) RemotingException(com.alibaba.dubbo.remoting.RemotingException) IoSession(org.apache.mina.common.IoSession)

Aggregations

ConnectFuture (org.apache.mina.common.ConnectFuture)3 IoHandler (org.apache.mina.common.IoHandler)2 RemotingException (com.alibaba.dubbo.remoting.RemotingException)1 SocketAddress (java.net.SocketAddress)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 IoFuture (org.apache.mina.common.IoFuture)1 IoFutureListener (org.apache.mina.common.IoFutureListener)1 IoSession (org.apache.mina.common.IoSession)1