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

  • Jakarta EE Cookbook
  • Elder Moraes
  • 149字
  • 2021-06-24 16:12:36

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));
主站蜘蛛池模板: 新竹县| 通化县| 武宁县| 灵宝市| 朝阳区| 资阳市| 尚志市| 贵德县| 义马市| 河池市| 兴城市| 南昌市| 通江县| 徐闻县| 康乐县| 榆社县| 兖州市| 当阳市| 铁力市| 光山县| 泽库县| 新兴县| 温泉县| 鄯善县| 郁南县| 宜兴市| 邛崃市| 池州市| 邳州市| 长白| 曲麻莱县| 调兵山市| 墨玉县| 乌兰浩特市| 水富县| 广元市| 克山县| 华宁县| 万安县| 前郭尔| 海阳市|