Class ExecutorInstrumentation
- java.lang.Object
-
- graphql.execution.instrumentation.SimplePerformantInstrumentation
-
- graphql.execution.instrumentation.threadpools.ExecutorInstrumentation
-
- All Implemented Interfaces:
Instrumentation
@Beta public class ExecutorInstrumentation extends SimplePerformantInstrumentation
This instrumentation can be used to control on what thread calls toDataFetcher
s happen on.If your data fetching is inherently IO bound then you could use a IO oriented thread pool for your fetches and transfer control back to a CPU oriented thread pool and allow graphql-java code to run the post-processing of results there.
An IO oriented thread pool is typically a multiple of
Runtime.availableProcessors()
while a CPU oriented thread pool is typically no more thanRuntime.availableProcessors()
.The instrumentation will use the
Instrumentation.instrumentDataFetcher(DataFetcher, InstrumentationFieldFetchParameters, InstrumentationState)
method to change your data fetchers, so they are executed on a thread pool dedicated to fetching (if you provide one).Once the data fetcher value is returns it will transfer control back to a processing thread pool (if you provide one).
This code uses
CompletableFuture.supplyAsync(Supplier, Executor)
andCompletableFuture.thenApplyAsync(Function, Executor)
to transfer control between thread pools.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ExecutorInstrumentation.Builder
-
Field Summary
-
Fields inherited from class graphql.execution.instrumentation.SimplePerformantInstrumentation
INSTANCE
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.concurrent.Executor
getFetchExecutor()
java.util.concurrent.Executor
getProcessingExecutor()
@NotNull DataFetcher<?>
instrumentDataFetcher(DataFetcher<?> originalDataFetcher, InstrumentationFieldFetchParameters parameters, InstrumentationState state)
This is called to instrument aDataFetcher
just before it is used to fetch a field, allowing you to adjust what information is passed back or record information about specific data fetches.static ExecutorInstrumentation.Builder
newThreadPoolExecutionInstrumentation()
-
Methods inherited from class graphql.execution.instrumentation.SimplePerformantInstrumentation
beginExecuteObject, beginExecuteOperation, beginExecution, beginExecutionStrategy, beginFieldCompletion, beginFieldExecution, beginFieldFetch, beginFieldListCompletion, beginParse, beginSubscribedFieldEvent, beginValidation, createStateAsync, instrumentDocumentAndVariables, instrumentExecutionContext, instrumentExecutionInput, instrumentExecutionResult, instrumentSchema
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface graphql.execution.instrumentation.Instrumentation
beginDeferredField
-
-
-
-
Method Detail
-
getFetchExecutor
public java.util.concurrent.Executor getFetchExecutor()
-
getProcessingExecutor
public java.util.concurrent.Executor getProcessingExecutor()
-
newThreadPoolExecutionInstrumentation
public static ExecutorInstrumentation.Builder newThreadPoolExecutionInstrumentation()
-
instrumentDataFetcher
@NotNull public @NotNull DataFetcher<?> instrumentDataFetcher(DataFetcher<?> originalDataFetcher, InstrumentationFieldFetchParameters parameters, InstrumentationState state)
Description copied from interface:Instrumentation
This is called to instrument aDataFetcher
just before it is used to fetch a field, allowing you to adjust what information is passed back or record information about specific data fetches. Note the same data fetcher instance maybe presented to you many times and that data fetcher implementations widely vary.- Specified by:
instrumentDataFetcher
in interfaceInstrumentation
- Overrides:
instrumentDataFetcher
in classSimplePerformantInstrumentation
- Parameters:
originalDataFetcher
- the data fetcher about to be usedparameters
- the parameters describing the field to be fetchedstate
- the state created during the call toInstrumentation.createStateAsync(InstrumentationCreateStateParameters)
- Returns:
- a non-null instrumented DataFetcher, the default is to return to the same object
-
-