|
6 | 6 | import org.dataloader.DataLoader; |
7 | 7 | import org.dataloader.DataLoaderFactory; |
8 | 8 | import org.dataloader.DataLoaderOptions; |
| 9 | +import org.dataloader.DataLoaderRegistry; |
| 10 | +import org.dataloader.DispatchResult; |
9 | 11 | import org.dataloader.MappedBatchLoaderWithContext; |
10 | 12 | import org.dataloader.MappedBatchPublisher; |
11 | 13 | import org.dataloader.Try; |
12 | 14 | import org.dataloader.fixtures.SecurityCtx; |
13 | 15 | import org.dataloader.fixtures.User; |
14 | 16 | import org.dataloader.fixtures.UserManager; |
| 17 | +import org.dataloader.instrumentation.DataLoaderInstrumentation; |
| 18 | +import org.dataloader.instrumentation.DataLoaderInstrumentationContext; |
| 19 | +import org.dataloader.instrumentation.DataLoaderInstrumentationHelper; |
15 | 20 | import org.dataloader.registries.DispatchPredicate; |
16 | 21 | import org.dataloader.registries.ScheduledDataLoaderRegistry; |
17 | 22 | import org.dataloader.scheduler.BatchLoaderScheduler; |
@@ -228,6 +233,7 @@ private void clearCacheOnError() { |
228 | 233 | } |
229 | 234 |
|
230 | 235 | BatchLoader<String, User> userBatchLoader; |
| 236 | + BatchLoader<String, User> teamsBatchLoader; |
231 | 237 |
|
232 | 238 | private void disableCache() { |
233 | 239 | DataLoaderFactory.newDataLoader(userBatchLoader, DataLoaderOptions.newOptions().setCachingEnabled(false)); |
@@ -380,4 +386,45 @@ private void ScheduledDispatcherChained() { |
380 | 386 | .build(); |
381 | 387 |
|
382 | 388 | } |
| 389 | + |
| 390 | + private DataLoaderInstrumentation timingInstrumentation = DataLoaderInstrumentationHelper.NOOP_INSTRUMENTATION; |
| 391 | + |
| 392 | + private void instrumentationExample() { |
| 393 | + |
| 394 | + DataLoaderInstrumentation timingInstrumentation = new DataLoaderInstrumentation() { |
| 395 | + @Override |
| 396 | + public DataLoaderInstrumentationContext<DispatchResult<?>> beginDispatch(DataLoader<?, ?> dataLoader) { |
| 397 | + long then = System.currentTimeMillis(); |
| 398 | + return DataLoaderInstrumentationHelper.whenCompleted((result, err) -> { |
| 399 | + long ms = System.currentTimeMillis() - then; |
| 400 | + System.out.println(format("dispatch time: %d ms", ms)); |
| 401 | + }); |
| 402 | + } |
| 403 | + |
| 404 | + @Override |
| 405 | + public DataLoaderInstrumentationContext<List<?>> beginBatchLoader(DataLoader<?, ?> dataLoader, List<?> keys, BatchLoaderEnvironment environment) { |
| 406 | + long then = System.currentTimeMillis(); |
| 407 | + return DataLoaderInstrumentationHelper.whenCompleted((result, err) -> { |
| 408 | + long ms = System.currentTimeMillis() - then; |
| 409 | + System.out.println(format("batch loader time: %d ms", ms)); |
| 410 | + }); |
| 411 | + } |
| 412 | + }; |
| 413 | + DataLoaderOptions options = DataLoaderOptions.newOptions().setInstrumentation(timingInstrumentation); |
| 414 | + DataLoader<String, User> userDataLoader = DataLoaderFactory.newDataLoader(userBatchLoader, options); |
| 415 | + } |
| 416 | + |
| 417 | + private void registryExample() { |
| 418 | + DataLoader<String, User> userDataLoader = DataLoaderFactory.newDataLoader(userBatchLoader); |
| 419 | + DataLoader<String, User> teamsDataLoader = DataLoaderFactory.newDataLoader(teamsBatchLoader); |
| 420 | + |
| 421 | + DataLoaderRegistry registry = DataLoaderRegistry.newRegistry() |
| 422 | + .instrumentation(timingInstrumentation) |
| 423 | + .register("users", userDataLoader) |
| 424 | + .register("teams", teamsDataLoader) |
| 425 | + .build(); |
| 426 | + |
| 427 | + DataLoader<String, User> changedUsersDataLoader = registry.getDataLoader("users"); |
| 428 | + |
| 429 | + } |
383 | 430 | } |
0 commit comments