io.nats.client.Connection Java Examples
The following examples show how to use
io.nats.client.Connection.
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: 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 #2
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 #3
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 #4
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 #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: PingTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test(expected=TimeoutException.class) public void testFlushTimeoutDisconnected() throws Exception { TestHandler handler = new TestHandler(); try (NatsTestServer ts = new NatsTestServer(false)) { Options options = new Options.Builder().connectionListener(handler).server(ts.getURI()).build(); NatsConnection nc = (NatsConnection) Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); nc.flush(Duration.ofSeconds(2)); handler.prepForStatusChange(Events.DISCONNECTED); ts.close(); handler.waitForStatusChange(2, TimeUnit.SECONDS); nc.flush(Duration.ofSeconds(2)); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
Example #7
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 #8
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 #9
Source File: PingTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testPingTimer() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false)) { Options options = new Options.Builder().server(ts.getURI()).pingInterval(Duration.ofMillis(5)).build(); NatsConnection nc = (NatsConnection) Nats.connect(options); NatsStatistics stats = nc.getNatsStatistics(); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); try { Thread.sleep(200); // should get 10+ pings } catch (Exception exp) { //Ignore } assertTrue("got pings", stats.getPings() > 10); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
Example #10
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 #11
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 #12
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 #13
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 #14
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 #15
Source File: DrainTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test(expected = IllegalStateException.class) public void testCreateDispatcherDuringDrainThrows() 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)); subCon.createDispatcher((msg) -> { }); assertTrue(tracker.get(1000, TimeUnit.SECONDS)); } }
Example #16
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 #17
Source File: TLSConnectTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testURISchemeOpenTLSConnection() throws Exception { try (NatsTestServer ts = new NatsTestServer("src/test/resources/tls.conf", false)) { Options options = new Options.Builder(). server("opentls://localhost:"+ts.getPort()). 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 #18
Source File: PingTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test public void testMaxPingsOut() throws Exception { try (NatsServerProtocolMock ts = new NatsServerProtocolMock(ExitAt.NO_EXIT)) { Options options = new Options.Builder(). server(ts.getURI()). pingInterval(Duration.ofSeconds(10)). // Avoid auto pings maxPingsOut(2). maxReconnects(0). build(); NatsConnection nc = (NatsConnection) Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); nc.sendPing(); nc.sendPing(); assertNull("No future returned when past max", nc.sendPing()); } finally { nc.close(); } } }
Example #19
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 #20
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 #21
Source File: TLSConnectTests.java From nats.java with Apache License 2.0 | 6 votes |
@Test(expected=IOException.class) public void testServerSecureClientNotMismatch() throws Exception { Connection nc = null; try (NatsTestServer ts = new NatsTestServer("src/test/resources/tlsverify.conf", false)) { Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). build(); try { nc = Nats.connect(options); } finally { if (nc != null) { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } } }
Example #22
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 #23
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 #24
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 #25
Source File: NatsBenchmarkDriver.java From openmessaging-benchmark with Apache License 2.0 | 5 votes |
@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)); }
Example #26
Source File: StanRandom.java From nats.java with Apache License 2.0 | 5 votes |
public static void main(String args[]) { String server = "help"; if (args.length == 1) { server = args[0]; } else if (args.length == 0) { server = Options.DEFAULT_URL; } if (server.equals("help")) { usage(); return; } try { Options options = new Options.Builder().server(server).noReconnect().build(); Connection nc = Nats.connect(options); Future<Message> replyFuture = nc.request("stan.random", null); Message reply = replyFuture.get(); System.out.printf("The next stan-random number is %s\n", new String(reply.getData(), StandardCharsets.UTF_8)); nc.close(); } catch (Exception exp) { exp.printStackTrace(); } }
Example #27
Source File: StanTime.java From nats.java with Apache License 2.0 | 5 votes |
public static void main(String args[]) { String server = "help"; if (args.length == 1) { server = args[0]; } else if (args.length == 0) { server = Options.DEFAULT_URL; } if (server.equals("help")) { usage(); return; } try { Options options = new Options.Builder().server(server).noReconnect().build(); Connection nc = Nats.connect(options); Future<Message> replyFuture = nc.request("stan.time", null); Message reply = replyFuture.get(); System.out.printf("The time where stan is, is \"%s\"\n", new String(reply.getData(), StandardCharsets.UTF_8)); nc.close(); } catch (Exception exp) { exp.printStackTrace(); } }
Example #28
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalArgumentException.class) public void testThrowOnNullSubjectWithQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe(null, "quque"); assertFalse(true); } }
Example #29
Source File: DrainTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test public void testSimpleSubDrain() 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()); Subscription sub = subCon.subscribe("draintest"); subCon.flush(Duration.ofSeconds(1)); // Get the sub to the server pubCon.publish("draintest", null); pubCon.publish("draintest", null); // publish 2 pubCon.flush(Duration.ofSeconds(1)); Message msg = sub.nextMessage(Duration.ofSeconds(1)); // read 1 assertNotNull(msg); subCon.flush(Duration.ofSeconds(1)); CompletableFuture<Boolean> tracker = sub.drain(Duration.ofSeconds(1)); msg = sub.nextMessage(Duration.ofSeconds(1)); // read the second one, should be there because we drained assertNotNull(msg); assertTrue(tracker.get(1, TimeUnit.SECONDS)); assertFalse(sub.isActive()); assertEquals(((NatsConnection) subCon).getConsumerCount(), 0); } }
Example #30
Source File: DrainTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test public void testSimpleDispatchDrain() 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()); AtomicInteger count = new AtomicInteger(); Dispatcher d = subCon.createDispatcher((msg) -> { count.incrementAndGet(); try { Thread.sleep(2000); // go slow so the main app can drain us } catch (Exception e) { } }); d.subscribe("draintest"); d.subscribe("draintest", (msg) -> { count.incrementAndGet(); }); subCon.flush(Duration.ofSeconds(5)); // Get the sub to the server pubCon.publish("draintest", null); pubCon.publish("draintest", null); pubCon.flush(Duration.ofSeconds(1)); subCon.flush(Duration.ofSeconds(1)); // Drain will unsub the dispatcher, only messages that already arrived // are there CompletableFuture<Boolean> tracker = d.drain(Duration.ofSeconds(8)); assertTrue(tracker.get(10, TimeUnit.SECONDS)); // wait for the drain to complete assertEquals(count.get(), 4); // Should get both, two times. assertFalse(d.isActive()); assertEquals(((NatsConnection) subCon).getConsumerCount(), 0); } }