28
28
import java .io .IOException ;
29
29
import java .io .ObjectInputStream ;
30
30
import java .util .Objects ;
31
+ import java .util .concurrent .ExecutorService ;
31
32
import java .util .concurrent .ScheduledExecutorService ;
32
33
import java .util .concurrent .ScheduledThreadPoolExecutor ;
33
34
import java .util .concurrent .TimeUnit ;
@@ -50,7 +51,7 @@ public abstract class GrpcServiceOptions<ServiceT extends Service<OptionsT>, Ser
50
51
private final double timeoutMultiplier ;
51
52
private final int maxTimeout ;
52
53
53
- private transient ExecutorFactory executorFactory ;
54
+ private transient ExecutorFactory < ScheduledExecutorService > executorFactory ;
54
55
55
56
/**
56
57
* Shared thread pool executor.
@@ -73,30 +74,32 @@ public void close(ScheduledExecutorService instance) {
73
74
};
74
75
75
76
/**
76
- * An interface for {@link ScheduledExecutorService } factories. Implementations of this interface
77
- * can be used to provide an user-defined scheduled executor to execute requests. Any
78
- * implementation of this interface must override the {@code get()} method to return the desired
79
- * executor. The {@code release(executor)} method should be overriden to free resources used by
80
- * the executor (if needed) according to application's logic.
77
+ * An interface for {@link ExecutorService } factories. Implementations of this interface can be
78
+ * used to provide an user-defined executor to execute requests. Any implementation of this
79
+ * interface must override the {@code get()} method to return the desired executor. The
80
+ * {@code release(executor)} method should be overriden to free resources used by the executor (if
81
+ * needed) according to application's logic.
81
82
*
82
83
* <p>Implementation must provide a public no-arg constructor. Loading of a factory implementation
83
84
* is done via {@link java.util.ServiceLoader}.
85
+ *
86
+ * @param <T> the {@link ExecutorService} subclass created by this factory
84
87
*/
85
- public interface ExecutorFactory {
88
+ public interface ExecutorFactory < T extends ExecutorService > {
86
89
87
90
/**
88
- * Gets a scheduled executor service instance.
91
+ * Gets an executor service instance.
89
92
*/
90
- ScheduledExecutorService get ();
93
+ T get ();
91
94
92
95
/**
93
96
* Releases resources used by the executor and possibly shuts it down.
94
97
*/
95
- void release (ScheduledExecutorService executor );
98
+ void release (T executor );
96
99
}
97
100
98
101
@ VisibleForTesting
99
- static class DefaultExecutorFactory implements ExecutorFactory {
102
+ static class DefaultExecutorFactory implements ExecutorFactory < ScheduledExecutorService > {
100
103
101
104
private static final DefaultExecutorFactory INSTANCE = new DefaultExecutorFactory ();
102
105
@@ -148,7 +151,7 @@ protected Builder(GrpcServiceOptions<ServiceT, ServiceRpcT, OptionsT> options) {
148
151
*
149
152
* @return the builder
150
153
*/
151
- public B executorFactory (ExecutorFactory executorFactory ) {
154
+ public B executorFactory (ExecutorFactory < ScheduledExecutorService > executorFactory ) {
152
155
this .executorFactory = executorFactory ;
153
156
return self ();
154
157
}
@@ -192,6 +195,7 @@ public B maxTimeout(int maxTimeout) {
192
195
}
193
196
}
194
197
198
+ @ SuppressWarnings ("unchecked" )
195
199
protected GrpcServiceOptions (
196
200
Class <? extends ServiceFactory <ServiceT , OptionsT >> serviceFactoryClass ,
197
201
Class <? extends ServiceRpcFactory <ServiceRpcT , OptionsT >> rpcFactoryClass , Builder <ServiceT ,
@@ -208,7 +212,7 @@ protected GrpcServiceOptions(
208
212
/**
209
213
* Returns a scheduled executor service provider.
210
214
*/
211
- protected ExecutorFactory executorFactory () {
215
+ protected ExecutorFactory < ScheduledExecutorService > executorFactory () {
212
216
return executorFactory ;
213
217
}
214
218
0 commit comments