发送消息后,队列中没有显示
最后发现只能在flatMap中创建和关闭RpcClient
。
public Mono<ServerResponse> addCart(ServerRequest request){
AtomicReference<Cart> temp = new AtomicReference<>();
return request.bodyToMono(Cart.class)
.flatMap(cart -> doctorService.addCart(cart))
.flatMap(cart -> {
System.out.println("cart : " + cart);
Supplier<String> correlationIdSupplier = () -> UUID.randomUUID().toString();
RpcClient rpcClient = this.sender
.rpcClient("", RPC_QUEUE, correlationIdSupplier);
Mono<Delivery> reply =
rpcClient.rpc(Mono.just(new RpcClient.RpcRequest(cart.toString().getBytes())))
.doOnError(e -> System.out.println("send failed" + e));
rpcClient.close();
temp.set(cart);
reply.subscribe(m -> System.out.println("返回的数据为: " + new String(m.getBody()) ));
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).bodyValue(temp);
});
}