public NetClient createNetClient(NetClientOptions options) { return new NetClientImpl(this, options); }
@Override protected void finalize() throws Throwable { // Make sure this gets cleaned up if there are no more references to it // so as not to leave connections and resources dangling until the system is shutdown // which could make the JVM run out of file handles. close(); super.finalize(); } }
public synchronized NetClient connect(int port, String host, Handler<AsyncResult<NetSocket>> connectHandler) { connect(port, host, null, connectHandler); return this; }
protected void doConnect(SocketAddress remoteAddress, String serverName, Handler<AsyncResult<NetSocket>> connectHandler, int remainingAttempts) { checkClosed(); Objects.requireNonNull(connectHandler, "No null connectHandler accepted"); ContextInternal context = vertx.getOrCreateContext(); bootstrap.channelFactory(vertx.transport().channelFactory(remoteAddress.path() != null)); applyConnectionOptions(remoteAddress.path() != null, bootstrap); if (res.succeeded()) { Channel ch = res.result(); connected(context, ch, connectHandler, remoteAddress); } else { Throwable cause = res.cause(); doConnect(remoteAddress, serverName, connectHandler, remainingAttempts == -1 ? remainingAttempts : remainingAttempts - 1) ); }); } else { failed(context, null, cause, connectHandler);
@Override public NetClient connect(SocketAddress remoteAddress, String serverName, Handler<AsyncResult<NetSocket>> connectHandler) { doConnect(remoteAddress, serverName, connectHandler); return this; }
private void failed(ContextInternal context, Channel ch, Throwable th, Handler<AsyncResult<NetSocket>> connectHandler) { if (ch != null) { ch.close(); } context.executeFromIO(v -> doFailed(connectHandler, th)); }
private void connected(ContextInternal context, Channel ch, Handler<AsyncResult<NetSocket>> connectHandler, SocketAddress remoteAddress) { initChannel(ch.pipeline()); VertxHandler<NetSocketImpl> handler = VertxHandler.create(context, ctx -> new NetSocketImpl(vertx, ctx, remoteAddress, context, sslHelper, metrics)); handler.addHandler(sock -> { socketMap.put(ch, sock); context.executeFromIO(v -> { if (metrics != null) { sock.metric(metrics.connected(sock.remoteAddress(), sock.remoteName())); } sock.registerEventBusHandler(); connectHandler.handle(Future.succeededFuture(sock)); }); }); handler.removeHandler(conn -> { socketMap.remove(ch); }); ch.pipeline().addLast("handler", handler); }
protected void doConnect(SocketAddress remoteAddress, String serverName, Handler<AsyncResult<NetSocket>> connectHandler, int remainingAttempts) { checkClosed(); Objects.requireNonNull(connectHandler, "No null connectHandler accepted"); ContextInternal context = vertx.getOrCreateContext(); bootstrap.channelFactory(vertx.transport().channelFactory(remoteAddress.path() != null)); applyConnectionOptions(remoteAddress.path() != null, bootstrap); if (res.succeeded()) { Channel ch = res.result(); connected(context, ch, connectHandler, remoteAddress); } else { Throwable cause = res.cause(); doConnect(remoteAddress, serverName, connectHandler, remainingAttempts == -1 ? remainingAttempts : remainingAttempts - 1) ); }); } else { failed(context, null, cause, connectHandler);
@Override public NetClient connect(SocketAddress remoteAddress, Handler<AsyncResult<NetSocket>> connectHandler) { doConnect(remoteAddress, null, connectHandler); return this; }
private void failed(ContextInternal context, Channel ch, Throwable th, Handler<AsyncResult<NetSocket>> connectHandler) { if (ch != null) { ch.close(); } context.executeFromIO(v -> doFailed(connectHandler, th)); }
private void connected(ContextInternal context, Channel ch, Handler<AsyncResult<NetSocket>> connectHandler, SocketAddress remoteAddress) { initChannel(ch.pipeline()); VertxHandler<NetSocketImpl> handler = VertxHandler.create(context, ctx -> new NetSocketImpl(vertx, ctx, remoteAddress, context, sslHelper, metrics)); handler.addHandler(sock -> { socketMap.put(ch, sock); context.executeFromIO(v -> { if (metrics != null) { sock.metric(metrics.connected(sock.remoteAddress(), sock.remoteName())); } sock.registerEventBusHandler(); connectHandler.handle(Future.succeededFuture(sock)); }); }); handler.removeHandler(conn -> { socketMap.remove(ch); }); ch.pipeline().addLast("handler", handler); }
ConnectionHolder(ClusteredEventBus eventBus, ServerID serverID, EventBusOptions options) { this.eventBus = eventBus; this.serverID = serverID; this.vertx = eventBus.vertx(); this.metrics = eventBus.getMetrics(); NetClientOptions clientOptions = new NetClientOptions(options.toJson()); ClusteredEventBus.setCertOptions(clientOptions, options.getKeyCertOptions()); ClusteredEventBus.setTrustOptions(clientOptions, options.getTrustOptions()); client = new NetClientImpl(eventBus.vertx(), clientOptions, false); }
protected void doConnect(SocketAddress remoteAddress, String serverName, Handler<AsyncResult<NetSocket>> connectHandler) { doConnect(remoteAddress, serverName, connectHandler, options.getReconnectAttempts()); }
public NetClientImpl(VertxInternal vertx, NetClientOptions options, boolean useCreatingContext) { this.vertx = vertx; this.options = new NetClientOptions(options); this.sslHelper = new SSLHelper(options, options.getKeyCertOptions(), options.getTrustOptions()); this.closeHook = completionHandler -> { NetClientImpl.this.close(); completionHandler.handle(Future.succeededFuture()); }; if (useCreatingContext) { creatingContext = vertx.getContext(); if (creatingContext != null) { creatingContext.addCloseHook(closeHook); } } else { creatingContext = null; } VertxMetrics metrics = vertx.metricsSPI(); this.metrics = metrics != null ? metrics.createNetClientMetrics(options) : null; logEnabled = options.getLogActivity(); idleTimeout = options.getIdleTimeout(); idleTimeoutUnit = options.getIdleTimeoutUnit(); }
public synchronized NetClient connect(int port, String host, Handler<AsyncResult<NetSocket>> connectHandler) { connect(port, host, null, connectHandler); return this; }
public NetClient createNetClient(NetClientOptions options) { return new NetClientImpl(this, options); }
@Override public NetClient connect(int port, String host, String serverName, Handler<AsyncResult<NetSocket>> connectHandler) { doConnect(SocketAddress.inetSocketAddress(port, host), serverName, connectHandler != null ? ar -> connectHandler.handle(ar.map(s -> (NetSocket) s)) : null); return this; }
@Override protected void finalize() throws Throwable { // Make sure this gets cleaned up if there are no more references to it // so as not to leave connections and resources dangling until the system is shutdown // which could make the JVM run out of file handles. close(); super.finalize(); } }
ConnectionHolder(ClusteredEventBus eventBus, ServerID serverID, EventBusOptions options) { this.eventBus = eventBus; this.serverID = serverID; this.vertx = eventBus.vertx(); this.metrics = eventBus.getMetrics(); NetClientOptions clientOptions = new NetClientOptions(options.toJson()); ClusteredEventBus.setCertOptions(clientOptions, options.getKeyCertOptions()); ClusteredEventBus.setTrustOptions(clientOptions, options.getTrustOptions()); client = new NetClientImpl(eventBus.vertx(), clientOptions, false); }
@Override public NetClient connect(SocketAddress remoteAddress, Handler<AsyncResult<NetSocket>> connectHandler) { doConnect(remoteAddress, null, connectHandler); return this; }