


























nats js sdk 很早就支持RequestMany的特性,java 版实际是不支持的,但是nats 的背后公司基于jnats 包装了orbit.java 实现了类似功能
<dependency>
<groupId>io.synadia</groupId>
<artifactId>request-many</artifactId>
<version>0.1.1</version>
</dependency>
Options options = new Options.Builder()
.server("nats://localhost:4222")
.inboxPrefix("_inbox.dalong")
.build();
try (Connection nc = Nats.connect(options)) {
// Use the standard sentinel builder shortcut
RequestMany rm = RequestMany.standardSentinel(nc);
System.out.println(rm);
// start a responder simulator.
Dispatcher dispatcher = nc.createDispatcher(m -> {
for (int x = 0; x < 10; x++) {
System.out.println(m.getReplyTo());
nc.publish(m.getReplyTo(), ("R" + x + "-" + new String(m.getData())).getBytes());
}
nc.publish(m.getReplyTo(), null);
});
dispatcher.subscribe("dalong");
rm.request("dalong", "hello".getBytes(), (message) -> {
System.out.println("Received: " + new String(message.toString()));
return true;
// return list.add(message);
});
}
以上是简单说明(callback 模式),实际还支持其他模式的(max requests,timeout 等)
https://github.com/synadia-io/orbit.java/tree/main/request-many
https://github.com/synadia-ai/synadia-agents/tree/main/client-sdk
https://github.com/nats-io/nats.js/blob/main/core/src/nats.ts#L190
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。