【发布时间】:2019-09-05 08:20:27
【问题描述】:
如何发送 buf 然后接收 msg
方法
Mono<ByteBuf> send(ByteBuf buf){
// how to send the buf then receive a msg
}
我正在尝试通过从连接出站发送消息并从入站接收消息然后返回消息 Mono 来实现此方法。但我只能在 then(Publisher) 方法中接收消息。好像不能返回一个数据 Mono
我试过了。
// the connecttion has been initialized before entering this method.
Mono.just(buf)
.doOnNext(data -> connection.outbound().sendObject(data).then().subscribe())
.then(connection
.inbound()
.receiveObject()
.single()
.map(RpcDataPackage.class::cast)
.map(RpcDataPackage::getData)
.map(data -> {
try {
return resCodec.decode(data);
} catch (IOException e) {
throw new RpcRequestException(e);
}
})
);
但它会阻塞直到连接超时
我已经尝试了另一个代码。我添加了一个handle 方法并将响应放入地图。
然后我可以在map.get(key) != null 处获得带有while 循环中断的Mono.fromSupply()。
它会阻塞线程。
.handle(((nettyInbound, nettyOutbound) -> nettyInbound
.receiveObject()
.map(RpcDataPackage.class::cast)
.doOnNext(pkg -> {
String responseKey = "a key"
responseMap.put(responseKey, pkg);
})
.then()))
【问题讨论】:
标签: java project-reactor reactor-netty