io.nats.client.Nats Java Examples
The following examples show how to use
io.nats.client.Nats.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConnectionListenerTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testExceptionInConnectionListener() throws Exception { try (NatsTestServer ts = new NatsTestServer(false)) { BadHandler handler = new BadHandler(); Options options = new Options.Builder(). server(ts.getURI()). connectionListener(handler). 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()); } assertTrue(((NatsConnection)nc).getNatsStatistics().getExceptions() > 0); } }
Example #2
Source File: TLSConnectTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test(expected=IOException.class) public void testDisconnectOnUpgrade() throws Exception { Connection nc = null; try (NatsTestServer ts = new NatsTestServer("src/test/resources/tlsverify.conf", false)) { SSLContext ctx = TestSSLUtils.createTestSSLContext(); Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). dataPortType(CloseOnUpgradeAttempt.class.getCanonicalName()). sslContext(ctx). build(); try { nc = Nats.connect(options); } finally { if (nc != null) { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } } }
Example #3
Source File: RequestTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testRequireCleanupOnCancel() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false)) { Options options = new Options.Builder().server(ts.getURI()).requestCleanupInterval(Duration.ofHours(1)).build(); Connection nc = Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Future<Message> incoming = nc.request("subject", null); incoming.cancel(true); assertEquals(1, ((NatsStatistics)nc.getStatistics()).getOutstandingRequests()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
Example #4
Source File: TLSConnectTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testURISchemeTLSConnection() throws Exception { try (NatsTestServer ts = new NatsTestServer("src/test/resources/tlsverify.conf", false)) { Options options = new Options.Builder(). server("tls://localhost:"+ts.getPort()). sslContext(TestSSLUtils.createTestSSLContext()). // override the custom one maxReconnects(0). 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()); } } }
Example #5
Source File: ConnectTests.java From stan.java with Apache License 2.0 | 6 votes |
@Test public void testNatsConnNotClosedOnClose() throws Exception { try (NatsStreamingTestServer srv = new NatsStreamingTestServer(clusterName, false)) { try (io.nats.client.Connection nc = Nats.connect(srv.getURI())) { // Pass this NATS connection to NATS Streaming StreamingConnection sc = NatsStreaming.connect(clusterName, clientName, new Options.Builder().natsConn(nc).build()); assertNotNull(sc); // Now close the NATS Streaming connection sc.close(); // Verify that NATS connection is not closed assertFalse("NATS connection should NOT have been closed in Connect", nc.getStatus() == Status.CLOSED); } // nc } // srv }
Example #6
Source File: TLSConnectTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test(expected=IOException.class) public void testClientSecureServerNotMismatch() throws Exception { Connection nc = null; try (NatsTestServer ts = new NatsTestServer()) { SSLContext ctx = TestSSLUtils.createTestSSLContext(); Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). sslContext(ctx). build(); try { nc = Nats.connect(options); } finally { if (nc != null) { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } } }
Example #7
Source File: SharedNatsConnectionTests.java From stan.java with Apache License 2.0 | 6 votes |
@Test public void testSharedOnConnect() throws Exception { try (NatsStreamingTestServer srv = new NatsStreamingTestServer(clusterName, false)) { io.nats.client.Options options = new io.nats.client.Options.Builder().server(srv.getURI()).maxReconnects(0).build(); try (Connection nc = Nats.connect(options)){ Options streamingOptions = new Options.Builder().natsConn(nc).build(); StreamingConnection one = NatsStreaming.connect(clusterName, "one", streamingOptions); StreamingConnection two = NatsStreaming.connect(clusterName, "two", streamingOptions); try { assertNotNull(one); assertNotNull(two); } finally { one.close(); two.close(); } } } }
Example #8
Source File: StreamingConnectionImpl.java From stan.java with Apache License 2.0 | 6 votes |
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.Builder natsOpts = new io.nats.client.Options.Builder(). connectionName(clientId). errorListener(opts.getErrorListener()). connectionListener(opts.getConnectionListener()). server(opts.getNatsUrl()); if (opts.isTraceConnection()) { natsOpts.traceConnection(); } nc = Nats.connect(natsOpts.build()); } else { nc = Nats.connect(); } ncOwned = true; } return nc; }
Example #9
Source File: ErrorListenerTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testExceptionInErrorHandler() throws Exception { String[] customArgs = {"--user","stephen","--pass","password"}; BadHandler handler = new BadHandler(); Connection nc = null; try (NatsTestServer ts = new NatsTestServer(customArgs, false)) { // See config file for user/pass Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). errorListener(handler). // skip this so we get an error userInfo("stephen", "password"). build(); try { nc = Nats.connect(options); assertFalse(true); } catch(IOException e) { assertTrue(true); } finally { if (nc != null) { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } } }
Example #10
Source File: TLSConnectTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test(expected=IOException.class) public void testClientServerCertMismatch() throws Exception { Connection nc = null; try (NatsTestServer ts = new NatsTestServer("src/test/resources/tlsverify.conf", false)) { SSLContext ctx = TestSSLUtils.createEmptySSLContext(); Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). sslContext(ctx). build(); try { nc = Nats.connect(options); } finally { if (nc != null) { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } } }
Example #11
Source File: NatsBenchmarkDriver.java From openmessaging-benchmark with Apache License 2.0 | 6 votes |
@Override public CompletableFuture<BenchmarkConsumer> createConsumer(String topic, String subscriptionName, ConsumerCallback consumerCallback) { Dispatcher natsConsumer; Connection cn; log.info("createConsumer"); try { Options options = new Options.Builder().server(config.natsHostUrl).maxReconnects(5).build(); cn = Nats.connect(options); natsConsumer = cn.createDispatcher((msg) -> { consumerCallback.messageReceived(msg.getData(), Long.parseLong(msg.getReplyTo())); }); natsConsumer.subscribe(topic, subscriptionName); cn.flush(Duration.ZERO); } catch (Exception e) { log.error("createConsumer excetopin " + e); return null; } log.info("createCOnsumer done"); return CompletableFuture.completedFuture(new NatsBenchmarkConsumer(cn)); }
Example #12
Source File: TLSConnectTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testURISchemeIPTLSConnection() throws Exception { try (NatsTestServer ts = new NatsTestServer("src/test/resources/tlsverify.conf", false)) { Options options = new Options.Builder(). server("tls://127.0.0.1:"+ts.getPort()). sslContext(TestSSLUtils.createTestSSLContext()). // override the custom one maxReconnects(0). 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()); } } }
Example #13
Source File: TLSConnectTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testOpenTLSConnection() throws Exception { try (NatsTestServer ts = new NatsTestServer("src/test/resources/tls.conf", false)) { Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). opentls(). 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()); } } }
Example #14
Source File: TLSConnectTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testSimpleIPTLSConnection() throws Exception { //System.setProperty("javax.net.debug", "all"); try (NatsTestServer ts = new NatsTestServer("src/test/resources/tls.conf", false)) { SSLContext ctx = TestSSLUtils.createTestSSLContext(); Options options = new Options.Builder(). server("127.0.0.1:" + ts.getPort()). maxReconnects(0). sslContext(ctx). 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()); } } }
Example #15
Source File: ParseTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test(expected=IllegalArgumentException.class) public void testProtocolLineTooLong() throws Exception { try (NatsTestServer ts = new NatsTestServer(false); NatsConnection nc = (NatsConnection) Nats.connect(new Options.Builder(). server(ts.getURI()). maxControlLine(1024). build())) { NatsConnectionReader reader = nc.getReader(); StringBuilder longString = new StringBuilder(); longString.append("INFO "); for (int i=0;i<500;i++ ){ longString.append("helloworld"); } byte[] bytes = longString.toString().getBytes(StandardCharsets.US_ASCII); reader.fakeReadForTest(bytes); reader.gatherOp(bytes.length); reader.gatherProtocol(bytes.length); reader.parseProtocolMessage(); assertFalse(true); } }
Example #16
Source File: ConnectionListenerTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testDiscoveredServersCountAndListenerInOptions() throws Exception { try (NatsTestServer ts = new NatsTestServer()) { String customInfo = "{\"server_id\":\"myid\",\"connect_urls\": [\""+ts.getURI()+"\"]}"; try (NatsServerProtocolMock ts2 = new NatsServerProtocolMock(null, customInfo)) { TestHandler handler = new TestHandler(); Options options = new Options.Builder(). server(ts2.getURI()). maxReconnects(0). connectionListener(handler). build(); handler.prepForStatusChange(Events.CONNECTED); Connection nc = Nats.connect(options); try { handler.waitForStatusChange(5, TimeUnit.SECONDS); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); assertEquals(1, handler.getEventCount(Events.DISCOVERED_SERVERS)); } } } }
Example #17
Source File: ConnectionListenerTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testCloseCount() throws Exception { try (NatsTestServer ts = new NatsTestServer(false)) { TestHandler handler = new TestHandler(); Options options = new Options.Builder(). server(ts.getURI()). connectionListener(handler). build(); Connection nc = Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); assertEquals(ts.getURI(), nc.getConnectedUrl()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); assertNull(nc.getConnectedUrl()); } assertEquals(1, handler.getEventCount(Events.CLOSED)); } }
Example #18
Source File: NatsMessageTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test(expected=IllegalArgumentException.class) public void testBigProtocolLineWithBody() throws Exception { byte[] body = new byte[10]; String subject = "subject"; String replyTo = "reply"; while (subject.length() <= Options.DEFAULT_MAX_CONTROL_LINE) { subject = subject + subject; } try (NatsServerProtocolMock ts = new NatsServerProtocolMock(ExitAt.NO_EXIT); NatsConnection nc = (NatsConnection) Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); nc.publish(subject, replyTo, body); assertFalse(true); } }
Example #19
Source File: NatsMessageTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test(expected=IllegalArgumentException.class) public void testBigProtocolLineWithoutBody() throws Exception { String subject = "subject"; while (subject.length() <= Options.DEFAULT_MAX_CONTROL_LINE) { subject = subject + subject; } try (NatsServerProtocolMock ts = new NatsServerProtocolMock(ExitAt.NO_EXIT); NatsConnection nc = (NatsConnection) Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); nc.subscribe(subject); assertFalse(true); } }
Example #20
Source File: ErrorListenerTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testErrorOnNoAuth() throws Exception { String[] customArgs = {"--user","stephen","--pass","password"}; TestHandler handler = new TestHandler(); Connection nc = null; try (NatsTestServer ts = new NatsTestServer(customArgs, false)) { // See config file for user/pass Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). errorListener(handler). // skip this so we get an error userInfo("stephen", "password"). build(); try { nc = Nats.connect(options); } catch(IOException e) { assertTrue(handler.getCount() > 0); assertEquals(1, handler.getErrorCount("Authorization Violation")); } finally { if (nc != null) { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } } }
Example #21
Source File: DrainTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test(expected = IllegalStateException.class) public void testSubDuringDrainThrows() throws Exception { try (NatsTestServer ts = new NatsTestServer(false); Connection subCon = Nats.connect(new Options.Builder().server(ts.getURI()).maxReconnects(0).build()); Connection pubCon = Nats.connect(new Options.Builder().server(ts.getURI()).maxReconnects(0).build())) { assertTrue("Connected Status", Connection.Status.CONNECTED == subCon.getStatus()); assertTrue("Connected Status", Connection.Status.CONNECTED == pubCon.getStatus()); subCon.subscribe("draintest"); subCon.flush(Duration.ofSeconds(1)); // Get the sub to the server pubCon.publish("draintest", null); pubCon.publish("draintest", null); pubCon.flush(Duration.ofSeconds(1)); subCon.flush(Duration.ofSeconds(1)); CompletableFuture<Boolean> tracker = subCon.drain(Duration.ofSeconds(500)); // Try to subscribe while we are draining the sub subCon.subscribe("another"); // Should throw assertTrue(tracker.get(1000, TimeUnit.SECONDS)); } }
Example #22
Source File: PingTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testPingFailsWhenClosed() throws Exception { try (NatsServerProtocolMock ts = new NatsServerProtocolMock(ExitAt.NO_EXIT)) { Options options = new Options.Builder(). server(ts.getURI()). pingInterval(Duration.ofMillis(10)). maxPingsOut(5). maxReconnects(0). build(); NatsConnection nc = (NatsConnection) Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); } finally { nc.close(); } Future<Boolean> pong = nc.sendPing(); assertFalse(pong.get(10,TimeUnit.MILLISECONDS)); } }
Example #23
Source File: ParseTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test(expected=IOException.class) public void testMessageLineTooLong() throws Exception { try (NatsTestServer ts = new NatsTestServer(false); NatsConnection nc = (NatsConnection) Nats.connect(new Options.Builder(). server(ts.getURI()). maxControlLine(16). build())) { NatsConnectionReader reader = nc.getReader(); byte[] bytes = ("MSG reallylongsubjectobreakthelength 1 1\r\n").getBytes(StandardCharsets.US_ASCII); reader.fakeReadForTest(bytes); reader.gatherOp(bytes.length); reader.gatherMessageProtocol(bytes.length); reader.parseProtocolMessage(); assertFalse(true); } }
Example #24
Source File: InfoHandlerTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testInitialInfo() throws IOException, InterruptedException { String customInfo = "{\"server_id\":\"myid\"}"; try (NatsServerProtocolMock ts = new NatsServerProtocolMock(null, customInfo)) { Connection nc = Nats.connect(ts.getURI()); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); assertEquals("got custom info", "myid", ((NatsConnection) nc).getInfo().getServerId()); assertEquals(customInfo, ((NatsConnection) nc).getInfo().getRawJson()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
Example #25
Source File: TLSConnectTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testSimpleTLSConnection() throws Exception { //System.setProperty("javax.net.debug", "all"); try (NatsTestServer ts = new NatsTestServer("src/test/resources/tls.conf", false)) { SSLContext ctx = TestSSLUtils.createTestSSLContext(); Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). sslContext(ctx). 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()); } } }
Example #26
Source File: TLSConnectTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testVerifiedTLSConnection() throws Exception { try (NatsTestServer ts = new NatsTestServer("src/test/resources/tlsverify.conf", false)) { SSLContext ctx = TestSSLUtils.createTestSSLContext(); Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). sslContext(ctx). 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()); } } }
Example #27
Source File: FileAuthHandlerTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test public void testCredsFile() throws Exception { AuthHandler auth = Nats.credentials("src/test/resources/jwt_nkey/test.creds"); 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()); }
Example #28
Source File: FileAuthHandlerTests.java From nats.java with Apache License 2.0 | 5 votes |
@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()); }
Example #29
Source File: MessageContentTests.java From nats.java with Apache License 2.0 | 5 votes |
void runBadContentTest(NatsServerProtocolMock.Customizer badServer, CompletableFuture<Boolean> ready) throws Exception { TestHandler handler = new TestHandler(); try (NatsServerProtocolMock ts = new NatsServerProtocolMock(badServer, null)) { Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). errorListener(handler). connectionListener(handler). build(); Connection nc = Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); handler.prepForStatusChange(Events.DISCONNECTED); ready.complete(Boolean.TRUE); handler.waitForStatusChange(200, TimeUnit.MILLISECONDS); assertTrue(handler.getExceptionCount() > 0); assertTrue("Disconnected Status", Connection.Status.DISCONNECTED == nc.getStatus() || Connection.Status.CLOSED == nc.getStatus()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
Example #30
Source File: ReconnectTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test public void testMaxReconnects() throws Exception { Connection nc = null; TestHandler handler = new TestHandler(); int port = NatsTestServer.nextPort(); try { try (NatsTestServer ts = new NatsTestServer(port, false)) { Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(1). connectionListener(handler). reconnectWait(Duration.ofMillis(10)). build(); nc = Nats.connect(options); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); handler.prepForStatusChange(Events.CLOSED); } flushAndWait(nc, handler); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } finally { if (nc != null) { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }