diff --git a/base/src/main/java/de/staropensource/sosengine/base/EngineConfiguration.java b/base/src/main/java/de/staropensource/sosengine/base/EngineConfiguration.java index 5d72d55..eb158af 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/EngineConfiguration.java +++ b/base/src/main/java/de/staropensource/sosengine/base/EngineConfiguration.java @@ -201,7 +201,7 @@ public final class EngineConfiguration implements SubsystemConfiguration { private boolean optimizeLogging; /** - * If enabled, allows for {@link java.util.EventListener} precomputation. + * If enabled, will make all events asynchronous. * Don't disable unless you want your application to run slowly. * * @since 1-alpha0 diff --git a/base/src/main/java/de/staropensource/sosengine/base/classes/helpers/EventHelper.java b/base/src/main/java/de/staropensource/sosengine/base/classes/helpers/EventHelper.java index e79727e..437deac 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/classes/helpers/EventHelper.java +++ b/base/src/main/java/de/staropensource/sosengine/base/classes/helpers/EventHelper.java @@ -83,7 +83,7 @@ public class EventHelper { public static LinkedList getAnnotatedMethods(@NotNull Class clazz, boolean forceScanning) { LinkedList methods = new LinkedList<>(); - if (forceScanning || !cachedEventListeners.containsKey(clazz) || EngineConfiguration.getInstance().isOptimizeEvents()) { + if (forceScanning || !cachedEventListeners.containsKey(clazz)) { // Scan entire classpath through Reflections library Reflections reflections = new Reflections( new ConfigurationBuilder() @@ -102,6 +102,7 @@ public class EventHelper { // Sort 'methods' linked list methods.sort(Comparator.comparing(method0 -> method0.getAnnotation(EventListener.class).priority())); } else + // 'forcedScanning' is false and matching event listeners are cached methods = cachedEventListeners.get(clazz); return methods; @@ -126,13 +127,22 @@ public class EventHelper { * @since 1-alpha0 */ public static void invokeAnnotatedMethods(@NotNull Class clazz) { - logCall(clazz); + Runnable eventCode = () -> { + logCall(clazz); - for (Method method : getAnnotatedMethods(clazz)) { - try { - method.invoke(null); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NullPointerException | ExceptionInInitializerError ignored) {} - } + for (Method method : getAnnotatedMethods(clazz)) { + try { + method.invoke(null); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | + NullPointerException | ExceptionInInitializerError ignored) { + } + } + }; + + if (EngineConfiguration.getInstance().isOptimizeEvents()) + Thread.ofVirtual().start(eventCode); + else + eventCode.run(); } /** @@ -143,8 +153,6 @@ public class EventHelper { * @since 1-alpha0 */ public static void precomputeEventListeners(@Nullable Class clazz) { - if (EngineConfiguration.getInstance().isOptimizeEvents()) return; - if (clazz == null) for (Class event : cachedEventListeners.keySet()) precomputeEventListeners(event); diff --git a/base/src/main/java/de/staropensource/sosengine/base/events/LogEvent.java b/base/src/main/java/de/staropensource/sosengine/base/events/LogEvent.java index 026407c..7fc2fae 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/events/LogEvent.java +++ b/base/src/main/java/de/staropensource/sosengine/base/events/LogEvent.java @@ -19,6 +19,7 @@ package de.staropensource.sosengine.base.events; +import de.staropensource.sosengine.base.EngineConfiguration; import de.staropensource.sosengine.base.classes.Event; import de.staropensource.sosengine.base.types.LogIssuer; import de.staropensource.sosengine.base.types.LogLevel; @@ -58,13 +59,20 @@ public final class LogEvent implements Event { * @since 1-alpha0 */ public void callEvent(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String message) { - // Uncommenting this would be a great way to cause a StackOverflowException! - //EventHelper.logCall(getClass(), level, logIssuer, message); + Runnable eventCode = Thread.ofVirtual().start(() -> { + // Uncommenting this would be a great way to cause a StackOverflowException! + //EventHelper.logCall(getClass(), level, logIssuer, message); - for (Method method : EventHelper.getAnnotatedMethods(getClass())) { - try { - method.invoke(null, level, logIssuer, message); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NullPointerException | ExceptionInInitializerError ignored) {} - } + for (Method method : EventHelper.getAnnotatedMethods(getClass())) { + try { + method.invoke(null, level, logIssuer, message); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NullPointerException | ExceptionInInitializerError ignored) {} + } + }); + + if (EngineConfiguration.getInstance().isOptimizeEvents()) + Thread.ofVirtual().start(eventCode); + else + eventCode.run(); } }