Search in sources :

Example 1 with Simple

use of com.hry.spring.grpc.mystream.Simple in project spring_boot by hryou0922.

the class HelloStreamClient method client2ServerRpc.

/**
 * 异步客户端流
 */
public void client2ServerRpc(int count) throws InterruptedException {
    logger.info("request client2ServerRpc {}", count);
    final CountDownLatch finishLatch = new CountDownLatch(1);
    StreamObserver<SimpleSummary> responseObserver = new StreamObserver<SimpleSummary>() {

        @Override
        public void onNext(SimpleSummary value) {
            // 返回SimpleSummary
            logger.info("client2ServerRpc onNext : {}", value);
        }

        @Override
        public void onError(Throwable t) {
            logger.error("client2ServerRpc error : {}", t);
            finishLatch.countDown();
        }

        @Override
        public void onCompleted() {
            logger.error("client2ServerRpc finish");
            finishLatch.countDown();
        }
    };
    StreamObserver<Simple> requestObserver = asyncStub.client2ServerRpc(responseObserver);
    try {
        for (int i = 0; i < count; i++) {
            logger.info("simple2 : {}", i);
            Simple simple = Simple.newBuilder().setName("client2ServerRpc" + i).setNum(i).build();
            requestObserver.onNext(simple);
            Thread.sleep(random.nextInt(200) + 50);
        }
    } catch (RuntimeException e) {
        // Cancel RPC
        requestObserver.onError(e);
        throw e;
    }
    // 结束请求
    requestObserver.onCompleted();
    // Receiving happens asynchronously
    if (!finishLatch.await(1, TimeUnit.MINUTES)) {
        logger.error("client2ServerRpc can not finish within 1 minutes");
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) StatusRuntimeException(io.grpc.StatusRuntimeException) SimpleSummary(com.hry.spring.grpc.mystream.SimpleSummary) CountDownLatch(java.util.concurrent.CountDownLatch) Simple(com.hry.spring.grpc.mystream.Simple)

Example 2 with Simple

use of com.hry.spring.grpc.mystream.Simple in project spring_boot by hryou0922.

the class HelloStreamClient method bindirectionalStreamRpc.

/**
 * 双向流
 *
 * @throws InterruptedException
 */
public void bindirectionalStreamRpc() throws InterruptedException {
    logger.info("request bindirectionalStreamRpc");
    final CountDownLatch finishLatch = new CountDownLatch(1);
    StreamObserver<Simple> requestObserver = asyncStub.bindirectionalStreamRpc(new StreamObserver<Simple>() {

        @Override
        public void onNext(Simple value) {
            logger.info("bindirectionalStreamRpc receive message : {}", value);
        }

        @Override
        public void onError(Throwable t) {
            logger.error("bindirectionalStreamRpc Failed: {0}", Status.fromThrowable(t));
            finishLatch.countDown();
        }

        @Override
        public void onCompleted() {
            logger.info("Finished bindirectionalStreamRpc");
            finishLatch.countDown();
        }
    });
    try {
        Simple[] requests = { newSimple(1), newSimple(2), newSimple(3), newSimple(4) };
        for (Simple request : requests) {
            logger.info("Sending message {}", request);
            requestObserver.onNext(request);
        }
    } catch (RuntimeException e) {
        // Cancel RPC
        requestObserver.onError(e);
        throw e;
    }
    requestObserver.onCompleted();
    if (!finishLatch.await(1, TimeUnit.MINUTES)) {
        logger.error("routeChat can not finish within 1 minutes");
    }
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) CountDownLatch(java.util.concurrent.CountDownLatch) Simple(com.hry.spring.grpc.mystream.Simple)

Example 3 with Simple

use of com.hry.spring.grpc.mystream.Simple in project spring_boot by hryou0922.

the class HelloStreamClient method simpleRpc.

/**
 * 一元服务调用
 */
public void simpleRpc(int num) {
    logger.info("request simpleRpc: num={}", num);
    Simple simple = Simple.newBuilder().setName("simpleRpc").setNum(num).build();
    SimpleFeature feature;
    try {
        feature = blockingStub.simpleRpc(simple);
    } catch (StatusRuntimeException e) {
        logger.info("RPC failed: {0}", e.getStatus());
        return;
    }
    logger.info("simpleRpc end called {}", feature);
}
Also used : StatusRuntimeException(io.grpc.StatusRuntimeException) SimpleFeature(com.hry.spring.grpc.mystream.SimpleFeature) Simple(com.hry.spring.grpc.mystream.Simple)

Example 4 with Simple

use of com.hry.spring.grpc.mystream.Simple in project spring_boot by hryou0922.

the class HelloStreamClient method server2ClientRpc.

/**
 * 阻塞服务器流
 */
public void server2ClientRpc(int num1, int num2) {
    logger.info("request server2ClientRpc num1={}, num2={}", num1, num2);
    Simple simple = Simple.newBuilder().setName("simple2" + num1).setNum(num1).build();
    Simple simple2 = Simple.newBuilder().setName("simple2" + num2).setNum(num2).build();
    SimpleList simpleList = SimpleList.newBuilder().addSimpleList(simple).addSimpleList(simple2).build();
    Iterator<SimpleFeature> simpleFeatureIter = blockingStub.server2ClientRpc(simpleList);
    for (int i = 1; simpleFeatureIter.hasNext(); i++) {
        SimpleFeature feature = simpleFeatureIter.next();
        logger.info("Result {} : {}", i, feature);
    }
}
Also used : SimpleList(com.hry.spring.grpc.mystream.SimpleList) SimpleFeature(com.hry.spring.grpc.mystream.SimpleFeature) Simple(com.hry.spring.grpc.mystream.Simple)

Aggregations

Simple (com.hry.spring.grpc.mystream.Simple)4 StatusRuntimeException (io.grpc.StatusRuntimeException)3 SimpleFeature (com.hry.spring.grpc.mystream.SimpleFeature)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 SimpleList (com.hry.spring.grpc.mystream.SimpleList)1 SimpleSummary (com.hry.spring.grpc.mystream.SimpleSummary)1 StreamObserver (io.grpc.stub.StreamObserver)1