Skip to content

Commit

Permalink
Guiced Injection Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
GedMarc committed Dec 28, 2023
1 parent 182f5f5 commit e12b1a9
Show file tree
Hide file tree
Showing 54 changed files with 169 additions and 2,826 deletions.
27 changes: 5 additions & 22 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
<properties>
<ignore.moditect>true</ignore.moditect>
<flatten.pom>false</flatten.pom>
<project.scm.nameUrl>/GuicedEE/GuicedInjection</project.scm.nameUrl>
</properties>


<dependencyManagement>
<dependencies>
<dependency>
Expand Down Expand Up @@ -108,10 +108,6 @@
<artifactId>commons-lang3</artifactId>
</dependency>

<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand All @@ -133,18 +129,6 @@
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

<dependency>
<groupId>com.guicedee.services</groupId>
<artifactId>json</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.guicedee.services</groupId>
<artifactId>apache-poi-ooxml</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
Expand Down Expand Up @@ -175,15 +159,14 @@

<build>
<plugins>

<plugin>
<groupId>org.moditect</groupId>
<artifactId>moditect-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
</plugin>

<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.guicedee.guicedinjection;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

public class ExecutorServiceSupplier implements Supplier<ExecutorService>
{
@Override
public ExecutorService get()
{
return Executors.newCachedThreadPool();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.guicedee.guicedinjection;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

public class ExecutorServiceSupplier implements Supplier<ExecutorService>
{
@Override
public ExecutorService get()
{
return Executors.newVirtualThreadPerTaskExecutor();
}
}
88 changes: 68 additions & 20 deletions src/main/java/com/guicedee/guicedinjection/GuiceContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import jakarta.validation.constraints.*;
import lombok.extern.java.Log;

import java.lang.Module;
import java.lang.annotation.*;
import java.util.*;
import java.util.concurrent.*;
Expand All @@ -50,10 +51,11 @@ public class GuiceContext<J extends GuiceContext<J>>
* 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<Class, Set> allLoadedServices = Collections.synchronizedMap(new LinkedHashMap<>());
private static final Map<Class, Set> allLoadedServices = new LinkedHashMap<>();
/**
* The building injector
*/
Expand All @@ -69,7 +71,7 @@ public class GuiceContext<J extends GuiceContext<J>>
/**
* The configuration object
*/
private static GuiceConfig<?> config;
private static final GuiceConfig<?> config = new GuiceConfig<>();
/**
* The physical injector for the JVM container
*/
Expand All @@ -86,6 +88,10 @@ public class GuiceContext<J extends GuiceContext<J>>
* If the scan should run async
*/
private boolean async;
/**
* If this context is configured
*/
private static boolean configured;

/**
* Creates a new Guice context. Not necessary
Expand All @@ -95,6 +101,8 @@ private GuiceContext()
//No config required
}

private Set<String> registerModules = new LinkedHashSet<>();

/**
* Reference the Injector Directly
*
Expand All @@ -110,6 +118,7 @@ public static synchronized Injector inject()
}
if (GuiceContext.instance().injector == null)
{
SysStreamsLogger.bindSystemStreams();
try
{
GuiceContext.buildingInjector = true;
Expand Down Expand Up @@ -299,11 +308,22 @@ public static <T> T get(@NotNull Key<T> type)
public static void destroy()
{
Set<IGuicePreDestroy> destroyers = GuiceContext.instance()
.getLoader(IGuicePreDestroy.class, true, ServiceLoader.load(IGuicePreDestroy.class));
for (IGuicePreDestroy destroyer : destroyers)
.getLoader(IGuicePreDestroy.class, false, ServiceLoader.load(IGuicePreDestroy.class));
try
{
for (IGuicePreDestroy destroyer : destroyers)
{
try
{
destroyer.onDestroy();
} catch (Throwable T)
{
log.log(Level.SEVERE, "Could not run destroyer [" + destroyer.getClass().getCanonicalName() + "]");
}
}
} catch (Throwable T)
{
IGuicePreDestroy instance = GuiceContext.get(destroyer.getClass());
instance.onDestroy();
log.log(Level.SEVERE, "Could not run destroyers", T);
}
if (GuiceContext.instance().scanResult != null)
{
Expand Down Expand Up @@ -378,33 +398,26 @@ public static GuiceContext<?> instance()
return GuiceContext.instance;
}

private static boolean configured;

/**
* Loads the IGuiceConfigurator
*/
private void loadConfiguration()
{
if (!configured)
{
if (GuiceContext.config == null)
{
GuiceContext.config = new GuiceConfig<>();
}
Set<IGuiceConfigurator> guiceConfigurators = loadIGuiceConfigs();
for (IGuiceConfigurator guiceConfigurator : guiceConfigurators)
{
GuiceContext.log.config("Loading IGuiceConfigurator - " +
guiceConfigurator.getClass()
.getCanonicalName());
GuiceContext.config = (GuiceConfig<?>) guiceConfigurator.configure(GuiceContext.config);
guiceConfigurator.configure(GuiceContext.config);
}
GuiceContext.log.config("IGuiceConfigurator : " + GuiceContext.config.toString());
configured = true;
}
}


/**
* Returns a complete list of generic exclusions
*
Expand Down Expand Up @@ -745,6 +758,18 @@ private String[] getModulesExclusionList()
return strings.toArray(new String[0]);
}

/**
* Registers a module for scanning when filtering is enabled
*
* @param javaModuleName The name in the module-info.java file
* @return This instance
*/
@SuppressWarnings("unchecked")
public J registerModule(String javaModuleName)
{
this.registerModules.add(javaModuleName);
return (J) this;
}

/**
* Returns a complete list of generic exclusions
Expand All @@ -755,6 +780,7 @@ private String[] getModulesExclusionList()
private String[] getModulesInclusionsList()
{
Set<String> strings = new TreeSet<>();
strings.addAll(registerModules);
Set<IGuiceScanModuleInclusions> exclusions = getLoader(IGuiceScanModuleInclusions.class, true, ServiceLoader.load(IGuiceScanModuleInclusions.class));
if (exclusions.iterator()
.hasNext())
Expand Down Expand Up @@ -882,14 +908,39 @@ private void loadPostStartups()
}
} else
{
log.info("Starting Post Startup Group [" + key + "] in Parallel");
ExecutorService postStartup = null;
for (IGuicePostStartup iGuicePostStartup : value)
{
postStartup = JobService.getInstance().addJob("PostStartup", () -> {
try
{
iGuicePostStartup.postLoad();
} catch (Throwable T)
{
log.log(Level.SEVERE, "Cannot execute post startup - ", T);
}
});
}
try
{
if (postStartup != null)
{
log.config("Waiting for post startup group to finish....");
JobService.getInstance().removeJob("PostStartup");
}
} catch (Throwable e)
{
log.log(Level.SEVERE, "Cannot execute post startup - ", e);
}
/*try
{
value.parallelStream()
.forEach(IGuicePostStartup::postLoad);
} catch (Throwable T)
{
log.log(Level.SEVERE, "Cannot execute post startup - ", T);
}
}*/
}
GuiceContext.log.fine("Completed with Post Startups Key [" + key + "]");
}
Expand All @@ -902,10 +953,6 @@ private void loadPostStartups()
*/
public GuiceConfig<?> getConfig()
{
if (GuiceContext.config == null)
{
GuiceContext.config = new GuiceConfig<>();
}
return GuiceContext.config;
}

Expand Down Expand Up @@ -1173,7 +1220,8 @@ public <T> Set<T> loaderToSetNoInjection(ServiceLoader<T> loader)
String type = loader.toString();
type = type.replace("java.util.ServiceLoader[", "");
type = type.substring(0, type.length() - 1);
if (config.isServiceLoadWithClassPath() && !buildingInjector)
if (config.isServiceLoadWithClassPath() && !buildingInjector && instance()
.getScanResult() != null)
{
for (ClassInfo classInfo : instance()
.getScanResult()
Expand Down
31 changes: 13 additions & 18 deletions src/main/java/com/guicedee/guicedinjection/JobService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,11 @@
import lombok.Setter;
import lombok.extern.java.Log;

import java.sql.Time;
import java.time.temporal.ChronoUnit;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.util.concurrent.Executors.newWorkStealingPool;

/**
* Manages All Concurrent Threaded Jobs that execute asynchronously outside of the EE Context
Expand All @@ -36,17 +25,22 @@ public class JobService
private final Map<String, ScheduledExecutorService> pollingMap = new ConcurrentHashMap<>();
private final Map<String, Integer> maxQueueCount = new ConcurrentHashMap<>();

private final ExecutorServiceSupplier executorServiceSupplier = new ExecutorServiceSupplier();

@Getter
@Setter
private static long defaultWaitTime = 120;
@Getter
@Setter
private static TimeUnit defaultWaitUnit = TimeUnit.SECONDS;

@Getter
private static final JobService INSTANCE = new JobService();

JobService()
public static JobService getInstance(){
return INSTANCE;
}

public JobService()
{
//No config required
}
Expand Down Expand Up @@ -98,6 +92,7 @@ public ExecutorService removeJob(String pool)
{
es.shutdownNow();
}
es.close();
serviceMap.remove(pool);
return es;
}
Expand Down Expand Up @@ -190,15 +185,15 @@ public ExecutorService addJob(String jobPoolName, Runnable thread)
{
if (!serviceMap.containsKey(jobPoolName) || serviceMap.get(jobPoolName).isTerminated() || serviceMap.get(jobPoolName).isShutdown())
{
registerJobPool(jobPoolName, Executors.newWorkStealingPool());
registerJobPool(jobPoolName, executorServiceSupplier.get());
}

ExecutorService service = serviceMap.get(jobPoolName);
if (getCurrentTaskCount(service) >= maxQueueCount.get(jobPoolName))
{
log.log(Level.FINER, maxQueueCount + " Hit - Finishing before next run");
removeJob(jobPoolName);
service = registerJobPool(jobPoolName, newWorkStealingPool());
service = registerJobPool(jobPoolName,executorServiceSupplier.get());
}
service.execute(thread);
return service;
Expand All @@ -214,15 +209,15 @@ public ExecutorService addJob(String jobPoolName, Callable<?> thread)
{
if (!serviceMap.containsKey(jobPoolName) || serviceMap.get(jobPoolName).isTerminated() || serviceMap.get(jobPoolName).isShutdown())
{
registerJobPool(jobPoolName, newWorkStealingPool());
registerJobPool(jobPoolName, executorServiceSupplier.get());
}

ExecutorService service = serviceMap.get(jobPoolName);
if (getCurrentTaskCount(service) >= maxQueueCount.get(jobPoolName))
{
log.log(Level.FINER, maxQueueCount + " Hit - Finishing before next run");
removeJob(jobPoolName);
service = registerJobPool(jobPoolName, newWorkStealingPool());
service = registerJobPool(jobPoolName, executorServiceSupplier.get());
}
service.submit(thread);
return service;
Expand Down
Loading

0 comments on commit e12b1a9

Please sign in to comment.