I have started to play with Reactor but I'm having troubles with my first event :D
Following the example in github, I tried to write an "hello world" but without success...
What is the problem?
Code:
package reactor;
import static reactor.event.selector.Selectors.$;
import reactor.core.Environment;
import reactor.core.Reactor;
import reactor.core.spec.Reactors;
import reactor.event.Event;
import reactor.function.Consumer;
public class Main {
public static void main(String[] args) {
final Environment env = new Environment();
final Reactor reactor = Reactors.reactor(env);
String topic = "event.message";
reactor.on($(topic), new Consumer<Event<Message>>(){
@Override
public void accept(Event<Message> t) {
System.out.println("Hello World");
}
});
final Message event = new Message();
reactor.notify(topic, Event.wrap(event));
System.out.println("ends");
}
public static class Message{
}
}
Output:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
ends
Don't forget that Reactor
is an implementation of Reactive Streams
paradigm and everything there is async
.
So, your reactor.notify(topic, Event.wrap(event));
is an event publishing to the EventRouter
for some handler
within separate Thread
.
So, your main
thead should wait until all downstream work will be done.
Or add Thread.sleep(1000);
in the and of main
or use CoutDownLatch
to wait the event from that Reactor's thread (com.lmax.disruptor.RingBuffer
by defult):
final CountDownLatch stopLatch = new CountDownLatch(1);
reactor.on($(topic), new Consumer<Event<Message>>(){
@Override
public void accept(Event<Message> t) {
System.out.println("Hello World");
stopLatch.countDown();
}
});
....
stopLatch.await();