- 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));
推薦閱讀
- 新編Visual Basic程序設(shè)計(jì)上機(jī)實(shí)驗(yàn)教程
- JavaScript高效圖形編程
- 小程序?qū)崙?zhàn)視頻課:微信小程序開發(fā)全案精講
- 基于免疫進(jìn)化的算法及應(yīng)用研究
- Learning ELK Stack
- Python完全自學(xué)教程
- ArcGIS By Example
- Getting Started with NativeScript
- 從Java到Web程序設(shè)計(jì)教程
- Mastering Akka
- Web性能實(shí)戰(zhàn)
- JavaScript Unit Testing
- Visual C#(學(xué)習(xí)筆記)
- Practical Time Series Analysis
- Switching to Angular 2