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

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

Introduction

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

Prototype

public NetClientOptions setReconnectAttempts(int attempts) 

Source Link

Document

Set the value of reconnect attempts

Usage

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

License:Apache License

/**
 * {@inheritDoc}//from w  w  w  .  j  av a  2 s. c  om
 */
@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();
    }
}

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

License:Apache License

/**
 * Opens a new transfer client.//from   ww w . j  av a  2s  .c o m
 * 
 * @throws IOException if opening failed
 */
private void openTransfer() throws IOException {
    closeTransfer();

    if (socketTransfer != null) {
        socketTransfer.close();
        socketTransfer = null;
    }

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

    clientTransfer = vertx.createNetClient(options);

    isTransfer = new SyncedInputStream();

    clientTransfer.connect(iPort, sHost, new Handler<AsyncResult<NetSocket>>() {
        public void handle(AsyncResult<NetSocket> pCommunication) {
            NetSocket sock = pCommunication.result();

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

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

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

                });
            }

            synchronized (clientTransfer) {
                socketTransfer = sock;

                clientTransfer.notify();
            }
        }
    });

    try {
        synchronized (clientTransfer) {
            if (socketTransfer == null) {
                clientTransfer.wait(15000);
            }
        }
    } catch (InterruptedException ie) {
        throw new IOException(ie);
    }

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