其他示例程序
快速入门 一个展示了Rector 基本组件数种使用方法的应用。
示例代码 一个通过 JUnit 测试演示 Reactor 功能的简单示例应用。
非阻塞的作弊码
如果你深入到这一步的话,这里有一个非阻塞数据流的示例,让你识别 websocket 中的作弊码。告诉我它的奖励… 不要忘记在 reactor-net 的 classpath 中添加你的 Netty 地址。
final Processor<Integer, Integer> keyboardStream = RingBufferProcessor.create();
NetStreams.<String, String>httpServer(spec ->
spec
.codec(StandardCodecs.STRING_CODEC)
.listen(3000)
)
.ws("/", channel -> {
System.out.println("Connected a websocket client: " + channel.remoteAddress());
return Streams
.wrap(keyboardStream)
.skipWhile(key -> KeyEvent.VK_UP != key)
.buffer(10, 1)
.map(keys -> keys.size() == 10 &&
keys.get(0) == KeyEvent.VK_UP &&
keys.get(1) == KeyEvent.VK_UP &&
keys.get(2) == KeyEvent.VK_DOWN &&
keys.get(3) == KeyEvent.VK_DOWN &&
keys.get(4) == KeyEvent.VK_LEFT &&
keys.get(5) == KeyEvent.VK_RIGHT &&
keys.get(6) == KeyEvent.VK_LEFT &&
keys.get(7) == KeyEvent.VK_RIGHT &&
keys.get(8) == KeyEvent.VK_B &&
keys.get(9) == KeyEvent.VK_A
)
.map(isKonami -> isKonami ? "Konami!" : "Nah")
.nest()
.flatMap(konamis ->
channel.writeWith(konamis)
);
})
.start()
.await();
keyboardStream.onNext(KeyEvent.VK_RIGHT);
keyboardStream.onNext(KeyEvent.VK_UP);
keyboardStream.onNext(KeyEvent.VK_UP);
keyboardStream.onNext(KeyEvent.VK_DOWN);
keyboardStream.onNext(KeyEvent.VK_DOWN);
keyboardStream.onNext(KeyEvent.VK_LEFT);
keyboardStream.onNext(KeyEvent.VK_RIGHT);
keyboardStream.onNext(KeyEvent.VK_LEFT);
keyboardStream.onNext(KeyEvent.VK_RIGHT);
keyboardStream.onNext(KeyEvent.VK_B);
keyboardStream.onNext(KeyEvent.VK_A);
keyboardStream.onNext(KeyEvent.VK_C);
keyboardStream.onComplete();
System.out.println(konamis.await());
- 注意
buffer(size, skip)
的使用,我们希望评估至少10按键之间的全部组合,10 乘 10。就是说我们要创建一个包含所有按键、且每个按键都有10个按键的列表。
1. 除非你希望使用现阶段几乎独立的核心处理器。我们计划在下一步调整调度器和核心处理器,使它们相互协作。2. 有些人可能会挑战这种过度简化的愿景,但还是允许我们在这里更务实些吧 :)3. 包括 Akka Streams,Ratpack 和 RxJava