@Override public CompletableFuture<BenchmarkProducer> createProducer(String topic) { Connection natsProducer; try { Options options = new Options.Builder().server(config.natsHostUrl).maxReconnects(5).build(); natsProducer = Nats.connect(options); } catch (Exception e) { log.error("createProducer excetopin " + e); return null; } return CompletableFuture.completedFuture(new NatsBenchmarkProducer(natsProducer, topic)); }
/** * Options can be used to set the server URL, or multiple URLS, callback * handlers for various errors, and connection events. * * * <p>This is a synchronous call, and the connection should be ready for use on return * there are network timing issues that could result in a successful connect call but * the connection is invalid soon after return, where soon is in the network/thread world. * * <p>If the connection fails, an IOException is thrown * * @param options the options object to use to create the connection * @throws IOException if a networking issue occurs * @throws InterruptedException if the current thread is interrupted * @return the connection */ public static Connection connect(Options options) throws IOException, InterruptedException { return createConnection(options, false); }
@Test public void testSeparateWrappedFiles() throws Exception { AuthHandler auth = Nats.credentials("src/test/resources/jwt_nkey/test_wrapped.jwt", "src/test/resources/jwt_nkey/test_wrapped.nk"); NKey key = NKey.fromSeed(SEED.toCharArray()); byte[] test = "hello world again".getBytes(StandardCharsets.UTF_8); char[] pubKey = auth.getID(); assertArrayEquals(key.getPublicKey(), pubKey); assertArrayEquals(key.sign(test), auth.sign(test)); assertArrayEquals(JWT.toCharArray(), auth.getJWT()); }
@Test public void testJWTAuthWithChainFile() throws Exception { NKey theKey = NKey.createUser(null); assertNotNull(theKey); String version = NatsTestServer.generateGnatsdVersionString(); if (!version.contains("version 2")) { // Server version doesn't support this test return; } try (NatsTestServer ts = new NatsTestServer("src/test/resources/operator.conf", true)) { Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). authHandler(Nats.credentials("src/test/resources/jwt_nkey/user.chain")). build(); Connection nc = Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnAsyncWithoutListener() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false)) { Options options = new Options.Builder(). server(ts.getURI()). build(); Nats.connectAsynchronously(options, false); } }
Nats.connectAsynchronously(options, true);
private Connection initConnection() throws IOException { Options options = new Options.Builder() .errorCb(this::connectionError) .disconnectedCb(this::disconnected) .reconnectedCb(this::reconnected) .build(); return Nats.connect(this.server, options); }
/** * Options can be used to set the server URL, or multiple URLS, callback * handlers for various errors, and connection events. * * * <p>This is a synchronous call, and the connection should be ready for use on return * there are network timing issues that could result in a successful connect call but * the connection is invalid soon after return, where soon is in the network/thread world. * * <p>If the connection fails, an IOException is thrown * * @param options the options object to use to create the connection * @throws IOException if a networking issue occurs * @throws InterruptedException if the current thread is interrupted * @return the connection */ public static Connection connect(Options options) throws IOException, InterruptedException { return createConnection(options, false); }
@Test public void testErrorOnAsync() throws IOException, InterruptedException { TestHandler handler = new TestHandler(); Options options = new Options.Builder(). server("nats://localhost:"+NatsTestServer.nextPort()). connectionListener(handler). errorListener(handler). noReconnect(). build(); handler.prepForStatusChange(Events.CLOSED); Nats.connectAsynchronously(options, false); handler.waitForStatusChange(10, TimeUnit.SECONDS); assertTrue(1 <= handler.getExceptionCount()); assertTrue(handler.getConnection() == null || Connection.Status.CLOSED == handler.getConnection().getStatus()); } }
@Test public void testChainFile() throws Exception { AuthHandler auth = Nats.credentials("src/test/resources/jwt_nkey/test.chain"); NKey key = NKey.fromSeed(SEED.toCharArray()); byte[] test = "hello world".getBytes(StandardCharsets.UTF_8); char[] pubKey = auth.getID(); assertArrayEquals(key.getPublicKey(), pubKey); assertArrayEquals(key.sign(test), auth.sign(test)); assertArrayEquals(JWT.toCharArray(), auth.getJWT()); }
io.nats.client.Connection createNatsConnection() throws IOException, InterruptedException { io.nats.client.Connection nc = null; if (getNatsConnection() == null) { if (opts.getNatsUrl() != null) { io.nats.client.Options natsOpts = new io.nats.client.Options.Builder(). connectionName(clientId). errorListener(opts.getErrorListener()). connectionListener(opts.getConnectionListener()). server(opts.getNatsUrl()). build(); nc = Nats.connect(natsOpts); } else { nc = Nats.connect(); } ncOwned = true; } return nc; }
/** * Connect to the default URL, {@link Options#DEFAULT_URL Options.DEFAULT_URL}, with all of the * default options. * * <p>This is a synchronous call, and the connection should be ready for use on return * there are network timing issues that could result in a successful connect call but * the connection is invalid soon after return, where soon is in the network/thread world. * * <p>If the connection fails, an IOException is thrown * * @throws IOException if a networking issue occurs * @throws InterruptedException if the current thread is interrupted * @return the connection */ public static Connection connect() throws IOException, InterruptedException { Options options = new Options.Builder().server(Options.DEFAULT_URL).build(); return createConnection(options, false); }
@Test public void testAsyncConnection() throws IOException, InterruptedException { TestHandler handler = new TestHandler(); Connection nc = null; try (NatsTestServer ts = new NatsTestServer(false)) { Options options = new Options.Builder(). server(ts.getURI()). connectionListener(handler). build(); handler.prepForStatusChange(Events.CONNECTED); Nats.connectAsynchronously(options, false); handler.waitForStatusChange(1, TimeUnit.SECONDS); try { nc = handler.getConnection(); assertNotNull(nc); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); } finally { if (nc != null) { nc.close(); } assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
@Test public void testSeparateBareFiles() throws Exception { AuthHandler auth = Nats.credentials("src/test/resources/jwt_nkey/test.jwt", "src/test/resources/jwt_nkey/test.nk"); NKey key = NKey.fromSeed(SEED.toCharArray()); byte[] test = "hello world and again".getBytes(StandardCharsets.UTF_8); char[] pubKey = auth.getID(); assertArrayEquals(key.getPublicKey(), pubKey); assertArrayEquals(key.sign(test), auth.sign(test)); assertArrayEquals(JWT.toCharArray(), auth.getJWT()); } }
@Test(expected = IllegalStateException.class) public void testUnsubscribeInAnotherThread() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Subscription sub = nc.subscribe("subject"); Thread t = new Thread(() -> { sub.unsubscribe(); }); t.start(); sub.nextMessage(Duration.ofMillis(5000)); // throw assertFalse(true); } }
/** * Connect to the default URL, {@link Options#DEFAULT_URL Options.DEFAULT_URL}, with all of the * default options. * * <p>This is a synchronous call, and the connection should be ready for use on return * there are network timing issues that could result in a successful connect call but * the connection is invalid soon after return, where soon is in the network/thread world. * * <p>If the connection fails, an IOException is thrown * * @throws IOException if a networking issue occurs * @throws InterruptedException if the current thread is interrupted * @return the connection */ public static Connection connect() throws IOException, InterruptedException { Options options = new Options.Builder().server(Options.DEFAULT_URL).build(); return createConnection(options, false); }
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptyQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.subscribe("subject", ""); assertFalse(true); } }
/** * The Java client generally expects URLs of the form {@code nats://hostname:port} * * <p>but also allows urls with a user password {@code nats://user:pass@hostname:port}. * * <p>or token in them {@code nats://token@hostname:port}. * * <p>Moreover, you can initiate a TLS connection, by using the `tls` * schema, which will use the default SSLContext, or fail if one is not set. For * testing and development, the `opentls` schema is support when the server is * in non-verify mode. In this case, the client will accept any server * certificate and will not provide one of its own. * * <p>This is a synchronous call, and the connection should be ready for use on return * there are network timing issues that could result in a successful connect call but * the connection is invalid soon after return, where soon is in the network/thread world. * * <p>If the connection fails, an IOException is thrown * * @param url the url of the server, ie. nats://localhost:4222 * @throws IOException if a networking issue occurs * @throws InterruptedException if the current thread is interrupted * @return the connection */ public static Connection connect(String url) throws IOException, InterruptedException { Options options = new Options.Builder().server(url).build(); return createConnection(options, false); }
@Test(expected=IllegalArgumentException.class) public void testThrowOnNullSubjectWithQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.subscribe(null, "quque"); assertFalse(true); } }
/** * The Java client generally expects URLs of the form {@code nats://hostname:port} * * <p>but also allows urls with a user password {@code nats://user:pass@hostname:port}. * * <p>or token in them {@code nats://token@hostname:port}. * * <p>Moreover, you can initiate a TLS connection, by using the `tls` * schema, which will use the default SSLContext, or fail if one is not set. For * testing and development, the `opentls` schema is support when the server is * in non-verify mode. In this case, the client will accept any server * certificate and will not provide one of its own. * * <p>This is a synchronous call, and the connection should be ready for use on return * there are network timing issues that could result in a successful connect call but * the connection is invalid soon after return, where soon is in the network/thread world. * * <p>If the connection fails, an IOException is thrown * * @param url the url of the server, ie. nats://localhost:4222 * @throws IOException if a networking issue occurs * @throws InterruptedException if the current thread is interrupted * @return the connection */ public static Connection connect(String url) throws IOException, InterruptedException { Options options = new Options.Builder().server(url).build(); return createConnection(options, false); }