|
15 | 15 | */ |
16 | 16 | package feign; |
17 | 17 |
|
| 18 | + |
| 19 | +import dagger.Lazy; |
18 | 20 | import dagger.ObjectGraph; |
19 | 21 | import dagger.Provides; |
20 | 22 | import feign.Logger.NoOpLogger; |
|
23 | 25 | import feign.codec.Decoder; |
24 | 26 | import feign.codec.Encoder; |
25 | 27 | import feign.codec.ErrorDecoder; |
| 28 | +import feign.codec.IncrementalDecoder; |
26 | 29 |
|
| 30 | +import javax.inject.Named; |
| 31 | +import javax.inject.Singleton; |
27 | 32 | import javax.net.ssl.SSLSocketFactory; |
| 33 | +import java.io.Closeable; |
28 | 34 | import java.lang.reflect.Method; |
29 | 35 | import java.util.ArrayList; |
30 | 36 | import java.util.Collections; |
31 | 37 | import java.util.List; |
32 | 38 | import java.util.Set; |
| 39 | +import java.util.concurrent.Executor; |
| 40 | +import java.util.concurrent.ExecutorService; |
| 41 | +import java.util.concurrent.Executors; |
| 42 | +import java.util.concurrent.ThreadFactory; |
| 43 | + |
| 44 | +import static java.lang.Thread.MIN_PRIORITY; |
33 | 45 |
|
34 | 46 | /** |
35 | 47 | * Feign's purpose is to ease development against http apis that feign |
|
38 | 50 | * In implementation, Feign is a {@link Feign#newInstance factory} for |
39 | 51 | * generating {@link Target targeted} http apis. |
40 | 52 | */ |
41 | | -public abstract class Feign { |
| 53 | +public abstract class Feign implements Closeable { |
42 | 54 |
|
43 | 55 | /** |
44 | 56 | * Returns a new instance of an HTTP API, defined by annotations in the |
@@ -119,6 +131,26 @@ public static class Defaults { |
119 | 131 | @Provides Set<Decoder> noDecoders() { |
120 | 132 | return Collections.emptySet(); |
121 | 133 | } |
| 134 | + |
| 135 | + @Provides Set<IncrementalDecoder> noIncrementalDecoders() { |
| 136 | + return Collections.emptySet(); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Used for both http invocation and decoding when incrementalCallbacks are used. |
| 141 | + */ |
| 142 | + @Provides @Singleton @Named("http") Executor httpExecutor() { |
| 143 | + return Executors.newCachedThreadPool(new ThreadFactory() { |
| 144 | + @Override public Thread newThread(final Runnable r) { |
| 145 | + return new Thread(new Runnable() { |
| 146 | + @Override public void run() { |
| 147 | + Thread.currentThread().setPriority(MIN_PRIORITY); |
| 148 | + r.run(); |
| 149 | + } |
| 150 | + }, MethodHandler.IDLE_THREAD_NAME); |
| 151 | + } |
| 152 | + }); |
| 153 | + } |
122 | 154 | } |
123 | 155 |
|
124 | 156 | /** |
@@ -162,7 +194,16 @@ private static List<Object> modulesForGraph(Object... modules) { |
162 | 194 | return modulesForGraph; |
163 | 195 | } |
164 | 196 |
|
165 | | - Feign() { |
| 197 | + private final Lazy<Executor> httpExecutor; |
166 | 198 |
|
| 199 | + Feign(Lazy<Executor> httpExecutor) { |
| 200 | + this.httpExecutor = httpExecutor; |
| 201 | + } |
| 202 | + |
| 203 | + @Override public void close() { |
| 204 | + Executor e = httpExecutor.get(); |
| 205 | + if (e instanceof ExecutorService) { |
| 206 | + ExecutorService.class.cast(e).shutdownNow(); |
| 207 | + } |
167 | 208 | } |
168 | 209 | } |
0 commit comments