Search in sources :

Example 1 with RequestFuture

use of com.alibaba.nacos.api.remote.RequestFuture in project nacos by alibaba.

the class GrpcConnection method requestFuture.

@Override
public RequestFuture requestFuture(Request request) throws NacosException {
    Payload grpcRequest = GrpcUtils.convert(request);
    final ListenableFuture<Payload> requestFuture = grpcFutureServiceStub.request(grpcRequest);
    return new RequestFuture() {

        @Override
        public boolean isDone() {
            return requestFuture.isDone();
        }

        @Override
        public Response get() throws Exception {
            Payload grpcResponse = requestFuture.get();
            Response response = (Response) GrpcUtils.parse(grpcResponse);
            if (response instanceof ErrorResponse) {
                throw new NacosException(response.getErrorCode(), response.getMessage());
            }
            return response;
        }

        @Override
        public Response get(long timeout) throws Exception {
            Payload grpcResponse = requestFuture.get(timeout, TimeUnit.MILLISECONDS);
            Response response = (Response) GrpcUtils.parse(grpcResponse);
            if (response instanceof ErrorResponse) {
                throw new NacosException(response.getErrorCode(), response.getMessage());
            }
            return response;
        }
    };
}
Also used : Response(com.alibaba.nacos.api.remote.response.Response) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse) RequestFuture(com.alibaba.nacos.api.remote.RequestFuture) Payload(com.alibaba.nacos.api.grpc.auto.Payload) NacosException(com.alibaba.nacos.api.exception.NacosException) ErrorResponse(com.alibaba.nacos.api.remote.response.ErrorResponse)

Aggregations

NacosException (com.alibaba.nacos.api.exception.NacosException)1 Payload (com.alibaba.nacos.api.grpc.auto.Payload)1 RequestFuture (com.alibaba.nacos.api.remote.RequestFuture)1 ErrorResponse (com.alibaba.nacos.api.remote.response.ErrorResponse)1 Response (com.alibaba.nacos.api.remote.response.Response)1