Make events asynchronous + change configuration

This commit is contained in:
JeremyStar™ 2024-06-12 16:35:58 +02:00
parent 4592fa0828
commit b59a38cb7c
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
3 changed files with 33 additions and 17 deletions

View file

@ -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

View file

@ -83,7 +83,7 @@ public class EventHelper {
public static LinkedList<Method> getAnnotatedMethods(@NotNull Class<? extends Event> clazz, boolean forceScanning) {
LinkedList<Method> 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<? extends Event> 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<? extends Event> clazz) {
if (EngineConfiguration.getInstance().isOptimizeEvents()) return;
if (clazz == null)
for (Class<? extends Event> event : cachedEventListeners.keySet())
precomputeEventListeners(event);

View file

@ -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();
}
}