Use Executor in stable builder APIs instead of ExecutorService

This commit is contained in:
Louis Ryan 2015-09-03 14:27:48 -07:00
parent 07a7279742
commit 6a782a035e
7 changed files with 21 additions and 20 deletions

View File

@ -39,7 +39,7 @@ import io.grpc.okhttp.NegotiationType;
import io.grpc.okhttp.OkHttpChannelBuilder; import io.grpc.okhttp.OkHttpChannelBuilder;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executor;
/** /**
* A {@link io.grpc.ManagedChannelBuilder} that provides a stable interface for producing * A {@link io.grpc.ManagedChannelBuilder} that provides a stable interface for producing
@ -77,7 +77,7 @@ public class AndroidChannelBuilder extends ManagedChannelBuilder<AndroidChannelB
} }
@Override @Override
public AndroidChannelBuilder executor(ExecutorService executor) { public AndroidChannelBuilder executor(Executor executor) {
baseBuilder.executor(executor); baseBuilder.executor(executor);
return this; return this;
} }

View File

@ -32,7 +32,7 @@
package io.grpc; package io.grpc;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executor;
/** /**
* A builder for {@link ManagedChannel} instances. * A builder for {@link ManagedChannel} instances.
@ -50,7 +50,7 @@ public abstract class ManagedChannelBuilder<T extends ManagedChannelBuilder<T>>
* <p>The channel won't take ownership of the given executor. It's caller's responsibility to * <p>The channel won't take ownership of the given executor. It's caller's responsibility to
* shut down the executor when it's desired. * shut down the executor when it's desired.
*/ */
public abstract T executor(ExecutorService executor); public abstract T executor(Executor executor);
/** /**
* Adds interceptors that will be called before the channel performs its real work. This is * Adds interceptors that will be called before the channel performs its real work. This is

View File

@ -31,7 +31,7 @@
package io.grpc; package io.grpc;
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executor;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -50,7 +50,7 @@ public abstract class ServerBuilder<T extends ServerBuilder<T>> {
* <p>The server won't take ownership of the given executor. It's caller's responsibility to * <p>The server won't take ownership of the given executor. It's caller's responsibility to
* shut down the executor when it's desired. * shut down the executor when it's desired.
*/ */
public abstract T executor(@Nullable ExecutorService executor); public abstract T executor(@Nullable Executor executor);
/** /**
* Adds a service implementation to the handler registry. * Adds a service implementation to the handler registry.

View File

@ -38,7 +38,7 @@ import io.grpc.ManagedChannelBuilder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executor;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -51,14 +51,14 @@ public abstract class AbstractManagedChannelImplBuilder
<T extends AbstractManagedChannelImplBuilder<T>> extends ManagedChannelBuilder<T> { <T extends AbstractManagedChannelImplBuilder<T>> extends ManagedChannelBuilder<T> {
@Nullable @Nullable
private ExecutorService executor; private Executor executor;
private final List<ClientInterceptor> interceptors = new ArrayList<ClientInterceptor>(); private final List<ClientInterceptor> interceptors = new ArrayList<ClientInterceptor>();
@Nullable @Nullable
private String userAgent; private String userAgent;
@Override @Override
public final T executor(ExecutorService executor) { public final T executor(Executor executor) {
this.executor = executor; this.executor = executor;
return thisT(); return thisT();
} }

View File

@ -40,7 +40,7 @@ import io.grpc.MutableHandlerRegistryImpl;
import io.grpc.ServerBuilder; import io.grpc.ServerBuilder;
import io.grpc.ServerServiceDefinition; import io.grpc.ServerServiceDefinition;
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executor;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -54,7 +54,7 @@ public abstract class AbstractServerImplBuilder<T extends AbstractServerImplBuil
private final HandlerRegistry registry; private final HandlerRegistry registry;
@Nullable @Nullable
private ExecutorService executor; private Executor executor;
/** /**
* Constructs using a given handler registry. * Constructs using a given handler registry.
@ -71,7 +71,7 @@ public abstract class AbstractServerImplBuilder<T extends AbstractServerImplBuil
} }
@Override @Override
public final T executor(@Nullable ExecutorService executor) { public final T executor(@Nullable Executor executor) {
this.executor = executor; this.executor = executor;
return thisT(); return thisT();
} }

View File

@ -67,7 +67,7 @@ public final class ManagedChannelImpl extends ManagedChannel {
private static final Logger log = Logger.getLogger(ManagedChannelImpl.class.getName()); private static final Logger log = Logger.getLogger(ManagedChannelImpl.class.getName());
private final ClientTransportFactory transportFactory; private final ClientTransportFactory transportFactory;
private final ExecutorService executor; private final Executor executor;
private final boolean usingSharedExecutor; private final boolean usingSharedExecutor;
private final String userAgent; private final String userAgent;
private final Object lock = new Object(); private final Object lock = new Object();
@ -114,7 +114,7 @@ public final class ManagedChannelImpl extends ManagedChannel {
} }
}; };
ManagedChannelImpl(ClientTransportFactory transportFactory, @Nullable ExecutorService executor, ManagedChannelImpl(ClientTransportFactory transportFactory, @Nullable Executor executor,
@Nullable String userAgent, List<ClientInterceptor> interceptors) { @Nullable String userAgent, List<ClientInterceptor> interceptors) {
this.transportFactory = transportFactory; this.transportFactory = transportFactory;
this.userAgent = userAgent; this.userAgent = userAgent;
@ -387,7 +387,7 @@ public final class ManagedChannelImpl extends ManagedChannel {
*/ */
private void onChannelTerminated() { private void onChannelTerminated() {
if (usingSharedExecutor) { if (usingSharedExecutor) {
SharedResourceHolder.release(GrpcUtil.SHARED_CHANNEL_EXECUTOR, executor); SharedResourceHolder.release(GrpcUtil.SHARED_CHANNEL_EXECUTOR, (ExecutorService) executor);
} }
// Release the transport factory so that it can deallocate any resources. // Release the transport factory so that it can deallocate any resources.
transportFactory.release(); transportFactory.release();

View File

@ -49,6 +49,7 @@ import io.grpc.internal.ClientTransportFactory;
import io.grpc.internal.SharedResourceHolder; import io.grpc.internal.SharedResourceHolder;
import io.grpc.internal.SharedResourceHolder.Resource; import io.grpc.internal.SharedResourceHolder.Resource;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -96,7 +97,7 @@ public final class OkHttpChannelBuilder extends
return new OkHttpChannelBuilder(host, port); return new OkHttpChannelBuilder(host, port);
} }
private ExecutorService transportExecutor; private Executor transportExecutor;
private final String host; private final String host;
private final int port; private final int port;
private String authorityHost; private String authorityHost;
@ -117,7 +118,7 @@ public final class OkHttpChannelBuilder extends
* <p>The channel does not take ownership of the given executor. It is the caller' responsibility * <p>The channel does not take ownership of the given executor. It is the caller' responsibility
* to shutdown the executor when appropriate. * to shutdown the executor when appropriate.
*/ */
public OkHttpChannelBuilder transportExecutor(@Nullable ExecutorService transportExecutor) { public OkHttpChannelBuilder transportExecutor(@Nullable Executor transportExecutor) {
this.transportExecutor = transportExecutor; this.transportExecutor = transportExecutor;
return this; return this;
} }
@ -200,7 +201,7 @@ public final class OkHttpChannelBuilder extends
private final String host; private final String host;
private final int port; private final int port;
private final String authorityHost; private final String authorityHost;
private final ExecutorService executor; private final Executor executor;
private final boolean usingSharedExecutor; private final boolean usingSharedExecutor;
private final SSLSocketFactory socketFactory; private final SSLSocketFactory socketFactory;
private final ConnectionSpec connectionSpec; private final ConnectionSpec connectionSpec;
@ -210,7 +211,7 @@ public final class OkHttpChannelBuilder extends
private OkHttpTransportFactory(String host, private OkHttpTransportFactory(String host,
int port, int port,
String authorityHost, String authorityHost,
ExecutorService executor, Executor executor,
SSLSocketFactory socketFactory, SSLSocketFactory socketFactory,
ConnectionSpec connectionSpec, ConnectionSpec connectionSpec,
int maxMessageSize) { int maxMessageSize) {
@ -245,7 +246,7 @@ public final class OkHttpChannelBuilder extends
@Override @Override
protected void deallocate() { protected void deallocate() {
if (usingSharedExecutor) { if (usingSharedExecutor) {
SharedResourceHolder.release(SHARED_EXECUTOR, executor); SharedResourceHolder.release(SHARED_EXECUTOR, (ExecutorService) executor);
} }
} }
} }