|
| 1 | +package eu.happycoders.structuredconcurrency.demo3_suppliers; |
| 2 | + |
| 3 | +import static eu.happycoders.structuredconcurrency.util.SimpleLogger.log; |
| 4 | + |
| 5 | +import eu.happycoders.structuredconcurrency.demo3_suppliers.model.SupplierDeliveryTime; |
| 6 | +import eu.happycoders.structuredconcurrency.demo3_suppliers.service.SupplierDeliveryTimeServiceUsingScopedValue; |
| 7 | +import java.util.List; |
| 8 | +import java.util.concurrent.StructuredTaskScope; |
| 9 | +import java.util.concurrent.StructuredTaskScope.Subtask; |
| 10 | +import java.util.concurrent.StructuredTaskScope.Subtask.State; |
| 11 | + |
| 12 | +public class SupplierDeliveryTimeCheck4_NestedStructuredTaskScopeUsingScopedValue { |
| 13 | + |
| 14 | + private static final boolean FAIL_ALL = false; |
| 15 | + |
| 16 | + public static void main(String[] args) throws Exception { |
| 17 | + SupplierDeliveryTimeCheck4_NestedStructuredTaskScopeUsingScopedValue supplierDeliveryTimeCheck = |
| 18 | + new SupplierDeliveryTimeCheck4_NestedStructuredTaskScopeUsingScopedValue( |
| 19 | + new SupplierDeliveryTimeServiceUsingScopedValue(FAIL_ALL)); |
| 20 | + List<SupplierDeliveryTime> responses = |
| 21 | + supplierDeliveryTimeCheck.getSupplierDeliveryTimes( |
| 22 | + List.of("B004V9OA84", "0201310090", "0134685997"), |
| 23 | + List.of("A", "B", "C", "D", "E"), |
| 24 | + "t0p-s3cr3t"); |
| 25 | + log("Responses: " + responses); |
| 26 | + } |
| 27 | + |
| 28 | + private final SupplierDeliveryTimeServiceUsingScopedValue service; |
| 29 | + |
| 30 | + public SupplierDeliveryTimeCheck4_NestedStructuredTaskScopeUsingScopedValue( |
| 31 | + SupplierDeliveryTimeServiceUsingScopedValue service) { |
| 32 | + this.service = service; |
| 33 | + } |
| 34 | + |
| 35 | + public static final ScopedValue<String> API_KEY = ScopedValue.newInstance(); |
| 36 | + |
| 37 | + List<SupplierDeliveryTime> getSupplierDeliveryTimes( |
| 38 | + List<String> productIds, List<String> supplierIds, String apiKey) throws Exception { |
| 39 | + return ScopedValue.where(API_KEY, apiKey) |
| 40 | + .call(() -> getSupplierDeliveryTimes(productIds, supplierIds)); |
| 41 | + } |
| 42 | + |
| 43 | + List<SupplierDeliveryTime> getSupplierDeliveryTimes( |
| 44 | + List<String> productIds, List<String> supplierIds) throws InterruptedException { |
| 45 | + try (StructuredTaskScope<SupplierDeliveryTime> scope = new StructuredTaskScope<>()) { |
| 46 | + List<Subtask<SupplierDeliveryTime>> subtasks = |
| 47 | + productIds.stream() |
| 48 | + .map(productId -> scope.fork(() -> getSupplierDeliveryTime(productId, supplierIds))) |
| 49 | + .toList(); |
| 50 | + |
| 51 | + scope.join(); |
| 52 | + |
| 53 | + return subtasks.stream() |
| 54 | + .filter(subtask -> subtask.state() == State.SUCCESS) |
| 55 | + .map(Subtask::get) |
| 56 | + .toList(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + SupplierDeliveryTime getSupplierDeliveryTime(String productId, List<String> supplierIds) |
| 61 | + throws SupplierDeliveryTimeCheckException, InterruptedException { |
| 62 | + try (GetFastestDeliveryTimeScope scope = new GetFastestDeliveryTimeScope()) { |
| 63 | + for (String supplierId : supplierIds) { |
| 64 | + scope.fork(() -> service.getDeliveryTime(productId, supplierId)); |
| 65 | + } |
| 66 | + |
| 67 | + scope.join(); |
| 68 | + return scope.result(); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments