本文整理匯總了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;
}
}
}
示例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;
}
示例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();
}
示例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();
}
示例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();
}
示例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();
}
}
示例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();
}
}
示例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();
}
}
示例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");
}
}
示例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);
}