Convenient builders for channels and servers.
Right now only netty-based builders are implemented. OkHttp based
builders will be created separately.
Minimal example of creating a stub:
ChannelImpl channel = NettyChannelBuilder
.newBuilder(new InetSocketAddress("localhost", 8980))
.buildAndWaitForRunning();
StockBlockingStub stub = StockGrpc.newStub(channel);
Minimal example of creating and starting a server:
ServerImpl server = NettyServerBuilder.newBuilder(8980)
.addService(StockGrpc.bindService(new StockServer()))
.buildAndWaitForRunning();
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=77787502
diff --git a/core/src/main/java/com/google/net/stubby/AbstractChannelBuilder.java b/core/src/main/java/com/google/net/stubby/AbstractChannelBuilder.java
new file mode 100644
index 0000000..7f74d45
--- /dev/null
+++ b/core/src/main/java/com/google/net/stubby/AbstractChannelBuilder.java
@@ -0,0 +1,47 @@
+package com.google.net.stubby;
+
+import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.MoreExecutors;
+import com.google.common.util.concurrent.Service;
+import com.google.net.stubby.newtransport.ClientTransportFactory;
+
+import java.util.concurrent.ExecutorService;
+
+import javax.annotation.Nullable;
+
+/**
+ * The base class for channel builders.
+ */
+public abstract class AbstractChannelBuilder<BuilderT extends AbstractChannelBuilder>
+ extends AbstractServiceBuilder<ChannelImpl, BuilderT> {
+
+ @Override
+ protected final ChannelImpl buildImpl(ExecutorService executor) {
+ ChannelEssentials essentials = buildEssentials();
+ ChannelImpl channel = new ChannelImpl(essentials.transportFactory, executor);
+ if (essentials.listener != null) {
+ channel.addListener(essentials.listener, MoreExecutors.directExecutor());
+ }
+ return channel;
+ }
+
+ /**
+ * The essentials required for creating a channel.
+ */
+ protected static class ChannelEssentials {
+ final ClientTransportFactory transportFactory;
+ @Nullable final Service.Listener listener;
+
+ /**
+ * @param transportFactory the created channel uses this factory to create transports
+ * @param listener will be called at the channel's life-cycle events
+ */
+ public ChannelEssentials(ClientTransportFactory transportFactory,
+ @Nullable Service.Listener listener) {
+ this.transportFactory = Preconditions.checkNotNull(transportFactory);
+ this.listener = listener;
+ }
+ }
+
+ protected abstract ChannelEssentials buildEssentials();
+}