Skip to content

Commit c4e561f

Browse files
authored
Merge pull request #350 from graphql-java-kickstart/async-task-decorator
Support async task decorator
2 parents c75bd30 + d155920 commit c4e561f

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package graphql.kickstart.servlet;
2+
3+
public interface AsyncTaskDecorator {
4+
5+
Runnable decorate(Runnable runnable);
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package graphql.kickstart.servlet;
2+
3+
import java.util.concurrent.Executor;
4+
import lombok.NonNull;
5+
import lombok.RequiredArgsConstructor;
6+
7+
@RequiredArgsConstructor
8+
class AsyncTaskExecutor implements Executor {
9+
10+
private final Executor executor;
11+
private final AsyncTaskDecorator taskDecorator;
12+
13+
@Override
14+
public void execute(@NonNull Runnable command) {
15+
if (taskDecorator != null) {
16+
Runnable decorated = taskDecorator.decorate(command);
17+
executor.execute(decorated);
18+
} else {
19+
executor.execute(command);
20+
}
21+
}
22+
}

graphql-java-servlet/src/main/java/graphql/kickstart/servlet/GraphQLConfiguration.java

+39-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020
import java.util.concurrent.Executor;
21-
import java.util.concurrent.Executors;
21+
import java.util.concurrent.LinkedBlockingQueue;
22+
import java.util.concurrent.ThreadPoolExecutor;
23+
import java.util.concurrent.TimeUnit;
2224
import java.util.function.Supplier;
2325
import lombok.Getter;
2426

@@ -142,7 +144,10 @@ public static class Builder {
142144
private Supplier<BatchInputPreProcessor> batchInputPreProcessorSupplier =
143145
NoOpBatchInputPreProcessor::new;
144146
private GraphQLResponseCacheManager responseCacheManager;
145-
private Executor asyncExecutor = Executors.newCachedThreadPool();
147+
private int asyncCorePoolSize = 10;
148+
private int asyncMaxPoolSize = 200;
149+
private Executor asyncExecutor;
150+
private AsyncTaskDecorator asyncTaskDecorator;
146151

147152
private Builder(GraphQLInvocationInputFactory.Builder invocationInputFactoryBuilder) {
148153
this.invocationInputFactoryBuilder = invocationInputFactoryBuilder;
@@ -203,6 +208,16 @@ public Builder with(Executor asyncExecutor) {
203208
return this;
204209
}
205210

211+
public Builder asyncCorePoolSize(int asyncCorePoolSize) {
212+
this.asyncCorePoolSize = asyncCorePoolSize;
213+
return this;
214+
}
215+
216+
public Builder asyncMaxPoolSize(int asyncMaxPoolSize) {
217+
this.asyncMaxPoolSize = asyncMaxPoolSize;
218+
return this;
219+
}
220+
206221
public Builder with(ContextSetting contextSetting) {
207222
if (contextSetting != null) {
208223
this.contextSetting = contextSetting;
@@ -229,6 +244,27 @@ public Builder with(GraphQLResponseCacheManager responseCache) {
229244
return this;
230245
}
231246

247+
public Builder with(AsyncTaskDecorator asyncTaskDecorator) {
248+
this.asyncTaskDecorator = asyncTaskDecorator;
249+
return this;
250+
}
251+
252+
private Executor getAsyncExecutor() {
253+
if (asyncExecutor != null) {
254+
return asyncExecutor;
255+
}
256+
return new ThreadPoolExecutor(
257+
asyncCorePoolSize,
258+
asyncMaxPoolSize,
259+
60,
260+
TimeUnit.SECONDS,
261+
new LinkedBlockingQueue<>(Integer.MAX_VALUE));
262+
}
263+
264+
private Executor getAsyncTaskExecutor() {
265+
return new AsyncTaskExecutor(getAsyncExecutor(), asyncTaskDecorator);
266+
}
267+
232268
public GraphQLConfiguration build() {
233269
return new GraphQLConfiguration(
234270
this.invocationInputFactory != null
@@ -243,7 +279,7 @@ public GraphQLConfiguration build() {
243279
contextSetting,
244280
batchInputPreProcessorSupplier,
245281
responseCacheManager,
246-
asyncExecutor);
282+
getAsyncTaskExecutor());
247283
}
248284
}
249285
}

0 commit comments

Comments
 (0)