Java Code Examples for io.nats.client.Dispatcher#setPendingLimits()

The following examples show how to use io.nats.client.Dispatcher#setPendingLimits() . 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: StreamingConnectionImpl.java    From stan.java with Apache License 2.0 5 votes vote down vote up
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 2
Source File: SlowConsumerTests.java    From nats.java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSlowSDispatcherByMessages() throws Exception {

    try (NatsTestServer ts = new NatsTestServer(false);
            NatsConnection nc = (NatsConnection) Nats.connect(ts.getURI())) {
        
        final CompletableFuture<Void> ok = new CompletableFuture<>();
        Dispatcher d = nc.createDispatcher((msg) -> {
            ok.complete(null);
            Thread.sleep(5 * 60 * 1000); // will wait until interrupted
        });

        d.setPendingLimits(1, -1);
        d.subscribe("subject");

        assertEquals(1, d.getPendingMessageLimit());
        assertEquals(-1, d.getPendingByteLimit());
        assertEquals(0, d.getDroppedCount());

        nc.publish("subject", null);

        ok.get(1000,TimeUnit.MILLISECONDS); // make sure we got the first one
        
        nc.publish("subject", null);
        nc.publish("subject", null);
        nc.flush(Duration.ofMillis(1000));

        assertEquals(1, d.getDroppedCount());
        assertEquals(1, d.getPendingMessageCount());
        assertEquals(19, d.getPendingByteCount()); // "msg 1 subject 0" + crlf + crlf

        d.clearDroppedCount();
        
        nc.publish("subject", null);
        nc.flush(Duration.ofMillis(5000));

        assertEquals(1, d.getDroppedCount());
        assertEquals(1, d.getPendingMessageCount());
    }
}
 
Example 3
Source File: SlowConsumerTests.java    From nats.java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSlowSDispatcherByBytes() throws Exception {

    try (NatsTestServer ts = new NatsTestServer(false);
            NatsConnection nc = (NatsConnection) Nats.connect(ts.getURI())) {
        
        final CompletableFuture<Void> ok = new CompletableFuture<>();
        Dispatcher d = nc.createDispatcher((msg) -> {
            ok.complete(null);
            Thread.sleep(5 * 60 * 1000); // will wait until interrupted
        });

        d.setPendingLimits(-1, 10);
        d.subscribe("subject");

        assertEquals(-1, d.getPendingMessageLimit());
        assertEquals(10, d.getPendingByteLimit());
        assertEquals(0, d.getDroppedCount());

        nc.publish("subject", null);

        ok.get(1000,TimeUnit.MILLISECONDS); // make sure we got the first one
        
        nc.publish("subject", null);
        nc.publish("subject", null);
        nc.flush(Duration.ofMillis(5000));

        assertEquals(1, d.getDroppedCount());
        assertEquals(1, d.getPendingMessageCount());
        assertEquals(19, d.getPendingByteCount()); // "msg 1 subject 0" + crlf + crlf

        d.clearDroppedCount();
        
        nc.publish("subject", null);
        nc.flush(Duration.ofMillis(5000));

        assertEquals(1, d.getDroppedCount());
        assertEquals(1, d.getPendingMessageCount());
    }
}