我已经开始玩Reactor,但我遇到了第一次事件的麻烦:D
在github中example之后,我试着写一个“你好世界”,但没有成功......
有什么问题?
代码:
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{
}
}
输出:
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
答案 0 :(得分:2)
不要忘记Reactor
是Reactive Streams
范例的实现,其中的所有内容都是async
。
因此,您的reactor.notify(topic, Event.wrap(event));
是在EventRouter
内的某些handler
发布给Thread
的事件。
因此,您的main
thead应该等待,直到完成所有下游工作。
或者在Thread.sleep(1000);
和[{1}}中添加main
或使用CoutDownLatch
等待来自该Reactor线程的事件(com.lmax.disruptor.RingBuffer
通过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();