io.nats.client.Dispatcher Java Examples
The following examples show how to use
io.nats.client.Dispatcher.
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: 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 #2
Source File: NatsDispatcher.java From nats.java with Apache License 2.0 | 6 votes |
public Dispatcher unsubscribe(String subject, int after) { if (!this.running.get()) { throw new IllegalStateException("Dispatcher is closed"); } if (isDraining()) { // No op while draining return this; } if (subject == null || subject.length() == 0) { throw new IllegalArgumentException("Subject is required in unsubscribe"); } NatsSubscription sub = this.subscriptionsUsingDefaultHandler.get(subject); if (sub != null) { this.connection.unsubscribe(sub, after); // Connection will tell us when to remove from the map } return this; }
Example #3
Source File: NatsConnection.java From nats.java with Apache License 2.0 | 6 votes |
public void closeDispatcher(Dispatcher d) { if (isClosed()) { throw new IllegalStateException("Connection is Closed"); } else if (!(d instanceof NatsDispatcher)) { throw new IllegalArgumentException("Connection can only manage its own dispatchers"); } NatsDispatcher nd = ((NatsDispatcher) d); if (nd.isDraining()) { return; // No op while draining } if (!this.dispatchers.containsKey(nd.getId())) { throw new IllegalArgumentException("Dispatcher is already closed."); } cleanupDispatcher(nd); }
Example #4
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalArgumentException.class) public void testThrowOnNullSubject() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe(null); assertFalse(true); } }
Example #5
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test public void testMultiMessage() throws IOException, InterruptedException, ExecutionException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { final CompletableFuture<Boolean> done = new CompletableFuture<>(); int msgCount = 100; assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); final ConcurrentLinkedQueue<Message> q = new ConcurrentLinkedQueue<>(); Dispatcher d = nc.createDispatcher((msg) -> { if (msg.getSubject().equals("done")) { done.complete(Boolean.TRUE); } else { q.add(msg); } }); d.subscribe("subject"); d.subscribe("done"); nc.flush(Duration.ofMillis(1000)); // wait for them to go through for (int i = 0; i < msgCount; i++) { nc.publish("subject", new byte[16]); } nc.publish("done", new byte[16]); nc.flush(Duration.ofMillis(1000)); // wait for them to go through done.get(500, TimeUnit.MILLISECONDS); assertEquals(msgCount, q.size()); } }
Example #6
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptySubjectWithMessageHandler() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe("", (msg) -> {}); assertFalse(true); } }
Example #7
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test public void testDoubleSubscribeWithCustomHandler() throws IOException, InterruptedException, ExecutionException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { final CompletableFuture<Boolean> done = new CompletableFuture<>(); int msgCount = 100; assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); final AtomicInteger count = new AtomicInteger(0); Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe("subject", (msg) -> { count.incrementAndGet(); }); d.subscribe("subject", "queue", (msg) -> { count.incrementAndGet(); }); d.subscribe("done", (msg) -> { done.complete(Boolean.TRUE); }); nc.flush(Duration.ofSeconds(5)); // wait for them to go through for (int i = 0; i < msgCount; i++) { nc.publish("subject", new byte[16]); } nc.publish("done", new byte[16]); nc.flush(Duration.ofSeconds(5)); // wait for them to go through done.get(5, TimeUnit.SECONDS); assertEquals(msgCount * 2, count.get()); // We should get 2x the messages because we subscribed 2 times. } }
Example #8
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test public void testUnsubFromCallback() throws IOException, InterruptedException, ExecutionException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { final CompletableFuture<Boolean> done = new CompletableFuture<>(); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); final AtomicReference<Dispatcher> dispatcher = new AtomicReference<>(); final ConcurrentLinkedQueue<Message> q = new ConcurrentLinkedQueue<>(); final Dispatcher d = nc.createDispatcher((msg) -> { if (msg.getSubject().equals("done")) { done.complete(Boolean.TRUE); } else { q.add(msg); dispatcher.get().unsubscribe("subject"); } }); dispatcher.set(d); d.subscribe("subject"); d.subscribe("done"); nc.flush(Duration.ofMillis(500));// Get them all to the server nc.publish("subject", new byte[16]); nc.publish("subject", new byte[16]); nc.publish("done", new byte[16]); // when we get this we know the others are dispatched nc.flush(Duration.ofMillis(1000)); // Wait for the publish, or we will get multiples for sure done.get(200, TimeUnit.MILLISECONDS); // make sure we got them assertEquals(1, q.size()); } }
Example #9
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test public void testAutoUnsubFromCallback() throws IOException, InterruptedException, ExecutionException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { final CompletableFuture<Boolean> done = new CompletableFuture<>(); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); final AtomicReference<Dispatcher> dispatcher = new AtomicReference<>(); final ConcurrentLinkedQueue<Message> q = new ConcurrentLinkedQueue<>(); final Dispatcher d = nc.createDispatcher((msg) -> { if (msg.getSubject().equals("done")) { done.complete(Boolean.TRUE); } else { q.add(msg); dispatcher.get().unsubscribe("subject", 2); // get 1 more, for a total of 2 } }); dispatcher.set(d); d.subscribe("subject"); d.subscribe("done"); nc.flush(Duration.ofMillis(1000));// Get them all to the server nc.publish("subject", new byte[16]); nc.publish("subject", new byte[16]); nc.publish("subject", new byte[16]); nc.publish("done", new byte[16]); // when we get this we know the others are dispatched nc.flush(Duration.ofMillis(1000)); // Wait for the publish done.get(200, TimeUnit.MILLISECONDS); // make sure we got them assertEquals(2, q.size()); } }
Example #10
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test public void testDoubleSubscribe() throws IOException, InterruptedException, ExecutionException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { final CompletableFuture<Boolean> done = new CompletableFuture<>(); int msgCount = 100; assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); final ConcurrentLinkedQueue<Message> q = new ConcurrentLinkedQueue<>(); Dispatcher d = nc.createDispatcher((msg) -> { if (msg.getSubject().equals("done")) { done.complete(Boolean.TRUE); } else { q.add(msg); } }); d.subscribe("subject").subscribe("subject").subscribe("subject").subscribe("done"); nc.flush(Duration.ofSeconds(5)); // wait for them to go through for (int i = 0; i < msgCount; i++) { nc.publish("subject", new byte[16]); } nc.publish("done", new byte[16]); nc.flush(Duration.ofSeconds(5)); // wait for them to go through done.get(5, TimeUnit.SECONDS); assertEquals(msgCount, q.size()); // Shoudl only get one since all the extra subs do nothing?? } }
Example #11
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test public void testCloseFromCallback() throws IOException, InterruptedException, ExecutionException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { final CompletableFuture<Boolean> done = new CompletableFuture<>(); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); final Dispatcher d = nc.createDispatcher((msg) -> { try { if (msg.getSubject().equals("done")) { nc.close(); done.complete(Boolean.TRUE); } } catch (InterruptedException e) { e.printStackTrace(); } }); d.subscribe("done"); nc.flush(Duration.ofMillis(5000));// Get them all to the server nc.publish("done", new byte[16]); // when we get this we know the others are dispatched nc.flush(Duration.ofMillis(5000)); // Wait for the publish done.get(5000, TimeUnit.MILLISECONDS); // make sure we got them assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } }
Example #12
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test public void testDispatchHandlesExceptionInHandler() throws IOException, InterruptedException, ExecutionException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { final CompletableFuture<Boolean> done = new CompletableFuture<>(); int msgCount = 100; assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); final ConcurrentLinkedQueue<Message> q = new ConcurrentLinkedQueue<>(); Dispatcher d = nc.createDispatcher((msg) -> { if (msg.getSubject().equals("done")) { done.complete(Boolean.TRUE); } else { q.add(msg); throw new NumberFormatException(); } }); d.subscribe("subject"); d.subscribe("done"); nc.flush(Duration.ofMillis(500));// Get them all to the server for (int i = 0; i < msgCount; i++) { nc.publish("subject", new byte[16]); } nc.publish("done", new byte[16]); nc.flush(Duration.ofMillis(1000)); // wait for them to go through done.get(200, TimeUnit.MILLISECONDS); assertEquals(msgCount, q.size()); } }
Example #13
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalArgumentException.class) public void testThrowOnDoubleClose() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); nc.closeDispatcher(d); nc.closeDispatcher(d); assertFalse(true); } }
Example #14
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptySubject() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe(""); assertFalse(true); } }
Example #15
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalStateException.class) public void testThrowOnConnClosed() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); nc.close(); nc.closeDispatcher(d); assertFalse(true); } }
Example #16
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptyQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe("subject", ""); assertFalse(true); } }
Example #17
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 #18
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptySubjectWithQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe("", "quque"); assertFalse(true); } }
Example #19
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected = IllegalStateException.class) public void throwsOnSubscribeIfClosed() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); nc.close(); d.subscribe("subject"); assertFalse(true); } }
Example #20
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalStateException.class) public void testThrowOnSubscribeWhenClosed() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); nc.closeDispatcher(d); d.subscribe("foo"); assertFalse(true); } }
Example #21
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalStateException.class) public void testThrowOnUnsubscribeWhenClosed() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe("foo"); nc.closeDispatcher(d); d.unsubscribe("foo"); assertFalse(true); } }
Example #22
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); } }
Example #23
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalStateException.class) public void testThrowOnWrongSubscription() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); Subscription sub2 = nc.subscribe("test"); d.unsubscribe(sub2); assertFalse(true); } }
Example #24
Source File: StreamingConnectionImpl.java From stan.java with Apache License 2.0 | 5 votes |
Dispatcher getDispatcherByName(String name) { Dispatcher d = null; this.lock(); try { if (name == null || name.isEmpty()) { if (this.messageDispatcher == null) { this.messageDispatcher = nc.createDispatcher(msg -> { this.processMsg(msg); }); this.messageDispatcher.setPendingLimits(-1, -1); } return this.messageDispatcher; } d = customDispatchers.get(name); if (d == null) { d = this.getNatsConnection().createDispatcher(msg -> { this.processMsg(msg); }); d.setPendingLimits(-1, -1); customDispatchers.put(name, d); } } finally { this.unlock(); } return d; }
Example #25
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalStateException.class) public void testThrowOnUnsubWhenClosed() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); Subscription sub = d.subscribe("subject", (msg) -> {}); nc.closeDispatcher(d); d.unsubscribe(sub); assertFalse(true); } }
Example #26
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptySubjectInUnsub() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.unsubscribe(""); assertFalse(true); } }
Example #27
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptySubjectWithQueueWithMessageHandler() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe("", "quque", (msg) -> {}); assertFalse(true); } }
Example #28
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalArgumentException.class) public void testThrowOnNullSubjectWithQueueWithMessageHandler() 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", (msg) -> {}); assertFalse(true); } }
Example #29
Source File: DispatcherTests.java From nats.java with Apache License 2.0 | 5 votes |
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptyQueueWithMessageHandler() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe("subject", "", (msg) -> {}); assertFalse(true); } }
Example #30
Source File: NatsConnection.java From nats.java with Apache License 2.0 | 5 votes |
public Dispatcher createDispatcher(MessageHandler handler) { if (isClosed()) { throw new IllegalStateException("Connection is Closed"); } else if (isDraining()) { throw new IllegalStateException("Connection is Draining"); } NatsDispatcher dispatcher = new NatsDispatcher(this, handler); String id = this.nuid.next(); this.dispatchers.put(id, dispatcher); dispatcher.start(id); return dispatcher; }