mirror of https://github.com/grpc/grpc-java.git
Use Providers in examples
This commit is contained in:
parent
bccaf07497
commit
41d93cfd59
|
|
@ -32,11 +32,11 @@
|
||||||
package io.grpc.examples.header;
|
package io.grpc.examples.header;
|
||||||
|
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
|
import io.grpc.ServerBuilder;
|
||||||
import io.grpc.ServerInterceptors;
|
import io.grpc.ServerInterceptors;
|
||||||
import io.grpc.examples.helloworld.GreeterGrpc;
|
import io.grpc.examples.helloworld.GreeterGrpc;
|
||||||
import io.grpc.examples.helloworld.HelloRequest;
|
import io.grpc.examples.helloworld.HelloRequest;
|
||||||
import io.grpc.examples.helloworld.HelloResponse;
|
import io.grpc.examples.helloworld.HelloResponse;
|
||||||
import io.grpc.netty.NettyServerBuilder;
|
|
||||||
import io.grpc.stub.StreamObserver;
|
import io.grpc.stub.StreamObserver;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
@ -53,9 +53,11 @@ public class CustomHeaderServer {
|
||||||
private Server server;
|
private Server server;
|
||||||
|
|
||||||
private void start() throws Exception {
|
private void start() throws Exception {
|
||||||
server = NettyServerBuilder.forPort(port).addService(ServerInterceptors
|
server = ServerBuilder.forPort(port)
|
||||||
.intercept(GreeterGrpc.bindService(new GreeterImpl()), new HeaderServerInterceptor()))
|
.addService(ServerInterceptors.intercept(
|
||||||
.build().start();
|
GreeterGrpc.bindService(new GreeterImpl()), new HeaderServerInterceptor()))
|
||||||
|
.build()
|
||||||
|
.start();
|
||||||
logger.info("Server started, listening on " + port);
|
logger.info("Server started, listening on " + port);
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@
|
||||||
package io.grpc.examples.helloworld;
|
package io.grpc.examples.helloworld;
|
||||||
|
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.netty.NettyServerBuilder;
|
import io.grpc.ServerBuilder;
|
||||||
import io.grpc.stub.StreamObserver;
|
import io.grpc.stub.StreamObserver;
|
||||||
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
@ -48,9 +48,10 @@ public class HelloWorldServer {
|
||||||
private Server server;
|
private Server server;
|
||||||
|
|
||||||
private void start() throws Exception {
|
private void start() throws Exception {
|
||||||
server = NettyServerBuilder.forPort(port)
|
server = ServerBuilder.forPort(port)
|
||||||
.addService(GreeterGrpc.bindService(new GreeterImpl()))
|
.addService(GreeterGrpc.bindService(new GreeterImpl()))
|
||||||
.build().start();
|
.build()
|
||||||
|
.start();
|
||||||
logger.info("Server started, listening on " + port);
|
logger.info("Server started, listening on " + port);
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -41,7 +41,7 @@ import static java.lang.Math.toRadians;
|
||||||
import static java.util.concurrent.TimeUnit.NANOSECONDS;
|
import static java.util.concurrent.TimeUnit.NANOSECONDS;
|
||||||
|
|
||||||
import io.grpc.Server;
|
import io.grpc.Server;
|
||||||
import io.grpc.netty.NettyServerBuilder;
|
import io.grpc.ServerBuilder;
|
||||||
import io.grpc.stub.StreamObserver;
|
import io.grpc.stub.StreamObserver;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
@ -63,7 +63,7 @@ public class RouteGuideServer {
|
||||||
|
|
||||||
private final int port;
|
private final int port;
|
||||||
private final Collection<Feature> features;
|
private final Collection<Feature> features;
|
||||||
private Server grpcServer;
|
private Server server;
|
||||||
|
|
||||||
public RouteGuideServer(int port) {
|
public RouteGuideServer(int port) {
|
||||||
this(port, RouteGuideUtil.getDefaultFeaturesFile());
|
this(port, RouteGuideUtil.getDefaultFeaturesFile());
|
||||||
|
|
@ -81,9 +81,10 @@ public class RouteGuideServer {
|
||||||
|
|
||||||
/** Start serving requests. */
|
/** Start serving requests. */
|
||||||
public void start() throws IOException {
|
public void start() throws IOException {
|
||||||
grpcServer = NettyServerBuilder.forPort(port)
|
server = ServerBuilder.forPort(port)
|
||||||
.addService(RouteGuideGrpc.bindService(new RouteGuideService(features)))
|
.addService(RouteGuideGrpc.bindService(new RouteGuideService(features)))
|
||||||
.build().start();
|
.build()
|
||||||
|
.start();
|
||||||
logger.info("Server started, listening on " + port);
|
logger.info("Server started, listening on " + port);
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -98,8 +99,8 @@ public class RouteGuideServer {
|
||||||
|
|
||||||
/** Stop serving requests and shutdown resources. */
|
/** Stop serving requests and shutdown resources. */
|
||||||
public void stop() {
|
public void stop() {
|
||||||
if (grpcServer != null) {
|
if (server != null) {
|
||||||
grpcServer.shutdown();
|
server.shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,8 +108,8 @@ public class RouteGuideServer {
|
||||||
* Await termination on the main thread since the grpc library uses daemon threads.
|
* Await termination on the main thread since the grpc library uses daemon threads.
|
||||||
*/
|
*/
|
||||||
private void blockUntilShutdown() throws InterruptedException {
|
private void blockUntilShutdown() throws InterruptedException {
|
||||||
if (grpcServer != null) {
|
if (server != null) {
|
||||||
grpcServer.awaitTermination();
|
server.awaitTermination();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -181,7 +181,6 @@ public final class NettyChannelBuilder
|
||||||
}
|
}
|
||||||
|
|
||||||
private ProtocolNegotiator createProtocolNegotiator() {
|
private ProtocolNegotiator createProtocolNegotiator() {
|
||||||
ProtocolNegotiator negotiator;
|
|
||||||
switch (negotiationType) {
|
switch (negotiationType) {
|
||||||
case PLAINTEXT:
|
case PLAINTEXT:
|
||||||
return ProtocolNegotiators.plaintext();
|
return ProtocolNegotiators.plaintext();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue