diff --git a/src/main/java/com/guicedee/guicedinjection/GuiceContext.java b/src/main/java/com/guicedee/guicedinjection/GuiceContext.java index c7b95f9..02d1216 100644 --- a/src/main/java/com/guicedee/guicedinjection/GuiceContext.java +++ b/src/main/java/com/guicedee/guicedinjection/GuiceContext.java @@ -26,15 +26,16 @@ import io.github.classgraph.ScanResult; import lombok.extern.java.Log; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; import java.util.*; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.regex.Pattern; import static com.guicedee.guicedinjection.properties.GlobalProperties.getSystemPropertyOrEnvironment; -import static java.util.stream.Collectors.groupingBy; -import static java.util.stream.Collectors.toSet; /** * Provides an interface for reflection and injection in one. @@ -53,11 +54,6 @@ public class GuiceContext> implements IGuiceContext * This particular instance of the class */ private static final GuiceContext instance = new GuiceContext<>(); - - /** - * A list of all the loaded singleton sets - */ - //private static final Map allLoadedServices = new LinkedHashMap<>(); /** * The building injector */ @@ -96,13 +92,14 @@ private GuiceContext() } + /** * Reference the Injector Directly * * @return The global Guice Injector Object, Never Null, Instantiates the Injector if not configured */ - - public synchronized Injector inject() + + public Injector inject() { if (GuiceContext.buildingInjector) { @@ -114,6 +111,7 @@ public synchronized Injector inject() try { GuiceContext.buildingInjector = true; + LocalDateTime start = LocalDateTime.now(); GuiceContext.log.info("Starting up Guice Context"); GuiceContext .instance() @@ -159,8 +157,8 @@ public void run() .destroy(); } }); - - GuiceContext.log.config("Injection System Ready"); + LocalDateTime end = LocalDateTime.now(); + log.info("System started in " + ChronoUnit.MILLIS.between(start, end) + "ms"); } catch (Throwable e) { @@ -245,7 +243,7 @@ private static int getJavaVersion() * * @return The physical Scan Result from the complete class scanner */ - + public ScanResult getScanResult() { if (scanResult == null) @@ -352,7 +350,7 @@ private String[] getJarsInclusionList() /** * Starts up Guice and the scanner */ - private synchronized void loadScanner() + private void loadScanner() { if (scanner == null) { @@ -727,7 +725,7 @@ private Map quickScanFilesPattern() * @return A set of them */ @SuppressWarnings("unchecked") - + public Set getLoader(Class loaderType, @SuppressWarnings("unused") boolean dontInject, ServiceLoader serviceLoader) { if (!IGuiceContext @@ -778,10 +776,7 @@ public static void setScanner(ClassGraph scanner) private void loadPostStartups() { Set startupSet = loadPostStartupServices(); - Map> groupedPostStartup = startupSet.stream() - .collect(groupingBy(IGuicePostStartup::sortOrder, toSet())); - /* Map>> postStartupGroups = new TreeMap<>(); - + Map>> postStartupGroups = new TreeMap<>(); for (IGuicePostStartup postStartup : startupSet) { Integer sortOrder = postStartup.sortOrder(); @@ -789,47 +784,37 @@ private void loadPostStartups() .computeIfAbsent(sortOrder, k -> new TreeSet<>()) .add(postStartup); } -*/ - for (Map.Entry> entry : groupedPostStartup.entrySet()) + for (Map.Entry>> entry : postStartupGroups.entrySet()) { Integer key = entry.getKey(); - Set value = entry.getValue(); - if (value.size() == 1) + Set> value = entry.getValue(); + List> futures = new ArrayList<>(); + // log.info("Starting Post Startup Group [" + key + "]"); + ExecutorService ex = null; + for (IGuicePostStartup iGuicePostStartup : value) + { + log.info("Starting Post Load [" + iGuicePostStartup.getClass() + .getSimpleName() + "] - Start Order [" + key + "]"); + ex= iGuicePostStartup.getExecutorService(); + futures.addAll(iGuicePostStartup.postLoad()); + } + try { - //run in order - for (IGuicePostStartup iGuicePostStartup : value) + if(!futures.isEmpty()) { - try - { - iGuicePostStartup.postLoad(); - } - catch (Throwable T) + CompletableFuture.allOf(futures.toArray(new CompletableFuture[]{})).join(); + if (ex != null) { - log.log(Level.SEVERE, - "Cannot execute post startup - " + iGuicePostStartup - .getClass() - .getCanonicalName(), - T); + ex.shutdown(); + ex.awaitTermination(30, TimeUnit.SECONDS); } } } - else + catch (Exception e) { - log.info("Starting Post Startup Group [" + key + "] in Parallel"); - List> futures = new ArrayList<>(); - for (IGuicePostStartup iGuicePostStartup : value) - { - futures.add(CompletableFuture.runAsync(iGuicePostStartup::postLoad)); - } - try - { - CompletableFuture.allOf(futures.toArray(new CompletableFuture[]{})).join(); - } - catch (Exception e) - { - log.log(Level.SEVERE, "Exception in completing post startups", e); - } + log.log(Level.SEVERE, "Exception in completing post startups", e); } + GuiceContext.log.fine("Completed with Post Startups Key [" + key + "]"); } } @@ -849,7 +834,7 @@ public GuiceConfig getConfig() * * @return The list of guice post startups */ - public Set loadPostStartupServices() + public Set loadPostStartupServices() { return new TreeSet<>(getLoader(IGuicePostStartup.class, ServiceLoader.load(IGuicePostStartup.class))); } @@ -859,7 +844,7 @@ public Set loadPostStartupServices() * * @return The list of guice post startups */ - public Set loadPathRejectScanners() + public Set loadPathRejectScanners() { return getLoader(IPathContentsRejectListScanner.class, true, ServiceLoader.load(IPathContentsRejectListScanner.class)); } @@ -870,7 +855,7 @@ public Set loadPathRejectScanners() * * @return The list of guice post startups */ - public Set loadJarRejectScanners() + public Set loadJarRejectScanners() { return getLoader(IGuiceScanJarExclusions.class, true, ServiceLoader.load(IGuiceScanJarExclusions.class)); } @@ -881,7 +866,7 @@ public Set loadJarRejectScanners() * * @return The list of guice post startups */ - public Set loadJarInclusionScanners() + public Set loadJarInclusionScanners() { return getLoader(IGuiceScanJarInclusions.class, true, ServiceLoader.load(IGuiceScanJarInclusions.class)); } @@ -892,7 +877,7 @@ public Set loadJarInclusionScanners() * * @return The list of guice post startups */ - public Set loadPreStartupServices() + public Set loadPreStartupServices() { return new TreeSet<>(getLoader(IGuicePreStartup.class, true, ServiceLoader.load(IGuicePreStartup.class))); } @@ -902,7 +887,7 @@ public Set loadPreStartupServices() * * @return The list of guice post startups */ - public Set loadIGuiceModules() + public Set loadIGuiceModules() { return new TreeSet<>(getLoader(IGuiceModule.class, true, ServiceLoader.load(IGuiceModule.class))); } @@ -912,7 +897,7 @@ public Set loadIGuiceModules() * * @return The list of guice configs */ - public Set loadIGuiceConfigs() + public Set loadIGuiceConfigs() { return getLoader(IGuiceConfigurator.class, true, ServiceLoader.load(IGuiceConfigurator.class)); } @@ -941,7 +926,7 @@ private void loadPreStartups() * @return A set of them */ @SuppressWarnings("unchecked") - + public > Set getLoader(Class loaderType, ServiceLoader serviceLoader) { if (!IGuiceContext diff --git a/src/test/java/com/guicedee/tests/ParallelPostStartupTest1.java b/src/test/java/com/guicedee/tests/ParallelPostStartupTest1.java index 1db134c..212f6a9 100644 --- a/src/test/java/com/guicedee/tests/ParallelPostStartupTest1.java +++ b/src/test/java/com/guicedee/tests/ParallelPostStartupTest1.java @@ -2,12 +2,18 @@ import com.guicedee.guicedinjection.interfaces.IGuicePostStartup; +import java.util.List; +import java.util.concurrent.CompletableFuture; + public class ParallelPostStartupTest1 implements IGuicePostStartup { @Override - public void postLoad() + public List> postLoad() { - System.out.println("Starting 1"); + return List.of(CompletableFuture.supplyAsync(() -> { + System.out.println("Starting 1"); + return true; + })); } @Override diff --git a/src/test/java/com/guicedee/tests/ParallelPostStartupTest2.java b/src/test/java/com/guicedee/tests/ParallelPostStartupTest2.java index 028767c..08568f0 100644 --- a/src/test/java/com/guicedee/tests/ParallelPostStartupTest2.java +++ b/src/test/java/com/guicedee/tests/ParallelPostStartupTest2.java @@ -2,12 +2,18 @@ import com.guicedee.guicedinjection.interfaces.IGuicePostStartup; +import java.util.List; +import java.util.concurrent.CompletableFuture; + public class ParallelPostStartupTest2 implements IGuicePostStartup { @Override - public void postLoad() + public List> postLoad() { - System.out.println("Starting 2"); + return List.of(CompletableFuture.supplyAsync(() -> { + System.out.println("Starting 2"); + return true; + })); } @Override