Class ScheduledDataLoaderRegistry
- java.lang.Object
-
- org.dataloader.DataLoaderRegistry
-
- org.dataloader.registries.ScheduledDataLoaderRegistry
-
- All Implemented Interfaces:
java.lang.AutoCloseable
@ExperimentalApi @NullMarked public class ScheduledDataLoaderRegistry extends DataLoaderRegistry implements java.lang.AutoCloseable
ThisDataLoaderRegistry
will useDispatchPredicate
s whendispatchAll()
is called to test (for eachDataLoader
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 theScheduledExecutorService
.It;s possible to have a
DispatchPredicate
per dataloader as well as a defaultDispatchPredicate
for the wholeScheduledDataLoaderRegistry
.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 todispatchAll()
.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 theScheduledExecutorService
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 aScheduledDataLoaderRegistry
per request you will want to look at sharing thisScheduledExecutorService
to avoid creating a new thread per registry created.This code is currently marked as
ExperimentalApi
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ScheduledDataLoaderRegistry.Builder
-
Field Summary
-
Fields inherited from class org.dataloader.DataLoaderRegistry
dataLoaders, instrumentation
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Once closed this registry will never again reschedule checksScheduledDataLoaderRegistry
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 registryvoid
dispatchAll()
This will be calledDataLoader.dispatch()
on each of the registeredDataLoader
svoid
dispatchAllImmediately()
This will immediately dispatch theDataLoader
s in the registry without testing the predicatesint
dispatchAllWithCount()
Similar toDataLoaderRegistry.dispatchAll()
, this callsDataLoader.dispatch()
on each of the registeredDataLoader
s, but returns the number of dispatches.int
dispatchAllWithCountImmediately()
This will immediately dispatch theDataLoader
s in the registry without testing the predicatesjava.util.Map<DataLoader<?,?>,DispatchPredicate>
getDataLoaderPredicates()
DispatchPredicate
getDispatchPredicate()
There is a default predicate that applies to the wholeScheduledDataLoaderRegistry
java.util.concurrent.ScheduledExecutorService
getScheduledExecutorService()
java.time.Duration
getScheduleDuration()
boolean
isTickerMode()
static ScheduledDataLoaderRegistry.Builder
newScheduledRegistry()
By default, this will create use aExecutors.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 loadervoid
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-
Methods inherited from class org.dataloader.DataLoaderRegistry
computeIfAbsent, dispatchDepth, getDataLoader, getDataLoaders, getDataLoadersMap, getInstrumentation, getKeys, getStatistics, newRegistry, register, register, registerAndGet
-
-
-
-
Method Detail
-
close
public void close()
Once closed this registry will never again reschedule checks- Specified by:
close
in interfacejava.lang.AutoCloseable
-
getScheduledExecutorService
public java.util.concurrent.ScheduledExecutorService getScheduledExecutorService()
- Returns:
- executor being used by this registry
-
getScheduleDuration
public java.time.Duration getScheduleDuration()
- Returns:
- how long the
ScheduledExecutorService
task will wait before checking the predicate again
-
isTickerMode
public boolean isTickerMode()
- Returns:
- true of the registry is in ticker mode or false otherwise
-
combine
public 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- Overrides:
combine
in classDataLoaderRegistry
- Parameters:
registry
- the registry to combine into this registry- Returns:
- a new combined registry
-
unregister
public ScheduledDataLoaderRegistry unregister(java.lang.String key)
This will unregister a new dataloader- Overrides:
unregister
in classDataLoaderRegistry
- Parameters:
key
- the key of the data loader to unregister- Returns:
- this registry
-
getDataLoaderPredicates
public java.util.Map<DataLoader<?,?>,DispatchPredicate> getDataLoaderPredicates()
- Returns:
- a map of data loaders to specific dispatch predicates
-
getDispatchPredicate
public DispatchPredicate getDispatchPredicate()
There is a default predicate that applies to the wholeScheduledDataLoaderRegistry
- Returns:
- the default dispatch predicate
-
register
public ScheduledDataLoaderRegistry register(java.lang.String key, DataLoader<?,?> dataLoader, DispatchPredicate dispatchPredicate)
This will register a new dataloader and dispatch predicate associated with that data loader- Parameters:
key
- the key to put the data loader underdataLoader
- the data loader to registerdispatchPredicate
- the dispatch predicate to associate with this data loader- Returns:
- this registry
-
dispatchAll
public void dispatchAll()
Description copied from class:DataLoaderRegistry
This will be calledDataLoader.dispatch()
on each of the registeredDataLoader
s- Overrides:
dispatchAll
in classDataLoaderRegistry
-
dispatchAllWithCount
public int dispatchAllWithCount()
Description copied from class:DataLoaderRegistry
Similar toDataLoaderRegistry.dispatchAll()
, this callsDataLoader.dispatch()
on each of the registeredDataLoader
s, but returns the number of dispatches.- Overrides:
dispatchAllWithCount
in classDataLoaderRegistry
- Returns:
- total number of entries that were dispatched from registered
DataLoader
s.
-
dispatchAllImmediately
public void dispatchAllImmediately()
This will immediately dispatch theDataLoader
s in the registry without testing the predicates
-
dispatchAllWithCountImmediately
public int dispatchAllWithCountImmediately()
This will immediately dispatch theDataLoader
s in the registry without testing the predicates- Returns:
- total number of entries that were dispatched from registered
DataLoader
s.
-
rescheduleNow
public void rescheduleNow()
This will schedule a task to check the predicate and dispatch if true right now. It will not do a pre-check of the predicate likedispatchAll()
would
-
newScheduledRegistry
public static ScheduledDataLoaderRegistry.Builder newScheduledRegistry()
By default, this will create use aExecutors.newSingleThreadScheduledExecutor()
and a schedule duration of 10 milliseconds.- Returns:
- A builder of
ScheduledDataLoaderRegistry
s
-
-