當前位置: 首頁>>代碼示例>>Java>>正文


Java Server.shutdown方法代碼示例

本文整理匯總了Java中io.grpc.Server.shutdown方法的典型用法代碼示例。如果您正苦於以下問題:Java Server.shutdown方法的具體用法?Java Server.shutdown怎麽用?Java Server.shutdown使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在io.grpc.Server的用法示例。


在下文中一共展示了Server.shutdown方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: close

import io.grpc.Server; //導入方法依賴的package包/類
/**
 * Shutdown the gRPC {@link Server} when this object is closed.
 */
@Override
public void close() throws Exception {
    final Server server = server();

    if (server != null) {
        server.shutdown();

        try {
            // TODO: Maybe we should catch the InterruptedException from this?
            server.awaitTermination(shutdownWaitTimeInMillis, TimeUnit.MILLISECONDS);
        } finally {
            server.shutdownNow();

            this.server = null;
        }
    }
}
 
開發者ID:salesforce,項目名稱:grpc-java-contrib,代碼行數:21,代碼來源:GrpcServerHost.java

示例2: shutdownGracefully

import io.grpc.Server; //導入方法依賴的package包/類
/**
 * Attempt to {@link Server#shutdown()} the {@link Server} gracefully. If the max wait time is exceeded, give up and
 * perform a hard {@link Server#shutdownNow()}.
 *
 * @param server the server to be shutdown
 * @param timeout the max amount of time to wait for graceful shutdown to occur
 * @param unit the time unit denominating the shutdown timeout
 * @return the given server
 * @throws InterruptedException if waiting for termination is interrupted
 */
public static Server shutdownGracefully(Server server, long timeout, TimeUnit unit) throws InterruptedException {
    Preconditions.checkNotNull(server, "server");
    Preconditions.checkArgument(timeout > 0, "timeout must be greater than 0");
    Preconditions.checkNotNull(unit, "unit");

    server.shutdown();

    try {
        server.awaitTermination(timeout, unit);
    } finally {
        server.shutdownNow();
    }

    return server;
}
 
開發者ID:salesforce,項目名稱:grpc-java-contrib,代碼行數:26,代碼來源:Servers.java

示例3: serverRunsAndRespondsCorrectly

import io.grpc.Server; //導入方法依賴的package包/類
@Test
public void serverRunsAndRespondsCorrectly() throws ExecutionException,
        IOException,
        InterruptedException,
        TimeoutException {
    final String name = UUID.randomUUID().toString();

    Server server = ServerBuilder.forPort(9999)
            .addService(new GreeterImpl())
            .build();

    server.start();

    ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", server.getPort())
            .usePlaintext(true)
            .build();

    GreeterGrpc8.GreeterCompletableFutureStub stub = GreeterGrpc8.newCompletableFutureStub(channel);

    CompletableFuture<HelloResponse> response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());

    await().atMost(3, TimeUnit.SECONDS).until(() -> response.isDone() && response.get().getMessage().contains(name));

    channel.shutdown();
    channel.awaitTermination(1, TimeUnit.MINUTES);
    channel.shutdownNow();

    server.shutdown();
    server.awaitTermination(1, TimeUnit.MINUTES);
    server.shutdownNow();
}
 
開發者ID:salesforce,項目名稱:grpc-java-contrib,代碼行數:32,代碼來源:CompletableFutureEndToEndTest.java

示例4: run

import io.grpc.Server; //導入方法依賴的package包/類
void run() throws Exception {
  // Port 0 means that the operating system will pick an available port to use.
  Server server = ServerBuilder.forPort(0).addService(new GreeterGrpc.GreeterImplBase() {
    @Override
    public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
      responseObserver.onError(Status.INTERNAL
          .withDescription("Eggplant Xerxes Crybaby Overbite Narwhal").asRuntimeException());
    }
  }).build().start();
  channel =
      ManagedChannelBuilder.forAddress("localhost", server.getPort()).usePlaintext(true).build();

  blockingCall();
  futureCallDirect();
  futureCallCallback();
  asyncCall();
  advancedAsyncCall();

  channel.shutdown();
  server.shutdown();
  channel.awaitTermination(1, TimeUnit.SECONDS);
  server.awaitTermination();
}
 
開發者ID:grpc,項目名稱:grpc-java,代碼行數:24,代碼來源:ErrorHandlingClient.java

示例5: run

import io.grpc.Server; //導入方法依賴的package包/類
void run() throws Exception {
  Server server = ServerBuilder.forPort(0).addService(new GreeterGrpc.GreeterImplBase() {
    @Override
    public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
      Metadata trailers = new Metadata();
      trailers.put(DEBUG_INFO_TRAILER_KEY, DEBUG_INFO);
      responseObserver.onError(Status.INTERNAL.withDescription(DEBUG_DESC)
          .asRuntimeException(trailers));
    }
  }).build().start();
  channel =
      ManagedChannelBuilder.forAddress("localhost", server.getPort()).usePlaintext(true).build();

  blockingCall();
  futureCallDirect();
  futureCallCallback();
  asyncCall();
  advancedAsyncCall();

  channel.shutdown();
  server.shutdown();
  channel.awaitTermination(1, TimeUnit.SECONDS);
  server.awaitTermination();
}
 
開發者ID:grpc,項目名稱:grpc-java,代碼行數:25,代碼來源:DetailErrorSample.java

示例6: uniqueSessionIdPerChannel

import io.grpc.Server; //導入方法依賴的package包/類
@Test
public void uniqueSessionIdPerChannel() throws Exception {
    GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() {
        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
            responseObserver.onNext(HelloResponse.newBuilder().setMessage(SessionIdServerInterceptor.SESSION_ID.get().toString()).build());
            responseObserver.onCompleted();
        }
    };

    Server server = InProcessServerBuilder.forName("uniqueSessionIdPerChannel")
            .addTransportFilter(new ClientSessionTransportFilter())
            .intercept(new SessionIdServerInterceptor())
            .addService(svc)
            .build()
            .start();

    ManagedChannel channel1 = InProcessChannelBuilder.forName("uniqueSessionIdPerChannel")
            .usePlaintext(true)
            .build();
    GreeterGrpc.GreeterBlockingStub stub1 = GreeterGrpc.newBlockingStub(channel1);

    ManagedChannel channel2 = InProcessChannelBuilder.forName("uniqueSessionIdPerChannel")
            .usePlaintext(true)
            .build();
    GreeterGrpc.GreeterBlockingStub stub2 = GreeterGrpc.newBlockingStub(channel2);

    try {
        String sessionId1 = stub1.sayHello(HelloRequest.getDefaultInstance()).getMessage();
        String sessionId2 = stub2.sayHello(HelloRequest.getDefaultInstance()).getMessage();

        assertThat(sessionId1).isNotEqualTo(sessionId2);
    } finally {
        channel1.shutdown();
        channel2.shutdown();
        server.shutdown();
    }
}
 
開發者ID:salesforce,項目名稱:grpc-java-contrib,代碼行數:39,代碼來源:SessionIdTest.java

示例7: interceptorThrowsIfMissingTransportFilter

import io.grpc.Server; //導入方法依賴的package包/類
@Test
public void interceptorThrowsIfMissingTransportFilter() throws Exception {
    GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() {
        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
            responseObserver.onNext(HelloResponse.newBuilder().setMessage(SessionIdServerInterceptor.SESSION_ID.get().toString()).build());
            responseObserver.onCompleted();
        }
    };

    Server server = InProcessServerBuilder.forName("interceptorThrowsIfMissingTransportFilter")
            .intercept(new SessionIdServerInterceptor())
            .addService(svc)
            .build()
            .start();

    ManagedChannel channel = InProcessChannelBuilder.forName("interceptorThrowsIfMissingTransportFilter")
            .usePlaintext(true)
            .build();
    GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);

    try {
        assertThatThrownBy(() -> stub.sayHello(HelloRequest.getDefaultInstance()).getMessage()).isInstanceOf(StatusRuntimeException.class);

    } finally {
        channel.shutdown();
        server.shutdown();
    }
}
 
開發者ID:salesforce,項目名稱:grpc-java-contrib,代碼行數:30,代碼來源:SessionIdTest.java

示例8: perSessionShouldFailMissingTransportFilter

import io.grpc.Server; //導入方法依賴的package包/類
@Test
public void perSessionShouldFailMissingTransportFilter() throws Exception {
    class TestService extends GreeterGrpc.GreeterImplBase {
        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
            responseObserver.onNext(HelloResponse.newBuilder().setMessage(Integer.toString(System.identityHashCode(this))).build());
            responseObserver.onCompleted();
        }
    }

    ClientSessionTransportFilter tf = new ClientSessionTransportFilter();
    Server server = InProcessServerBuilder.forName("perSessionShouldInstantiateOneInstancePerSession")
            .addService(new PerSessionService<>(() -> new TestService(), tf))
            .build()
            .start();

    ManagedChannel channel = InProcessChannelBuilder.forName("perSessionShouldInstantiateOneInstancePerSession")
            .usePlaintext(true)
            .build();
    GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);

    try {
        assertThatThrownBy(() -> stub.sayHello(HelloRequest.getDefaultInstance()).getMessage()).isInstanceOf(StatusRuntimeException.class);
    } finally {
        channel.shutdown();
        server.shutdown();
    }
}
 
開發者ID:salesforce,項目名稱:grpc-java-contrib,代碼行數:29,代碼來源:PerSessionServiceTest.java

示例9: stopAndReleaseGrpcServer

import io.grpc.Server; //導入方法依賴的package包/類
protected void stopAndReleaseGrpcServer() {
	Server localServer = this.server;
	if (localServer != null) {
		localServer.shutdown();
		this.server = null;
		logger.info("gRPC server stopped");
	}
}
 
開發者ID:saturnism,項目名稱:spring-boot-starter-grpc,代碼行數:9,代碼來源:GrpcServerLifecycle.java

示例10: perSessionShouldInstantiateOneInstancePerSession

import io.grpc.Server; //導入方法依賴的package包/類
@Test
public void perSessionShouldInstantiateOneInstancePerSession() throws Exception {
    AtomicInteger closeCount = new AtomicInteger(0);

    class TestService extends GreeterGrpc.GreeterImplBase implements AutoCloseable {
        public TestService() {}

        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
            if (SessionIdServerInterceptor.SESSION_ID.get() == null) {
                responseObserver.onError(new Exception("Missing SESSION_ID"));
            } else {
                responseObserver.onNext(HelloResponse.newBuilder().setMessage(Integer.toString(System.identityHashCode(this))).build());
                responseObserver.onCompleted();
            }
        }

        @Override
        public void close() throws Exception {
            closeCount.incrementAndGet();
        }
    }

    ClientSessionTransportFilter tf = new ClientSessionTransportFilter();
    Server server = InProcessServerBuilder.forName("perSessionShouldInstantiateOneInstancePerSession")
            .addTransportFilter(tf)
            .intercept(new SessionIdServerInterceptor())
            .addService(new PerSessionService<>(() -> new TestService(), tf))
            .build()
            .start();

    ManagedChannel channel1 = InProcessChannelBuilder.forName("perSessionShouldInstantiateOneInstancePerSession")
            .usePlaintext(true)
            .build();
    GreeterGrpc.GreeterBlockingStub stub1 = GreeterGrpc.newBlockingStub(channel1);

    ManagedChannel channel2 = InProcessChannelBuilder.forName("perSessionShouldInstantiateOneInstancePerSession")
            .usePlaintext(true)
            .build();
    GreeterGrpc.GreeterBlockingStub stub2 = GreeterGrpc.newBlockingStub(channel2);

    ManagedChannel channel3 = InProcessChannelBuilder.forName("perSessionShouldInstantiateOneInstancePerSession")
            .usePlaintext(true)
            .build();
    GreeterGrpc.GreeterBlockingStub stub3 = GreeterGrpc.newBlockingStub(channel3);

    try {
        String oid11 = stub1.sayHello(HelloRequest.getDefaultInstance()).getMessage();
        String oid21 = stub2.sayHello(HelloRequest.getDefaultInstance()).getMessage();
        String oid31 = stub3.sayHello(HelloRequest.getDefaultInstance()).getMessage();

        String oid12 = stub1.sayHello(HelloRequest.getDefaultInstance()).getMessage();
        String oid22 = stub2.sayHello(HelloRequest.getDefaultInstance()).getMessage();
        String oid32 = stub3.sayHello(HelloRequest.getDefaultInstance()).getMessage();

        assertThat(oid11).isEqualTo(oid12);
        assertThat(oid21).isEqualTo(oid22);
        assertThat(oid31).isEqualTo(oid32);

        assertThat(oid11).isNotEqualTo(oid21);
        assertThat(oid21).isNotEqualTo(oid31);
        assertThat(oid31).isNotEqualTo(oid11);
    } finally {
        channel1.shutdown();
        server.shutdown();
        channel2.shutdown();
        channel3.shutdown();

        server.awaitTermination();
    }

    assertThat(closeCount.get()).isEqualTo(3);
}
 
開發者ID:salesforce,項目名稱:grpc-java-contrib,代碼行數:74,代碼來源:PerSessionServiceTest.java


注:本文中的io.grpc.Server.shutdown方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。