mirror of https://github.com/grpc/grpc-java.git
Make Route Guide client and server accept channel and server builers, respectively.
This commit is contained in:
parent
8e1fba7c90
commit
c85e04698f
|
|
@ -58,11 +58,14 @@ public class RouteGuideClient {
|
||||||
private final RouteGuideBlockingStub blockingStub;
|
private final RouteGuideBlockingStub blockingStub;
|
||||||
private final RouteGuideStub asyncStub;
|
private final RouteGuideStub asyncStub;
|
||||||
|
|
||||||
/** Construct client for accessing RoutGuide server at {@code host:port}. */
|
/** Construct client for accessing RouteGuide server at {@code host:port}. */
|
||||||
public RouteGuideClient(String host, int port) {
|
public RouteGuideClient(String host, int port) {
|
||||||
channel = ManagedChannelBuilder.forAddress(host, port)
|
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext(true));
|
||||||
.usePlaintext(true)
|
}
|
||||||
.build();
|
|
||||||
|
/** Construct client for accessing RouteGuide server using the existing channel. */
|
||||||
|
public RouteGuideClient(ManagedChannelBuilder<?> channelBuilder) {
|
||||||
|
channel = channelBuilder.build();
|
||||||
blockingStub = RouteGuideGrpc.newBlockingStub(channel);
|
blockingStub = RouteGuideGrpc.newBlockingStub(channel);
|
||||||
asyncStub = RouteGuideGrpc.newStub(channel);
|
asyncStub = RouteGuideGrpc.newStub(channel);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -62,29 +62,27 @@ public class RouteGuideServer {
|
||||||
private static final Logger logger = Logger.getLogger(RouteGuideServer.class.getName());
|
private static final Logger logger = Logger.getLogger(RouteGuideServer.class.getName());
|
||||||
|
|
||||||
private final int port;
|
private final int port;
|
||||||
private final Collection<Feature> features;
|
private final Server server;
|
||||||
private Server server;
|
|
||||||
|
|
||||||
public RouteGuideServer(int port) {
|
public RouteGuideServer(int port) throws IOException {
|
||||||
this(port, RouteGuideUtil.getDefaultFeaturesFile());
|
this(port, RouteGuideUtil.getDefaultFeaturesFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Create a RouteGuide server listening on {@code port} using {@code featureFile} database. */
|
/** Create a RouteGuide server listening on {@code port} using {@code featureFile} database. */
|
||||||
public RouteGuideServer(int port, URL featureFile) {
|
public RouteGuideServer(int port, URL featureFile) throws IOException {
|
||||||
try {
|
this(ServerBuilder.forPort(port), port, RouteGuideUtil.parseFeatures(featureFile));
|
||||||
this.port = port;
|
|
||||||
features = RouteGuideUtil.parseFeatures(featureFile);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Create a RouteGuide server using serverBuilder as a base and features as data. */
|
||||||
|
public RouteGuideServer(ServerBuilder<?> serverBuilder, int port, Collection<Feature> features) {
|
||||||
|
this.port = port;
|
||||||
|
server = serverBuilder.addService(RouteGuideGrpc.bindService(new RouteGuideService(features)))
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Start serving requests. */
|
/** Start serving requests. */
|
||||||
public void start() throws IOException {
|
public void start() throws IOException {
|
||||||
server = ServerBuilder.forPort(port)
|
server.start();
|
||||||
.addService(RouteGuideGrpc.bindService(new RouteGuideService(features)))
|
|
||||||
.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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue