本文整理汇总了Java中io.grpc.ManagedChannel.shutdown方法的典型用法代码示例。如果您正苦于以下问题:Java ManagedChannel.shutdown方法的具体用法?Java ManagedChannel.shutdown怎么用?Java ManagedChannel.shutdown使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.grpc.ManagedChannel
的用法示例。
在下文中一共展示了ManagedChannel.shutdown方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: serverRunsAndRespondsCorrectly
import io.grpc.ManagedChannel; //导入方法依赖的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();
}
示例2: withNewChannel
import io.grpc.ManagedChannel; //导入方法依赖的package包/类
<T extends AbstractStub<T>, R> CompletableFuture<R> withNewChannel(
String endpoint,
Function<ManagedChannel, T> stubCustomizer,
Function<T, CompletableFuture<R>> stubConsumer) {
final ManagedChannel channel = defaultChannelBuilder()
.nameResolverFactory(
forEndpoints(
Optional.ofNullable(builder.authority()).orElse("etcd"),
Collections.singleton(endpoint),
Optional.ofNullable(builder.uriResolverLoader())
.orElseGet(URIResolverLoader::defaultLoader)
)
).build();
try {
T stub = stubCustomizer.apply(channel);
return stubConsumer.apply(stub).whenComplete(
(r, t) -> channel.shutdown()
);
} catch (Exception e) {
channel.shutdown();
throw EtcdExceptionFactory.toEtcdException(e);
}
}
示例3: main
import io.grpc.ManagedChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
.usePlaintext(true)
.build();
GreetingServiceGrpc.GreetingServiceBlockingStub stub =
GreetingServiceGrpc.newBlockingStub(channel);
HelloResponse helloResponse = stub.greeting(
HelloRequest.newBuilder()
.setName("Ray")
.setAge(18)
.setSentiment(Sentiment.HAPPY)
.build());
System.out.println(helloResponse);
channel.shutdown();
}
示例4: wrongHostNameFailHostnameVerification
import io.grpc.ManagedChannel; //导入方法依赖的package包/类
@Test
public void wrongHostNameFailHostnameVerification() throws Exception {
ManagedChannel channel = createChannelBuilder()
.overrideAuthority(GrpcUtil.authorityFromHostAndPort(
BAD_HOSTNAME, getPort()))
.build();
TestServiceGrpc.TestServiceBlockingStub blockingStub =
TestServiceGrpc.newBlockingStub(channel);
Throwable actualThrown = null;
try {
blockingStub.emptyCall(Empty.getDefaultInstance());
} catch (Throwable t) {
actualThrown = t;
}
assertNotNull("The rpc should have been failed due to hostname verification", actualThrown);
Throwable cause = Throwables.getRootCause(actualThrown);
assertTrue(
"Failed by unexpected exception: " + cause, cause instanceof SSLPeerUnverifiedException);
channel.shutdown();
}
示例5: hostnameVerifierWithBadHostname
import io.grpc.ManagedChannel; //导入方法依赖的package包/类
@Test
public void hostnameVerifierWithBadHostname() throws Exception {
ManagedChannel channel = createChannelBuilder()
.overrideAuthority(GrpcUtil.authorityFromHostAndPort(
BAD_HOSTNAME, getPort()))
.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
})
.build();
TestServiceGrpc.TestServiceBlockingStub blockingStub =
TestServiceGrpc.newBlockingStub(channel);
blockingStub.emptyCall(Empty.getDefaultInstance());
channel.shutdown();
}
示例6: run
import io.grpc.ManagedChannel; //导入方法依赖的package包/类
/**
* Start the open loop client.
*/
public void run() throws Exception {
if (config == null) {
return;
}
config.channels = 1;
config.directExecutor = true;
ManagedChannel ch = config.newChannel();
SimpleRequest req = config.newRequest();
LoadGenerationWorker worker =
new LoadGenerationWorker(ch, req, config.targetQps, config.duration);
final long start = System.nanoTime();
Histogram histogram = worker.call();
final long end = System.nanoTime();
printStats(histogram, end - start);
if (config.histogramFile != null) {
saveHistogram(histogram, config.histogramFile);
}
ch.shutdown();
}
示例7: oobChannelsNoConnectionShutdown
import io.grpc.ManagedChannel; //导入方法依赖的package包/类
@Test
public void oobChannelsNoConnectionShutdown() {
createChannel(new FakeNameResolverFactory(true), NO_INTERCEPTOR);
ManagedChannel oob1 = helper.createOobChannel(addressGroup, "oob1Authority");
ManagedChannel oob2 = helper.createOobChannel(addressGroup, "oob2Authority");
channel.shutdown();
verify(mockLoadBalancer).shutdown();
oob1.shutdown();
assertTrue(oob1.isTerminated());
assertFalse(channel.isTerminated());
oob2.shutdown();
assertTrue(oob2.isTerminated());
assertTrue(channel.isTerminated());
verify(mockTransportFactory, never()).newClientTransport(any(SocketAddress.class), anyString(),
anyString(), any(ProxyParameters.class));
}
示例8: uniqueSessionIdPerChannel
import io.grpc.ManagedChannel; //导入方法依赖的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();
}
}
示例9: interceptorThrowsIfMissingTransportFilter
import io.grpc.ManagedChannel; //导入方法依赖的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();
}
}
示例10: perSessionShouldFailMissingTransportFilter
import io.grpc.ManagedChannel; //导入方法依赖的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();
}
}
示例11: close
import io.grpc.ManagedChannel; //导入方法依赖的package包/类
synchronized void close() {
ManagedChannel channel = channelRef.get();
if (channel != null) {
channel.shutdown();
}
executorService.shutdownNow();
}
示例12: shutdownChannel
import io.grpc.ManagedChannel; //导入方法依赖的package包/类
/**
* Shuts down the given channel if not <code>null</code>, waiting for up to 1 second.
*
* @param channel the channel to shut down
*/
public static void shutdownChannel(final ManagedChannel channel) {
if (channel != null) {
channel.shutdown();
try {
channel.awaitTermination(1, TimeUnit.SECONDS);
} catch (final InterruptedException e) {
// silently swallow exception
}
}
}
示例13: clearChannels
import io.grpc.ManagedChannel; //导入方法依赖的package包/类
private void clearChannels(String client)
{
Map<String,ManagedChannel> currentChannels = channels.put(client, new ConcurrentHashMap<String,ManagedChannel>());
if (currentChannels != null)
for (ManagedChannel ch : currentChannels.values())
{
ch.shutdown();
}
}
示例14: hostnameVerifierWithCorrectHostname
import io.grpc.ManagedChannel; //导入方法依赖的package包/类
@Test
public void hostnameVerifierWithCorrectHostname() throws Exception {
ManagedChannel channel = createChannelBuilder()
.overrideAuthority(GrpcUtil.authorityFromHostAndPort(
TestUtils.TEST_SERVER_HOST, getPort()))
.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return false;
}
})
.build();
TestServiceGrpc.TestServiceBlockingStub blockingStub =
TestServiceGrpc.newBlockingStub(channel);
Throwable actualThrown = null;
try {
blockingStub.emptyCall(Empty.getDefaultInstance());
} catch (Throwable t) {
actualThrown = t;
}
assertNotNull("The rpc should have been failed due to hostname verification", actualThrown);
Throwable cause = Throwables.getRootCause(actualThrown);
assertTrue(
"Failed by unexpected exception: " + cause, cause instanceof SSLPeerUnverifiedException);
channel.shutdown();
}
示例15: main
import io.grpc.ManagedChannel; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
// Connect to the sever
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", PORT).usePlaintext(true).build();
ReactorChatGrpc.ReactorChatStub stub = ReactorChatGrpc.newReactorStub(channel);
CountDownLatch done = new CountDownLatch(1);
ConsoleReader console = new ConsoleReader();
// Prompt the user for their name
console.println("Press ctrl+D to quit");
String author = console.readLine("Who are you? > ");
stub.postMessage(toMessage(author, author + " joined.")).subscribe();
// Subscribe to incoming messages
Disposable chatSubscription = stub.getMessages(Mono.just(Empty.getDefaultInstance())).subscribe(
message -> {
// Don't re-print our own messages
if (!message.getAuthor().equals(author)) {
printLine(console, message.getAuthor(), message.getMessage());
}
},
throwable -> {
printLine(console, "ERROR", throwable.getMessage());
done.countDown();
},
done::countDown
);
// Publish outgoing messages
Flux.fromIterable(new ConsoleIterator(console, author + " > "))
.map(msg -> toMessage(author, msg))
.flatMap(stub::postMessage)
.subscribe(
empty -> { },
throwable -> {
printLine(console, "ERROR", throwable.getMessage());
done.countDown();
},
done::countDown
);
// Wait for a signal to exit, then clean up
done.await();
stub.postMessage(toMessage(author, author + " left.")).subscribe();
chatSubscription.dispose();
channel.shutdown();
channel.awaitTermination(1, TimeUnit.SECONDS);
console.getTerminal().restore();
}