Hallo,
I am currently experimenting with AsyncEventBus and I am facing an unexpected event order that I do not understand. With respect to the docs, I thought that events are delivered in exact the order in which they were posted:
Have a look to the following code:
public class Test {
private static AsyncEventBus eventBus = new AsyncEventBus(Executors.newFixedThreadPool(10));
public static void main(String[] args) throws Exception {
eventBus.register(new EventListener());
eventBus.register(new SlowEventListener());
eventBus.register(new EventListener());
eventBus.post("E1: " + Instant.now());
eventBus.post("E2: " + Instant.now());
}
public static class EventListener {
@Subscribe
public void changeOccurred(Object event) {
System.out.println(Thread.currentThread() + "- " + this +": " + event);
}
}
public static class SlowEventListener {
@Subscribe
public void changeOccurred(Object event) throws Exception {
System.out.println(Thread.currentThread() + "- " + this +": " + event);
Thread.sleep(2000);
eventBus.post("E INTERNAL: " + Instant.now());
}
}
}
I get the following output:
Thread[pool-1-thread-3,5,main]- test.Test$EventListener@59f95c5d: E1: 2017-04-19T10:39:05.254Z
Thread[pool-1-thread-2,5,main]- test.Test$SlowEventListener@48cf768c: E1: 2017-04-19T10:39:05.254Z
Thread[pool-1-thread-6,5,main]- test.Test$EventListener@59f95c5d: E2: 2017-04-19T10:39:05.329Z
Thread[pool-1-thread-1,5,main]- test.Test$EventListener@39ed3c8d: E1: 2017-04-19T10:39:05.254Z
Thread[pool-1-thread-4,5,main]- test.Test$EventListener@39ed3c8d: E2: 2017-04-19T10:39:05.329Z
Thread[pool-1-thread-7,5,main]- test.Test$EventListener@39ed3c8d: E INTERNAL: 2017-04-19T10:39:07.330Z
Thread[pool-1-thread-8,5,main]- test.Test$SlowEventListener@48cf768c: E INTERNAL: 2017-04-19T10:39:07.330Z
Thread[pool-1-thread-9,5,main]- test.Test$EventListener@59f95c5d: E INTERNAL: 2017-04-19T10:39:07.330Z
Thread[pool-1-thread-10,5,main]- test.Test$EventListener@39ed3c8d: E INTERNAL: 2017-04-19T10:39:09.331Z
Thread[pool-1-thread-3,5,main]- test.Test$EventListener@59f95c5d: E INTERNAL: 2017-04-19T10:39:09.331Z
Thread[pool-1-thread-5,5,main]- test.Test$SlowEventListener@48cf768c: E2: 2017-04-19T10:39:05.329Z
Interesting is:
Thread[pool-1-thread-2,5,main]- test.Test$SlowEventListener@48cf768c: E1: 2017-04-19T10:39:05.254Z
Thread[pool-1-thread-8,5,main]- test.Test$SlowEventListener@48cf768c: E INTERNAL: 2017-04-19T10:39:07.330Z
Thread[pool-1-thread-5,5,main]- test.Test$SlowEventListener@48cf768c: E2: 2017-04-19T10:39:05.329Z
The event delivery order is not the same as the event posting order. I would expect that E INTERNAL: 2017-04-19T10:39:07.330Z comes after E2: 2017-04-19T10:39:05.329Z.
This output confuses me. Can someone help? What's going on here?
Regards,
Christoph