working on event manager

This commit is contained in:
Jörg Prante 2024-01-30 17:44:12 +01:00
parent 1c1290de0b
commit ef9528c1ad
3 changed files with 26 additions and 22 deletions

View file

@ -155,9 +155,12 @@ public final class EventManager {
private final List<EventConsumer> eventConsumers;
private boolean loadEventConsumers;
private EventManagerBuilder() {
this.eventConsumers = new ArrayList<>();
this.subscriberExceptionHandler = new EventManagerExceptionHandler();
this.loadEventConsumers = true;
}
public EventManagerBuilder setSettings(Settings settings) {
@ -201,17 +204,20 @@ public final class EventManager {
return this;
}
public EventManagerBuilder loadEventConsumers() {
if (classLoader == null) {
classLoader = EventManager.class.getClassLoader();
}
for (EventConsumer eventConsumer : ServiceLoader.load(EventConsumer.class, classLoader)) {
eventConsumers.add(eventConsumer);
}
public EventManagerBuilder loadEventConsumers(boolean enabled) {
this.loadEventConsumers = enabled;
return this;
}
public EventManager build() {
if (classLoader == null) {
classLoader = EventManager.class.getClassLoader();
}
if (loadEventConsumers) {
for (EventConsumer eventConsumer : ServiceLoader.load(EventConsumer.class, classLoader)) {
eventConsumers.add(eventConsumer);
}
}
if (settings == null) {
settings = Settings.emptySettings();
}
@ -222,9 +228,6 @@ public final class EventManager {
executorService = Executors.newFixedThreadPool(threadcount);
}
}
if (classLoader == null) {
classLoader = EventManager.class.getClassLoader();
}
if (eventBus == null) {
eventBus = new AsyncEventBus(executorService, subscriberExceptionHandler);
}

View file

@ -15,17 +15,16 @@ import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class GenericEventManagerTest {
private static final Logger logger = Logger.getLogger(GenericEventManagerTest.class.getName());
@Test
void testGenericEvents() {
Settings settings = Settings.settingsBuilder()
.build();
void testSimpleGenericEvent() {
TestEventConsumer consumer = new TestEventConsumer();
EventManager eventManager = EventManager.builder()
.setSettings(settings)
.register(consumer)
.build();
Event event = EventManager.eventBuilder()
@ -33,16 +32,14 @@ public class GenericEventManagerTest {
.setListener(e -> logger.log(Level.INFO, "received event " + e))
.build();
eventManager.getGenericEventManagerService().post(event);
assertEquals(1, consumer.getCount());
}
@Test
void testGenericEventWithWaitForSubscriber() throws InterruptedException, ExecutionException {
Settings settings = Settings.settingsBuilder()
.build();
void testGenericEventWithWaitForSubscriber() throws InterruptedException, ExecutionException, TimeoutException {
TestEventConsumer consumer = new TestEventConsumer();
EventManager eventManager = EventManager.builder()
.setSettings(settings)
.register(consumer)
.build();
CompletableFuture<Event> future = new CompletableFuture<>();
@ -54,8 +51,7 @@ public class GenericEventManagerTest {
})
.build();
eventManager.getGenericEventManagerService().post(event);
Event e = future.get();
logger.log(Level.INFO, "the event was received with result " + e + ", continuing");
Event e = future.get(1000L, TimeUnit.MILLISECONDS);
}
@Test
@ -68,7 +64,6 @@ public class GenericEventManagerTest {
.setSettings(settings)
.register(consumer1)
.register(consumer2)
.loadEventConsumers()
.build();
CompletableFuture<Event> future = new CompletableFuture<>();
Event event = EventManager.eventBuilder()
@ -87,12 +82,19 @@ public class GenericEventManagerTest {
private static class TestEventConsumer implements EventConsumer {
private int count;
TestEventConsumer() {
}
@Subscribe
public void onEvent(GenericEventImpl event) {
event.received();
count++;
}
public int getCount() {
return count;
}
}
}

View file

@ -48,7 +48,6 @@ public class HttpEventTest {
nettyHttpServerConfig.setDebug(true);
EventManager eventManager = EventManager.builder()
.loadEventConsumers()
.build();
HttpEventManagerService httpEventManagerService = eventManager.getEventManagerService(HttpEventManagerService.class);