initial commit
This commit is contained in:
commit
976b4f88cc
11 changed files with 249 additions and 0 deletions
25
natsio/subscriber/Subscriber.java
Normal file
25
natsio/subscriber/Subscriber.java
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import io.nats.client.Connection;
|
||||
import io.nats.client.Dispatcher;
|
||||
import io.nats.client.Nats;
|
||||
|
||||
public class Subscriber {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Connection nc = Nats.connect("nats://nats:4222");
|
||||
|
||||
Dispatcher dispatcher = nc.createDispatcher(msg -> {
|
||||
String received = new String(msg.getData()).trim();
|
||||
String[] parts = received.split(",");
|
||||
if (parts.length >= 2) {
|
||||
int counter = Integer.parseInt(parts[0].trim());
|
||||
long sendTime = Long.parseLong(parts[1].trim());
|
||||
long latency = System.currentTimeMillis() - sendTime;
|
||||
|
||||
System.out.printf("Received: %d | Latency: %d ms%n", counter, latency);
|
||||
}
|
||||
});
|
||||
|
||||
dispatcher.subscribe("demo.subject");
|
||||
|
||||
Thread.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue