Request-Reply Semantics
nc, err := nats.Connect("demo.nats.io")
if err != nil {
log.Fatal(err)
}
defer nc.Close()
// Send the request
msg, err := nc.Request("time", nil, time.Second)
if err != nil {
log.Fatal(err)
}
// Use the response
log.Printf("Reply: %s", msg.Data)
// Close the connection
nc.Close()Connection nc = Nats.connect("nats://demo.nats.io:4222");
// set up a listener for "time" requests
Dispatcher d = nc.createDispatcher(msg -> {
System.out.println("Received time request");
nc.publish(msg.getReplyTo(), ("" + System.currentTimeMillis()).getBytes());
});
d.subscribe("time");
// make a request to the "time" subject and wait 1 second for a response
Message msg = nc.request("time", null, Duration.ofSeconds(1));
// look at the response
long time = Long.parseLong(new String(msg.getData()));
System.out.println(new Date(time));
nc.close();Scatter-Gather
Last updated
Was this helpful?