From b59a38cb7c2c87a670bf53f158006fff4590d251 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Wed, 12 Jun 2024 16:35:58 +0200 Subject: [PATCH] Make events asynchronous + change configuration --- .../sosengine/base/EngineConfiguration.java | 2 +- .../base/classes/helpers/EventHelper.java | 26 ++++++++++++------- .../sosengine/base/events/LogEvent.java | 22 +++++++++++----- 3 files changed, 33 insertions(+), 17 deletions(-) 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(); } }