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();
}
}
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();
}
}
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();
}
}
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();
}
}
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);
}
});
}
Aggregations