working on event manager
This commit is contained in:
parent
1c1290de0b
commit
ef9528c1ad
3 changed files with 26 additions and 22 deletions
|
@ -155,9 +155,12 @@ public final class EventManager {
|
||||||
|
|
||||||
private final List<EventConsumer> eventConsumers;
|
private final List<EventConsumer> eventConsumers;
|
||||||
|
|
||||||
|
private boolean loadEventConsumers;
|
||||||
|
|
||||||
private EventManagerBuilder() {
|
private EventManagerBuilder() {
|
||||||
this.eventConsumers = new ArrayList<>();
|
this.eventConsumers = new ArrayList<>();
|
||||||
this.subscriberExceptionHandler = new EventManagerExceptionHandler();
|
this.subscriberExceptionHandler = new EventManagerExceptionHandler();
|
||||||
|
this.loadEventConsumers = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EventManagerBuilder setSettings(Settings settings) {
|
public EventManagerBuilder setSettings(Settings settings) {
|
||||||
|
@ -201,17 +204,20 @@ public final class EventManager {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EventManagerBuilder loadEventConsumers() {
|
public EventManagerBuilder loadEventConsumers(boolean enabled) {
|
||||||
if (classLoader == null) {
|
this.loadEventConsumers = enabled;
|
||||||
classLoader = EventManager.class.getClassLoader();
|
|
||||||
}
|
|
||||||
for (EventConsumer eventConsumer : ServiceLoader.load(EventConsumer.class, classLoader)) {
|
|
||||||
eventConsumers.add(eventConsumer);
|
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EventManager build() {
|
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) {
|
if (settings == null) {
|
||||||
settings = Settings.emptySettings();
|
settings = Settings.emptySettings();
|
||||||
}
|
}
|
||||||
|
@ -222,9 +228,6 @@ public final class EventManager {
|
||||||
executorService = Executors.newFixedThreadPool(threadcount);
|
executorService = Executors.newFixedThreadPool(threadcount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (classLoader == null) {
|
|
||||||
classLoader = EventManager.class.getClassLoader();
|
|
||||||
}
|
|
||||||
if (eventBus == null) {
|
if (eventBus == null) {
|
||||||
eventBus = new AsyncEventBus(executorService, subscriberExceptionHandler);
|
eventBus = new AsyncEventBus(executorService, subscriberExceptionHandler);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,17 +15,16 @@ import java.util.concurrent.TimeoutException;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
public class GenericEventManagerTest {
|
public class GenericEventManagerTest {
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(GenericEventManagerTest.class.getName());
|
private static final Logger logger = Logger.getLogger(GenericEventManagerTest.class.getName());
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGenericEvents() {
|
void testSimpleGenericEvent() {
|
||||||
Settings settings = Settings.settingsBuilder()
|
|
||||||
.build();
|
|
||||||
TestEventConsumer consumer = new TestEventConsumer();
|
TestEventConsumer consumer = new TestEventConsumer();
|
||||||
EventManager eventManager = EventManager.builder()
|
EventManager eventManager = EventManager.builder()
|
||||||
.setSettings(settings)
|
|
||||||
.register(consumer)
|
.register(consumer)
|
||||||
.build();
|
.build();
|
||||||
Event event = EventManager.eventBuilder()
|
Event event = EventManager.eventBuilder()
|
||||||
|
@ -33,16 +32,14 @@ public class GenericEventManagerTest {
|
||||||
.setListener(e -> logger.log(Level.INFO, "received event " + e))
|
.setListener(e -> logger.log(Level.INFO, "received event " + e))
|
||||||
.build();
|
.build();
|
||||||
eventManager.getGenericEventManagerService().post(event);
|
eventManager.getGenericEventManagerService().post(event);
|
||||||
|
assertEquals(1, consumer.getCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testGenericEventWithWaitForSubscriber() throws InterruptedException, ExecutionException {
|
void testGenericEventWithWaitForSubscriber() throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
Settings settings = Settings.settingsBuilder()
|
|
||||||
.build();
|
|
||||||
TestEventConsumer consumer = new TestEventConsumer();
|
TestEventConsumer consumer = new TestEventConsumer();
|
||||||
EventManager eventManager = EventManager.builder()
|
EventManager eventManager = EventManager.builder()
|
||||||
.setSettings(settings)
|
|
||||||
.register(consumer)
|
.register(consumer)
|
||||||
.build();
|
.build();
|
||||||
CompletableFuture<Event> future = new CompletableFuture<>();
|
CompletableFuture<Event> future = new CompletableFuture<>();
|
||||||
|
@ -54,8 +51,7 @@ public class GenericEventManagerTest {
|
||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
eventManager.getGenericEventManagerService().post(event);
|
eventManager.getGenericEventManagerService().post(event);
|
||||||
Event e = future.get();
|
Event e = future.get(1000L, TimeUnit.MILLISECONDS);
|
||||||
logger.log(Level.INFO, "the event was received with result " + e + ", continuing");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -68,7 +64,6 @@ public class GenericEventManagerTest {
|
||||||
.setSettings(settings)
|
.setSettings(settings)
|
||||||
.register(consumer1)
|
.register(consumer1)
|
||||||
.register(consumer2)
|
.register(consumer2)
|
||||||
.loadEventConsumers()
|
|
||||||
.build();
|
.build();
|
||||||
CompletableFuture<Event> future = new CompletableFuture<>();
|
CompletableFuture<Event> future = new CompletableFuture<>();
|
||||||
Event event = EventManager.eventBuilder()
|
Event event = EventManager.eventBuilder()
|
||||||
|
@ -87,12 +82,19 @@ public class GenericEventManagerTest {
|
||||||
|
|
||||||
private static class TestEventConsumer implements EventConsumer {
|
private static class TestEventConsumer implements EventConsumer {
|
||||||
|
|
||||||
|
private int count;
|
||||||
|
|
||||||
TestEventConsumer() {
|
TestEventConsumer() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
public void onEvent(GenericEventImpl event) {
|
public void onEvent(GenericEventImpl event) {
|
||||||
event.received();
|
event.received();
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCount() {
|
||||||
|
return count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,6 @@ public class HttpEventTest {
|
||||||
nettyHttpServerConfig.setDebug(true);
|
nettyHttpServerConfig.setDebug(true);
|
||||||
|
|
||||||
EventManager eventManager = EventManager.builder()
|
EventManager eventManager = EventManager.builder()
|
||||||
.loadEventConsumers()
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
HttpEventManagerService httpEventManagerService = eventManager.getEventManagerService(HttpEventManagerService.class);
|
HttpEventManagerService httpEventManagerService = eventManager.getEventManagerService(HttpEventManagerService.class);
|
||||||
|
|
Loading…
Reference in a new issue