|  | 
|  | 1 | +package org.dataloader.registries; | 
|  | 2 | + | 
|  | 3 | +import org.dataloader.DataLoader; | 
|  | 4 | +import org.dataloader.DataLoaderRegistry; | 
|  | 5 | +import org.dataloader.annotations.ExperimentalApi; | 
|  | 6 | + | 
|  | 7 | +import java.time.Duration; | 
|  | 8 | +import java.util.HashMap; | 
|  | 9 | +import java.util.Map; | 
|  | 10 | +import java.util.concurrent.Executors; | 
|  | 11 | +import java.util.concurrent.ScheduledExecutorService; | 
|  | 12 | +import java.util.concurrent.TimeUnit; | 
|  | 13 | + | 
|  | 14 | +import static org.dataloader.impl.Assertions.nonNull; | 
|  | 15 | + | 
|  | 16 | +/** | 
|  | 17 | + * This {@link DataLoaderRegistry} will use a {@link DispatchPredicate} when {@link #dispatchAll()} is called | 
|  | 18 | + * to test (for each {@link DataLoader} in the registry) if a dispatch should proceed. If the predicate returns false, then a task is scheduled | 
|  | 19 | + * to perform that predicate dispatch again via the {@link ScheduledExecutorService}. | 
|  | 20 | + * <p> | 
|  | 21 | + * This will continue to loop (test false and reschedule) until such time as the predicate returns true, in which case | 
|  | 22 | + * no rescheduling will occur and you will need to call dispatch again to restart the process. | 
|  | 23 | + * <p> | 
|  | 24 | + * If you wanted to create a ScheduledDataLoaderRegistry that started a rescheduling immediately, just create one and | 
|  | 25 | + * call {@link #rescheduleNow()}. | 
|  | 26 | + * <p> | 
|  | 27 | + * This code is currently marked as {@link ExperimentalApi} | 
|  | 28 | + */ | 
|  | 29 | +@ExperimentalApi | 
|  | 30 | +public class ScheduledDataLoaderRegistry extends DataLoaderRegistry implements AutoCloseable { | 
|  | 31 | + | 
|  | 32 | + private final ScheduledExecutorService scheduledExecutorService; | 
|  | 33 | + private final DispatchPredicate dispatchPredicate; | 
|  | 34 | + private final Duration schedule; | 
|  | 35 | + private volatile boolean closed; | 
|  | 36 | + | 
|  | 37 | + private ScheduledDataLoaderRegistry(Builder builder) { | 
|  | 38 | + this.dataLoaders.putAll(builder.dataLoaders); | 
|  | 39 | + this.scheduledExecutorService = builder.scheduledExecutorService; | 
|  | 40 | + this.dispatchPredicate = builder.dispatchPredicate; | 
|  | 41 | + this.schedule = builder.schedule; | 
|  | 42 | + this.closed = false; | 
|  | 43 | + } | 
|  | 44 | + | 
|  | 45 | + /** | 
|  | 46 | + * Once closed this registry will never again reschedule checks | 
|  | 47 | + */ | 
|  | 48 | + @Override | 
|  | 49 | + public void close() { | 
|  | 50 | + closed = true; | 
|  | 51 | + } | 
|  | 52 | + | 
|  | 53 | + /** | 
|  | 54 | + * @return how long the {@link ScheduledExecutorService} task will wait before checking the predicate again | 
|  | 55 | + */ | 
|  | 56 | + public Duration getScheduleDuration() { | 
|  | 57 | + return schedule; | 
|  | 58 | + } | 
|  | 59 | + | 
|  | 60 | + @Override | 
|  | 61 | + public void dispatchAll() { | 
|  | 62 | + dispatchAllWithCount(); | 
|  | 63 | + } | 
|  | 64 | + | 
|  | 65 | + @Override | 
|  | 66 | + public int dispatchAllWithCount() { | 
|  | 67 | + int sum = 0; | 
|  | 68 | + for (Map.Entry<String, DataLoader<?, ?>> entry : dataLoaders.entrySet()) { | 
|  | 69 | + DataLoader<?, ?> dataLoader = entry.getValue(); | 
|  | 70 | + String key = entry.getKey(); | 
|  | 71 | + if (dispatchPredicate.test(key, dataLoader)) { | 
|  | 72 | + sum += dataLoader.dispatchWithCounts().getKeysCount(); | 
|  | 73 | + } else { | 
|  | 74 | + reschedule(key, dataLoader); | 
|  | 75 | + } | 
|  | 76 | + } | 
|  | 77 | + return sum; | 
|  | 78 | + } | 
|  | 79 | + | 
|  | 80 | + /** | 
|  | 81 | + * This will immediately dispatch the {@link DataLoader}s in the registry | 
|  | 82 | + * without testing the predicate | 
|  | 83 | + */ | 
|  | 84 | + public void dispatchAllImmediately() { | 
|  | 85 | + super.dispatchAll(); | 
|  | 86 | + } | 
|  | 87 | + | 
|  | 88 | + /** | 
|  | 89 | + * This will immediately dispatch the {@link DataLoader}s in the registry | 
|  | 90 | + * without testing the predicate | 
|  | 91 | + * | 
|  | 92 | + * @return total number of entries that were dispatched from registered {@link org.dataloader.DataLoader}s. | 
|  | 93 | + */ | 
|  | 94 | + public int dispatchAllWithCountImmediately() { | 
|  | 95 | + return super.dispatchAllWithCount(); | 
|  | 96 | + } | 
|  | 97 | + | 
|  | 98 | + /** | 
|  | 99 | + * This will schedule a task to check the predicate and dispatch if true right now. It will not do | 
|  | 100 | + * a pre check of the preodicate like {@link #dispatchAll()} would | 
|  | 101 | + */ | 
|  | 102 | + public void rescheduleNow() { | 
|  | 103 | + dataLoaders.forEach(this::reschedule); | 
|  | 104 | + } | 
|  | 105 | + | 
|  | 106 | + private void reschedule(String key, DataLoader<?, ?> dataLoader) { | 
|  | 107 | + if (!closed) { | 
|  | 108 | + Runnable runThis = () -> dispatchOrReschedule(key, dataLoader); | 
|  | 109 | + scheduledExecutorService.schedule(runThis, schedule.toMillis(), TimeUnit.MILLISECONDS); | 
|  | 110 | + } | 
|  | 111 | + } | 
|  | 112 | + | 
|  | 113 | + private void dispatchOrReschedule(String key, DataLoader<?, ?> dataLoader) { | 
|  | 114 | + if (dispatchPredicate.test(key, dataLoader)) { | 
|  | 115 | + dataLoader.dispatch(); | 
|  | 116 | + } else { | 
|  | 117 | + reschedule(key, dataLoader); | 
|  | 118 | + } | 
|  | 119 | + } | 
|  | 120 | + | 
|  | 121 | + /** | 
|  | 122 | + * By default this will create use a {@link Executors#newSingleThreadScheduledExecutor()} | 
|  | 123 | + * and a schedule duration of 10 milli seconds. | 
|  | 124 | + * | 
|  | 125 | + * @return A builder of {@link ScheduledDataLoaderRegistry}s | 
|  | 126 | + */ | 
|  | 127 | + public static Builder newScheduledRegistry() { | 
|  | 128 | + return new Builder(); | 
|  | 129 | + } | 
|  | 130 | + | 
|  | 131 | + public static class Builder { | 
|  | 132 | + | 
|  | 133 | + private ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); | 
|  | 134 | + private DispatchPredicate dispatchPredicate = (key, dl) -> true; | 
|  | 135 | + private Duration schedule = Duration.ofMillis(10); | 
|  | 136 | + private final Map<String, DataLoader<?, ?>> dataLoaders = new HashMap<>(); | 
|  | 137 | + | 
|  | 138 | + public Builder scheduledExecutorService(ScheduledExecutorService executorService) { | 
|  | 139 | + this.scheduledExecutorService = nonNull(executorService); | 
|  | 140 | + return this; | 
|  | 141 | + } | 
|  | 142 | + | 
|  | 143 | + public Builder schedule(Duration schedule) { | 
|  | 144 | + this.schedule = schedule; | 
|  | 145 | + return this; | 
|  | 146 | + } | 
|  | 147 | + | 
|  | 148 | + public Builder dispatchPredicate(DispatchPredicate dispatchPredicate) { | 
|  | 149 | + this.dispatchPredicate = nonNull(dispatchPredicate); | 
|  | 150 | + return this; | 
|  | 151 | + } | 
|  | 152 | + | 
|  | 153 | + /** | 
|  | 154 | + * This will register a new dataloader | 
|  | 155 | + * | 
|  | 156 | + * @param key the key to put the data loader under | 
|  | 157 | + * @param dataLoader the data loader to register | 
|  | 158 | + * | 
|  | 159 | + * @return this builder for a fluent pattern | 
|  | 160 | + */ | 
|  | 161 | + public Builder register(String key, DataLoader<?, ?> dataLoader) { | 
|  | 162 | + dataLoaders.put(key, dataLoader); | 
|  | 163 | + return this; | 
|  | 164 | + } | 
|  | 165 | + | 
|  | 166 | + /** | 
|  | 167 | + * This will combine together the data loaders in this builder with the ones | 
|  | 168 | + * from a previous {@link DataLoaderRegistry} | 
|  | 169 | + * | 
|  | 170 | + * @param otherRegistry the previous {@link DataLoaderRegistry} | 
|  | 171 | + * | 
|  | 172 | + * @return this builder for a fluent pattern | 
|  | 173 | + */ | 
|  | 174 | + public Builder registerAll(DataLoaderRegistry otherRegistry) { | 
|  | 175 | + dataLoaders.putAll(otherRegistry.getDataLoadersMap()); | 
|  | 176 | + return this; | 
|  | 177 | + } | 
|  | 178 | + | 
|  | 179 | + /** | 
|  | 180 | + * @return the newly built {@link ScheduledDataLoaderRegistry} | 
|  | 181 | + */ | 
|  | 182 | + public ScheduledDataLoaderRegistry build() { | 
|  | 183 | + return new ScheduledDataLoaderRegistry(this); | 
|  | 184 | + } | 
|  | 185 | + } | 
|  | 186 | +} | 
0 commit comments