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 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) {
|
||||
channel = ManagedChannelBuilder.forAddress(host, port)
|
||||
.usePlaintext(true)
|
||||
.build();
|
||||
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext(true));
|
||||
}
|
||||
|
||||
/** Construct client for accessing RouteGuide server using the existing channel. */
|
||||
public RouteGuideClient(ManagedChannelBuilder<?> channelBuilder) {
|
||||
channel = channelBuilder.build();
|
||||
blockingStub = RouteGuideGrpc.newBlockingStub(channel);
|
||||
asyncStub = RouteGuideGrpc.newStub(channel);
|
||||
}
|
||||
|
|
|
@ -62,29 +62,27 @@ public class RouteGuideServer {
|
|||
private static final Logger logger = Logger.getLogger(RouteGuideServer.class.getName());
|
||||
|
||||
private final int port;
|
||||
private final Collection<Feature> features;
|
||||
private Server server;
|
||||
private final Server server;
|
||||
|
||||
public RouteGuideServer(int port) {
|
||||
public RouteGuideServer(int port) throws IOException {
|
||||
this(port, RouteGuideUtil.getDefaultFeaturesFile());
|
||||
}
|
||||
|
||||
/** Create a RouteGuide server listening on {@code port} using {@code featureFile} database. */
|
||||
public RouteGuideServer(int port, URL featureFile) {
|
||||
try {
|
||||
this.port = port;
|
||||
features = RouteGuideUtil.parseFeatures(featureFile);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
public RouteGuideServer(int port, URL featureFile) throws IOException {
|
||||
this(ServerBuilder.forPort(port), port, RouteGuideUtil.parseFeatures(featureFile));
|
||||
}
|
||||
|
||||
/** 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. */
|
||||
public void start() throws IOException {
|
||||
server = ServerBuilder.forPort(port)
|
||||
.addService(RouteGuideGrpc.bindService(new RouteGuideService(features)))
|
||||
.build()
|
||||
.start();
|
||||
server.start();
|
||||
logger.info("Server started, listening on " + port);
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue