@ExperimentalApi public class ScheduledDataLoaderRegistry extends DataLoaderRegistry implements java.lang.AutoCloseable
DataLoaderRegistry
will use DispatchPredicate
s when dispatchAll()
is called
to test (for each DataLoader
in the registry) if a dispatch should proceed. If the predicate returns false, then a task is scheduled
to perform that predicate dispatch again via the ScheduledExecutorService
.
It;s possible to have a DispatchPredicate
per dataloader as well as a default DispatchPredicate
for the
whole ScheduledDataLoaderRegistry
.
This will continue to loop (test false and reschedule) until such time as the predicate returns true, in which case no rescheduling will occur, and you will need to call dispatch again to restart the process.
In the default mode, when tickerMode
is false, the registry will continue to loop (test false and reschedule) until such time as the predicate returns true, in which case
no rescheduling will occur, and you will need to call dispatch again to restart the process.
However, when tickerMode
is true, the registry will always reschedule continuously after the first ever call to dispatchAll()
.
This will allow you to chain together DataLoader
load calls like this :
CompletableFuture<String> future = dataLoaderA.load("A")
.thenCompose(value -> dataLoaderB.load(value));
However, it may mean your batching will not be as efficient as it might be. In environments
like graphql this might mean you are too eager in fetching. The DispatchPredicate
still runs to decide if
dispatch should happen however in ticker mode it will be continuously rescheduled.
When tickerMode
is true, you really SHOULD close the registry say at the end of a request otherwise you will leave a job
on the ScheduledExecutorService
that is continuously dispatching.
If you wanted to create a ScheduledDataLoaderRegistry that started a rescheduling immediately, just create one and
call rescheduleNow()
.
By default, it uses a Executors.newSingleThreadScheduledExecutor()
} to schedule the tasks. However, if you
are creating a ScheduledDataLoaderRegistry
per request you will want to look at sharing this ScheduledExecutorService
to avoid creating a new thread per registry created.
This code is currently marked as ExperimentalApi
Modifier and Type | Class and Description |
---|---|
static class |
ScheduledDataLoaderRegistry.Builder |
dataLoaders
Modifier and Type | Method and Description |
---|---|
void |
close()
Once closed this registry will never again reschedule checks
|
ScheduledDataLoaderRegistry |
combine(DataLoaderRegistry registry)
This will combine all the current data loaders in this registry and all the data loaders from the specified registry
and return a new combined registry
|
void |
dispatchAll()
This will be called
DataLoader.dispatch() on each of the registered
DataLoader s |
void |
dispatchAllImmediately()
This will immediately dispatch the
DataLoader s in the registry
without testing the predicates |
int |
dispatchAllWithCount()
Similar to
DataLoaderRegistry.dispatchAll() , this calls DataLoader.dispatch() on
each of the registered DataLoader s, but returns the number of dispatches. |
int |
dispatchAllWithCountImmediately()
This will immediately dispatch the
DataLoader s in the registry
without testing the predicates |
java.util.Map<DataLoader<?,?>,DispatchPredicate> |
getDataLoaderPredicates() |
DispatchPredicate |
getDispatchPredicate()
There is a default predicate that applies to the whole
ScheduledDataLoaderRegistry |
java.util.concurrent.ScheduledExecutorService |
getScheduledExecutorService() |
java.time.Duration |
getScheduleDuration() |
boolean |
isTickerMode() |
static ScheduledDataLoaderRegistry.Builder |
newScheduledRegistry()
By default, this will create use a
Executors.newSingleThreadScheduledExecutor()
and a schedule duration of 10 milliseconds. |
ScheduledDataLoaderRegistry |
register(java.lang.String key,
DataLoader<?,?> dataLoader,
DispatchPredicate dispatchPredicate)
This will register a new dataloader and dispatch predicate associated with that data loader
|
void |
rescheduleNow()
This will schedule a task to check the predicate and dispatch if true right now.
|
ScheduledDataLoaderRegistry |
unregister(java.lang.String key)
This will unregister a new dataloader
|
computeIfAbsent, dispatchDepth, getDataLoader, getDataLoaders, getDataLoadersMap, getKeys, getStatistics, newRegistry, register
public void close()
close
in interface java.lang.AutoCloseable
public java.util.concurrent.ScheduledExecutorService getScheduledExecutorService()
public java.time.Duration getScheduleDuration()
ScheduledExecutorService
task will wait before checking the predicate againpublic boolean isTickerMode()
public ScheduledDataLoaderRegistry combine(DataLoaderRegistry registry)
combine
in class DataLoaderRegistry
registry
- the registry to combine into this registrypublic ScheduledDataLoaderRegistry unregister(java.lang.String key)
unregister
in class DataLoaderRegistry
key
- the key of the data loader to unregisterpublic java.util.Map<DataLoader<?,?>,DispatchPredicate> getDataLoaderPredicates()
public DispatchPredicate getDispatchPredicate()
ScheduledDataLoaderRegistry
public ScheduledDataLoaderRegistry register(java.lang.String key, DataLoader<?,?> dataLoader, DispatchPredicate dispatchPredicate)
key
- the key to put the data loader underdataLoader
- the data loader to registerdispatchPredicate
- the dispatch predicate to associate with this data loaderpublic void dispatchAll()
DataLoaderRegistry
DataLoader.dispatch()
on each of the registered
DataLoader
sdispatchAll
in class DataLoaderRegistry
public int dispatchAllWithCount()
DataLoaderRegistry
DataLoaderRegistry.dispatchAll()
, this calls DataLoader.dispatch()
on
each of the registered DataLoader
s, but returns the number of dispatches.dispatchAllWithCount
in class DataLoaderRegistry
DataLoader
s.public void dispatchAllImmediately()
DataLoader
s in the registry
without testing the predicatespublic int dispatchAllWithCountImmediately()
DataLoader
s in the registry
without testing the predicatesDataLoader
s.public void rescheduleNow()
dispatchAll()
wouldpublic static ScheduledDataLoaderRegistry.Builder newScheduledRegistry()
Executors.newSingleThreadScheduledExecutor()
and a schedule duration of 10 milliseconds.ScheduledDataLoaderRegistry
s