java nats RequestMany
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
浙公网安备 33010602011771号