@Beta public class ExecutorInstrumentation extends SimpleInstrumentation
DataFetcher
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 than Runtime.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)
and CompletableFuture.thenApplyAsync(Function, Executor)
to transfer
control between thread pools.
Modifier and Type | Class and Description |
---|---|
static class |
ExecutorInstrumentation.Builder |
INSTANCE
Modifier and Type | Method and 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 a
DataFetcher 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() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
beginExecuteOperation, beginExecuteOperation, beginExecution, beginExecution, beginExecutionStrategy, beginExecutionStrategy, beginField, beginField, beginFieldComplete, beginFieldComplete, beginFieldFetch, beginFieldFetch, beginFieldListComplete, beginFieldListComplete, beginParse, beginParse, beginSubscribedFieldEvent, beginSubscribedFieldEvent, beginValidation, beginValidation, createState, createState, instrumentDataFetcher, instrumentDocumentAndVariables, instrumentDocumentAndVariables, instrumentExecutionContext, instrumentExecutionContext, instrumentExecutionInput, instrumentExecutionInput, instrumentExecutionResult, instrumentExecutionResult, instrumentSchema, instrumentSchema
public java.util.concurrent.Executor getFetchExecutor()
public java.util.concurrent.Executor getProcessingExecutor()
public static ExecutorInstrumentation.Builder newThreadPoolExecutionInstrumentation()
@NotNull public @NotNull DataFetcher<?> instrumentDataFetcher(DataFetcher<?> originalDataFetcher, InstrumentationFieldFetchParameters parameters, InstrumentationState state)
Instrumentation
DataFetcher
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.originalDataFetcher
- the data fetcher about to be usedparameters
- the parameters describing the field to be fetchedstate
- the state created during the call to Instrumentation.createState(InstrumentationCreateStateParameters)