Make events asynchronous + change configuration
This commit is contained in:
parent
4592fa0828
commit
b59a38cb7c
3 changed files with 33 additions and 17 deletions
|
@ -201,7 +201,7 @@ public final class EngineConfiguration implements SubsystemConfiguration {
|
||||||
private boolean optimizeLogging;
|
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.
|
* Don't disable unless you want your application to run slowly.
|
||||||
*
|
*
|
||||||
* @since 1-alpha0
|
* @since 1-alpha0
|
||||||
|
|
|
@ -83,7 +83,7 @@ public class EventHelper {
|
||||||
public static LinkedList<Method> getAnnotatedMethods(@NotNull Class<? extends Event> clazz, boolean forceScanning) {
|
public static LinkedList<Method> getAnnotatedMethods(@NotNull Class<? extends Event> clazz, boolean forceScanning) {
|
||||||
LinkedList<Method> methods = new LinkedList<>();
|
LinkedList<Method> methods = new LinkedList<>();
|
||||||
|
|
||||||
if (forceScanning || !cachedEventListeners.containsKey(clazz) || EngineConfiguration.getInstance().isOptimizeEvents()) {
|
if (forceScanning || !cachedEventListeners.containsKey(clazz)) {
|
||||||
// Scan entire classpath through Reflections library
|
// Scan entire classpath through Reflections library
|
||||||
Reflections reflections = new Reflections(
|
Reflections reflections = new Reflections(
|
||||||
new ConfigurationBuilder()
|
new ConfigurationBuilder()
|
||||||
|
@ -102,6 +102,7 @@ public class EventHelper {
|
||||||
// Sort 'methods' linked list
|
// Sort 'methods' linked list
|
||||||
methods.sort(Comparator.comparing(method0 -> method0.getAnnotation(EventListener.class).priority()));
|
methods.sort(Comparator.comparing(method0 -> method0.getAnnotation(EventListener.class).priority()));
|
||||||
} else
|
} else
|
||||||
|
// 'forcedScanning' is false and matching event listeners are cached
|
||||||
methods = cachedEventListeners.get(clazz);
|
methods = cachedEventListeners.get(clazz);
|
||||||
|
|
||||||
return methods;
|
return methods;
|
||||||
|
@ -126,13 +127,22 @@ public class EventHelper {
|
||||||
* @since 1-alpha0
|
* @since 1-alpha0
|
||||||
*/
|
*/
|
||||||
public static void invokeAnnotatedMethods(@NotNull Class<? extends Event> clazz) {
|
public static void invokeAnnotatedMethods(@NotNull Class<? extends Event> clazz) {
|
||||||
logCall(clazz);
|
Runnable eventCode = () -> {
|
||||||
|
logCall(clazz);
|
||||||
|
|
||||||
for (Method method : getAnnotatedMethods(clazz)) {
|
for (Method method : getAnnotatedMethods(clazz)) {
|
||||||
try {
|
try {
|
||||||
method.invoke(null);
|
method.invoke(null);
|
||||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NullPointerException | ExceptionInInitializerError ignored) {}
|
} 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
|
* @since 1-alpha0
|
||||||
*/
|
*/
|
||||||
public static void precomputeEventListeners(@Nullable Class<? extends Event> clazz) {
|
public static void precomputeEventListeners(@Nullable Class<? extends Event> clazz) {
|
||||||
if (EngineConfiguration.getInstance().isOptimizeEvents()) return;
|
|
||||||
|
|
||||||
if (clazz == null)
|
if (clazz == null)
|
||||||
for (Class<? extends Event> event : cachedEventListeners.keySet())
|
for (Class<? extends Event> event : cachedEventListeners.keySet())
|
||||||
precomputeEventListeners(event);
|
precomputeEventListeners(event);
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
package de.staropensource.sosengine.base.events;
|
package de.staropensource.sosengine.base.events;
|
||||||
|
|
||||||
|
import de.staropensource.sosengine.base.EngineConfiguration;
|
||||||
import de.staropensource.sosengine.base.classes.Event;
|
import de.staropensource.sosengine.base.classes.Event;
|
||||||
import de.staropensource.sosengine.base.types.LogIssuer;
|
import de.staropensource.sosengine.base.types.LogIssuer;
|
||||||
import de.staropensource.sosengine.base.types.LogLevel;
|
import de.staropensource.sosengine.base.types.LogLevel;
|
||||||
|
@ -58,13 +59,20 @@ public final class LogEvent implements Event {
|
||||||
* @since 1-alpha0
|
* @since 1-alpha0
|
||||||
*/
|
*/
|
||||||
public void callEvent(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String message) {
|
public void callEvent(@NotNull LogLevel level, @NotNull LogIssuer logIssuer, @NotNull String message) {
|
||||||
// Uncommenting this would be a great way to cause a StackOverflowException!
|
Runnable eventCode = Thread.ofVirtual().start(() -> {
|
||||||
//EventHelper.logCall(getClass(), level, logIssuer, message);
|
// Uncommenting this would be a great way to cause a StackOverflowException!
|
||||||
|
//EventHelper.logCall(getClass(), level, logIssuer, message);
|
||||||
|
|
||||||
for (Method method : EventHelper.getAnnotatedMethods(getClass())) {
|
for (Method method : EventHelper.getAnnotatedMethods(getClass())) {
|
||||||
try {
|
try {
|
||||||
method.invoke(null, level, logIssuer, message);
|
method.invoke(null, level, logIssuer, message);
|
||||||
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NullPointerException | ExceptionInInitializerError ignored) {}
|
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NullPointerException | ExceptionInInitializerError ignored) {}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (EngineConfiguration.getInstance().isOptimizeEvents())
|
||||||
|
Thread.ofVirtual().start(eventCode);
|
||||||
|
else
|
||||||
|
eventCode.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue