Example usage for io.vertx.core.net NetClientOptions setConnectTimeout

List of usage examples for io.vertx.core.net NetClientOptions setConnectTimeout

Introduction

In this page you can find the example usage for io.vertx.core.net NetClientOptions setConnectTimeout.

Prototype

@Override
    public NetClientOptions setConnectTimeout(int connectTimeout) 

Source Link

Usage

From source file:com.sibvisions.rad.remote.vertx.NetSocketConnection.java

License:Apache License

/**
 * {@inheritDoc}/*ww w .  j av  a2s .com*/
 */
@Override
public void open(ConnectionInfo pConnectionInfo) throws Throwable {
    closeSocket();

    NetClientOptions options = new NetClientOptions();
    options.setReconnectAttempts(3);
    options.setConnectTimeout(5000);
    options.setReconnectInterval(1000);

    client = vertx.createNetClient(options);
    client.connect(iPort, sHost, new Handler<AsyncResult<NetSocket>>() {
        public void handle(final AsyncResult<NetSocket> pCommunication) {
            NetSocket sock = pCommunication.result();

            if (sock != null) {
                sock.handler(new Handler<Buffer>() {
                    public void handle(Buffer pBuffer) {
                        if (inputStream != null) {
                            inputStream.receive(pBuffer);
                        }
                    }
                });

                sock.exceptionHandler(new Handler<Throwable>() {
                    public void handle(Throwable pException) {
                        if (inputStream != null) {
                            inputStream.finish();
                        }
                    }
                });

                sock.endHandler(new Handler<Void>() {
                    public void handle(Void pParam) {
                        if (inputStream != null) {
                            inputStream.finish();
                        }
                    }
                });
            }

            synchronized (NetSocketConnection.this) {
                if (pCommunication.succeeded()) {
                    socket = sock;
                } else {
                    LoggerFactory.getInstance(NetSocketConnection.class).error(pCommunication.cause());

                    socket = null;
                }

                NetSocketConnection.this.notify();
            }
        }
    });

    synchronized (this) {
        if (socket == null) {
            wait(15000);
        }
    }

    if (socket == null) {
        throw new ConnectException("Can't establish connection!");
    }

    socket.write(Buffer.buffer(new byte[] { STREAM_COMMUNICATION }));

    super.open(pConnectionInfo);

    if (oInitialConId == null) {
        oInitialConId = pConnectionInfo.getConnectionId();
    }
}