Search in sources :

Example 1 with Subscription

use of io.nats.client.Subscription in project java-nats-examples by nats-io.

the class SubscribeSync method main.

public static void main(String[] args) {
    try {
        // [begin subscribe_sync]
        Connection nc = Nats.connect("nats://demo.nats.io:4222");
        // Subscribe
        Subscription sub = nc.subscribe("updates");
        // Read a message
        Message msg = sub.nextMessage(Duration.ZERO);
        String str = new String(msg.getData(), StandardCharsets.UTF_8);
        System.out.println(str);
        // Close the connection
        nc.close();
    // [end subscribe_sync]
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Message(io.nats.client.Message) Connection(io.nats.client.Connection) Subscription(io.nats.client.Subscription)

Example 2 with Subscription

use of io.nats.client.Subscription in project java-nats-examples by nats-io.

the class SubscribeWithReply method main.

public static void main(String[] args) {
    try {
        // [begin subscribe_w_reply]
        Connection nc = Nats.connect("nats://demo.nats.io:4222");
        // Subscribe
        Subscription sub = nc.subscribe("time");
        // Read a message
        Message msg = sub.nextMessage(Duration.ZERO);
        // Get the time
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        byte[] timeAsBytes = sdf.format(cal.getTime()).getBytes(StandardCharsets.UTF_8);
        // Send the time
        nc.publish(msg.getReplyTo(), timeAsBytes);
        // Flush and close the connection
        nc.flush(Duration.ZERO);
        nc.close();
    // [end subscribe_w_reply]
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Message(io.nats.client.Message) Calendar(java.util.Calendar) Connection(io.nats.client.Connection) Subscription(io.nats.client.Subscription) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with Subscription

use of io.nats.client.Subscription in project java-nats-examples by nats-io.

the class Unsubscribe method main.

public static void main(String[] args) {
    try {
        // [begin unsubscribe]
        Connection nc = Nats.connect("nats://demo.nats.io:4222");
        Dispatcher d = nc.createDispatcher((msg) -> {
            String str = new String(msg.getData(), StandardCharsets.UTF_8);
            System.out.println(str);
        });
        // Sync Subscription
        Subscription sub = nc.subscribe("updates");
        sub.unsubscribe();
        // Async Subscription
        d.subscribe("updates");
        d.unsubscribe("updates");
        // Close the connection
        nc.close();
    // [end unsubscribe]
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Connection(io.nats.client.Connection) Dispatcher(io.nats.client.Dispatcher) Subscription(io.nats.client.Subscription)

Example 4 with Subscription

use of io.nats.client.Subscription in project java-nats-examples by nats-io.

the class SetPendingLimits method main.

public static void main(String[] args) {
    try {
        // [begin slow_pending_limits]
        Connection nc = Nats.connect("nats://demo.nats.io:4222");
        Dispatcher d = nc.createDispatcher((msg) -> {
        // do something
        });
        d.subscribe("updates");
        // Set limits on a dispatcher
        d.setPendingLimits(1_000, 5 * 1024 * 1024);
        // Subscribe
        Subscription sub = nc.subscribe("updates");
        // Set limits on a subscription
        sub.setPendingLimits(1_000, 5 * 1024 * 1024);
        // Do something
        // Close the connection
        nc.close();
    // [end slow_pending_limits]
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Connection(io.nats.client.Connection) Dispatcher(io.nats.client.Dispatcher) Subscription(io.nats.client.Subscription)

Example 5 with Subscription

use of io.nats.client.Subscription in project nats.java by nats-io.

the class DispatcherTests method testThrowOnWrongSubscription.

@Test
public void testThrowOnWrongSubscription() {
    assertThrows(IllegalStateException.class, () -> {
        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);
        }
    });
}
Also used : Connection(io.nats.client.Connection) NatsTestServer(io.nats.client.NatsTestServer) Dispatcher(io.nats.client.Dispatcher) Subscription(io.nats.client.Subscription) Test(org.junit.jupiter.api.Test)

Aggregations

Connection (io.nats.client.Connection)11 Subscription (io.nats.client.Subscription)11 Dispatcher (io.nats.client.Dispatcher)7 Message (io.nats.client.Message)5 NatsTestServer (io.nats.client.NatsTestServer)3 Test (org.junit.jupiter.api.Test)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 BeanContext (io.micronaut.context.BeanContext)1 ExecutableMethodProcessor (io.micronaut.context.processor.ExecutableMethodProcessor)1 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)1 NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1 BoundExecutable (io.micronaut.core.bind.BoundExecutable)1 DefaultExecutableBinder (io.micronaut.core.bind.DefaultExecutableBinder)1 NameUtils (io.micronaut.core.naming.NameUtils)1 ArgumentUtils (io.micronaut.core.util.ArgumentUtils)1 StringUtils (io.micronaut.core.util.StringUtils)1 BeanDefinition (io.micronaut.inject.BeanDefinition)1 ExecutableMethod (io.micronaut.inject.ExecutableMethod)1 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)1