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

How it works...

This recipe is made up of three parts:

  • The server, represented by the ServerMock class
  • The SSE engine, represented by the SseResource class
  • The client, represented by the ClientConsumer class

So, once ServerMock is instantiated, it registers the SseResource class:

final ResourceConfig resourceConfig = new ResourceConfig(SseResource.class);
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(CONTEXT, resourceConfig, false);
server.start();

Then, two key methods from SseResource take place. The first one adds messages to the server queue:

addMessage(final String message, @Context Sse sse)

The second one consumes this queue and sends the messages to the clients:

@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public void getMessageQueue(@Context SseEventSink sink)

Note that this one has a media type, SERVER_SENT_EVENTS, introduced in this version for this very purpose. And finally, we have our client. In this recipe, it is both posting and consuming messages. 

It consumes here:

sseSource.register(System.out::println);
sseSource.open();

It posts here:

ServerMock.WEB_TARGET.request().post(Entity.json("event " + innerCounter));
主站蜘蛛池模板: 武定县| 柳河县| 会同县| 垦利县| 石渠县| 新和县| 运城市| 松原市| 疏附县| 澄迈县| 巩义市| 凌云县| 宝兴县| 松阳县| 舟曲县| 苗栗市| 仲巴县| 朔州市| 宣武区| 焉耆| 临西县| 峨眉山市| 德州市| 南郑县| 武威市| 新余市| 同仁县| 长治县| 武冈市| 太和县| 娱乐| 清河县| 嘉禾县| 颍上县| 云南省| 海宁市| 九寨沟县| 莫力| 会昌县| 高安市| 东港市|