其他示例程序

快速入门 一个展示了Rector 基本组件数种使用方法的应用。

示例代码 一个通过 JUnit 测试演示 Reactor 功能的简单示例应用。

非阻塞的作弊码

如果你深入到这一步的话,这里有一个非阻塞数据流的示例,让你识别 websocket 中的作弊码。告诉我它的奖励… 不要忘记在 reactor-net 的 classpath 中添加你的 Netty 地址。

  1. final Processor<Integer, Integer> keyboardStream = RingBufferProcessor.create();
  2. NetStreams.<String, String>httpServer(spec ->
  3. spec
  4. .codec(StandardCodecs.STRING_CODEC)
  5. .listen(3000)
  6. )
  7. .ws("/", channel -> {
  8. System.out.println("Connected a websocket client: " + channel.remoteAddress());
  9. return Streams
  10. .wrap(keyboardStream)
  11. .skipWhile(key -> KeyEvent.VK_UP != key)
  12. .buffer(10, 1)
  13. .map(keys -> keys.size() == 10 &&
  14. keys.get(0) == KeyEvent.VK_UP &&
  15. keys.get(1) == KeyEvent.VK_UP &&
  16. keys.get(2) == KeyEvent.VK_DOWN &&
  17. keys.get(3) == KeyEvent.VK_DOWN &&
  18. keys.get(4) == KeyEvent.VK_LEFT &&
  19. keys.get(5) == KeyEvent.VK_RIGHT &&
  20. keys.get(6) == KeyEvent.VK_LEFT &&
  21. keys.get(7) == KeyEvent.VK_RIGHT &&
  22. keys.get(8) == KeyEvent.VK_B &&
  23. keys.get(9) == KeyEvent.VK_A
  24. )
  25. .map(isKonami -> isKonami ? "Konami!" : "Nah")
  26. .nest()
  27. .flatMap(konamis ->
  28. channel.writeWith(konamis)
  29. );
  30. })
  31. .start()
  32. .await();
  33. keyboardStream.onNext(KeyEvent.VK_RIGHT);
  34. keyboardStream.onNext(KeyEvent.VK_UP);
  35. keyboardStream.onNext(KeyEvent.VK_UP);
  36. keyboardStream.onNext(KeyEvent.VK_DOWN);
  37. keyboardStream.onNext(KeyEvent.VK_DOWN);
  38. keyboardStream.onNext(KeyEvent.VK_LEFT);
  39. keyboardStream.onNext(KeyEvent.VK_RIGHT);
  40. keyboardStream.onNext(KeyEvent.VK_LEFT);
  41. keyboardStream.onNext(KeyEvent.VK_RIGHT);
  42. keyboardStream.onNext(KeyEvent.VK_B);
  43. keyboardStream.onNext(KeyEvent.VK_A);
  44. keyboardStream.onNext(KeyEvent.VK_C);
  45. keyboardStream.onComplete();
  46. System.out.println(konamis.await());
  • 注意 buffer(size, skip) 的使用,我们希望评估至少10按键之间的全部组合,10 乘 10。就是说我们要创建一个包含所有按键、且每个按键都有10个按键的列表。

1. 除非你希望使用现阶段几乎独立的核心处理器。我们计划在下一步调整调度器和核心处理器,使它们相互协作。2. 有些人可能会挑战这种过度简化的愿景,但还是允许我们在这里更务实些吧 :)3. 包括 Akka StreamsRatpackRxJava