public void connect(String serverURI, NatsConnection conn) throws IOException { try { this.connection = conn; Options options = this.connection.getOptions(); long timeout = options.getConnectionTimeout().toMillis(); URI uri = options.createURIForServer(serverURI); this.host = uri.getHost(); this.port = uri.getPort(); this.socket = new Socket(); socket.connect(new InetSocketAddress(host, port), (int) timeout); in = socket.getInputStream(); out = socket.getOutputStream(); } catch (Exception ex) { throw new IOException(ex); } }
public void connect(String serverURI, NatsConnection conn) throws IOException { try { this.connection = conn; Options options = this.connection.getOptions(); long timeout = options.getConnectionTimeout().toMillis(); URI uri = options.createURIForServer(serverURI); this.host = uri.getHost(); this.port = uri.getPort(); this.socket = new Socket(); socket.connect(new InetSocketAddress(host, port), (int) timeout); in = socket.getInputStream(); out = socket.getOutputStream(); } catch (Exception ex) { throw new IOException(ex); } }
/** * Upgrade the port to SSL. If it is already secured, this is a no-op. * If the data port type doesn't support SSL it should throw an exception. */ public void upgradeToSecure() throws IOException { Options options = this.connection.getOptions(); SSLContext context = options.getSslContext(); SSLSocketFactory factory = context.getSocketFactory(); Duration timeout = options.getConnectionTimeout(); this.sslSocket = (SSLSocket) factory.createSocket(socket, null, true); this.sslSocket.setUseClientMode(true); final CompletableFuture<Void> waitForHandshake = new CompletableFuture<>(); this.sslSocket.addHandshakeCompletedListener((evt) -> { waitForHandshake.complete(null); }); this.sslSocket.startHandshake(); try { waitForHandshake.get(timeout.toNanos(), TimeUnit.NANOSECONDS); } catch (Exception ex) { this.connection.handleCommunicationIssue(ex); } in = sslSocket.getInputStream(); out = sslSocket.getOutputStream(); }
try { if (checkDrainStatus && this.isDraining()) { waitForDisconnectOrClose(this.options.getConnectionTimeout()); return; waitForDisconnectOrClose(this.options.getConnectionTimeout()); return; } else { callbackRunner.awaitTermination(this.options.getConnectionTimeout().toNanos(), TimeUnit.NANOSECONDS); } finally { callbackRunner.shutdownNow();
/** * Upgrade the port to SSL. If it is already secured, this is a no-op. * If the data port type doesn't support SSL it should throw an exception. */ public void upgradeToSecure() throws IOException { Options options = this.connection.getOptions(); SSLContext context = options.getSslContext(); SSLSocketFactory factory = context.getSocketFactory(); Duration timeout = options.getConnectionTimeout(); this.sslSocket = (SSLSocket) factory.createSocket(socket, null, true); this.sslSocket.setUseClientMode(true); final CompletableFuture<Void> waitForHandshake = new CompletableFuture<>(); this.sslSocket.addHandshakeCompletedListener((evt) -> { waitForHandshake.complete(null); }); this.sslSocket.startHandshake(); try { waitForHandshake.get(timeout.toNanos(), TimeUnit.NANOSECONDS); } catch (Exception ex) { this.connection.handleCommunicationIssue(ex); } in = sslSocket.getInputStream(); out = sslSocket.getOutputStream(); }
try { if (checkDrainStatus && this.isDraining()) { waitForDisconnectOrClose(this.options.getConnectionTimeout()); return; waitForDisconnectOrClose(this.options.getConnectionTimeout()); return; } else { callbackRunner.awaitTermination(this.options.getConnectionTimeout().toNanos(), TimeUnit.NANOSECONDS); } finally { callbackRunner.shutdownNow();
try { if (isDisconnectingOrClosed()) { waitForDisconnectOrClose(this.options.getConnectionTimeout()); return; } else {
try { if (isDisconnectingOrClosed()) { waitForDisconnectOrClose(this.options.getConnectionTimeout()); return; } else {
Duration connectTimeout = options.getConnectionTimeout();
Duration connectTimeout = options.getConnectionTimeout();
@Test public void testPropertyDurationOptions() { Properties props = new Properties(); props.setProperty(Options.PROP_RECONNECT_WAIT, "101"); props.setProperty(Options.PROP_CONNECTION_TIMEOUT, "202"); props.setProperty(Options.PROP_PING_INTERVAL, "303"); props.setProperty(Options.PROP_CLEANUP_INTERVAL, "404"); Options o = new Options.Builder(props).build(); assertEquals("default verbose", false, o.isVerbose()); // One from a different type assertEquals("property reconnect wait", Duration.ofMillis(101), o.getReconnectWait()); assertEquals("property connection timeout", Duration.ofMillis(202), o.getConnectionTimeout()); assertEquals("property ping interval", Duration.ofMillis(303), o.getPingInterval()); assertEquals("property cleanup interval", Duration.ofMillis(404), o.getRequestCleanupInterval()); }
@Test public void testChainedDurationOptions() { Options o = new Options.Builder().reconnectWait(Duration.ofMillis(101)) .connectionTimeout(Duration.ofMillis(202)).pingInterval(Duration.ofMillis(303)) .requestCleanupInterval(Duration.ofMillis(404)).build(); assertEquals("default verbose", false, o.isVerbose()); // One from a different type assertEquals("chained reconnect wait", Duration.ofMillis(101), o.getReconnectWait()); assertEquals("chained connection timeout", Duration.ofMillis(202), o.getConnectionTimeout()); assertEquals("chained ping interval", Duration.ofMillis(303), o.getPingInterval()); assertEquals("chained cleanup interval", Duration.ofMillis(404), o.getRequestCleanupInterval()); }
@Test public void testDefaultPropertyIntOptions() { Properties props = new Properties(); props.setProperty(Options.PROP_RECONNECT_WAIT, "-1"); props.setProperty(Options.PROP_CONNECTION_TIMEOUT, "-1"); props.setProperty(Options.PROP_PING_INTERVAL, "-1"); props.setProperty(Options.PROP_CLEANUP_INTERVAL, "-1"); props.setProperty(Options.PROP_MAX_CONTROL_LINE, "-1"); Options o = new Options.Builder(props).build(); assertEquals("default max control line", Options.DEFAULT_MAX_CONTROL_LINE, o.getMaxControlLine()); assertEquals("default reconnect wait", Options.DEFAULT_RECONNECT_WAIT, o.getReconnectWait()); assertEquals("default connection timeout", Options.DEFAULT_CONNECTION_TIMEOUT, o.getConnectionTimeout()); assertEquals("default ping interval", Options.DEFAULT_PING_INTERVAL, o.getPingInterval()); assertEquals("default cleanup interval", Options.DEFAULT_REQUEST_CLEANUP_INTERVAL, o.getRequestCleanupInterval()); }
this.flush(this.options.getConnectionTimeout()); } catch (Exception exp) { this.processException(exp);
this.flush(this.options.getConnectionTimeout()); } catch (Exception exp) { this.processException(exp);
@Test public void testDefaultOptions() { Options o = new Options.Builder().build(); assertEquals("default one server", 1, o.getServers().size()); assertEquals("default url", Options.DEFAULT_URL, o.getServers().toArray()[0].toString()); assertEquals("default data port type", Options.DEFAULT_DATA_PORT_TYPE, o.getDataPortType()); assertEquals("default verbose", false, o.isVerbose()); assertEquals("default pedantic", false, o.isPedantic()); assertEquals("default norandomize", false, o.isNoRandomize()); assertEquals("default oldstyle", false, o.isOldRequestStyle()); assertEquals("default noEcho", false, o.isNoEcho()); assertEquals("default UTF8 Support", false, o.supportUTF8Subjects()); assertNull("default username", o.getUsername()); assertNull("default password", o.getPassword()); assertNull("default token", o.getToken()); assertNull("default connection name", o.getConnectionName()); assertNull("default ssl context", o.getSslContext()); assertEquals("default max reconnect", Options.DEFAULT_MAX_RECONNECT, o.getMaxReconnect()); assertEquals("default ping max", Options.DEFAULT_MAX_PINGS_OUT, o.getMaxPingsOut()); assertEquals("default reconnect buffer size", Options.DEFAULT_RECONNECT_BUF_SIZE, o.getReconnectBufferSize()); assertEquals("default reconnect wait", Options.DEFAULT_RECONNECT_WAIT, o.getReconnectWait()); assertEquals("default connection timeout", Options.DEFAULT_CONNECTION_TIMEOUT, o.getConnectionTimeout()); assertEquals("default ping interval", Options.DEFAULT_PING_INTERVAL, o.getPingInterval()); assertEquals("default cleanup interval", Options.DEFAULT_REQUEST_CLEANUP_INTERVAL, o.getRequestCleanupInterval()); assertNull("error handler", o.getErrorListener()); assertNull("disconnect handler", o.getConnectionListener()); }