官术网_书友最值得收藏!

Ratpack improvements

Along with Vert.x, another well-known web framework called Ratpack also provides support for Reactive Streams. In contrast to Vert.x, Ratpack offers direct support for Reactive Streams. For example, sending streams of logs in a Ratpack case looks like the following:

RatpackServer.start(server ->                                      // (1)
server.handlers(chain -> //
chain.all(ctx -> { //

Publisher<String> logs = logsService.stream(); // (2)

ServerSentEvents events = serverSentEvents( // (3)
logs, //
event -> event.id(Objects::toString) // (3.1)
.event("log") //
.data(Function.identity()) //
); //

ctx.render(events); // (4)
})
)
);

The key is as follows:

  1. This is the server start's action and request handler declaration. 
  2. This is the logs stream declaration.
  3. This is the ServerSentEvents preparation. Here, the mentioned class plays a role in the mapping stage which converts the elements in the Publisher to the Server-Sent Events' representative. As we may have noticed, ServerSentEvents mandates the mapper function declaration, which describes how to map an element to the particular Event's fields.
  4. This is the rendering of the stream to I/O. 

As we may see from the example, Ratpack provides the support for Reactive Streams in the core. Now, the same LogService#stream method may be reused without providing an additional type conversion or a requirement for the additional modules to add support for the particular reactive library. 

Moreover, in contrast to Vert.x, which provides just plain support of the Reactive Streams specification, Ratpack provides its own implementation of the interfaces of the specification. This functionality is available within the ratpack.stream.Streams class, which is similar to RxJava API:


Publisher<String> logs = logsService.stream();
TransformablePublisher publisher = Streams
.transformable(logs)
.filter(this::filterUsersSensitiveLogs)
.map(this::escape);

Here, the Ratpack offers a static factory to convert any Publisher to TransformablePublisher, which in turn gives the ability to flexibly process a stream of events using familiar operators and transformational stages.

主站蜘蛛池模板: 新源县| 凉山| 米泉市| 赤水市| 来宾市| 凤凰县| 横峰县| 浮山县| 东乌珠穆沁旗| 申扎县| 肇庆市| 肥西县| 霸州市| 获嘉县| 深州市| 宜春市| 格尔木市| 广昌县| 巴东县| 阳原县| 葫芦岛市| 福海县| 徐汇区| 永昌县| 武川县| 旬邑县| 临城县| 来安县| 岳阳县| 谷城县| 游戏| 青浦区| 柳河县| 泊头市| 朝阳区| 望奎县| 威海市| 张家川| 石家庄市| 南和县| 临洮县|