- Jakarta EE Cookbook
- Elder Moraes
- 307字
- 2021-06-24 16:12:36
How to do it...
We need to perform the following steps to try this recipe:
- First, we create a class that will be our server:
public class ServerMock {
public static final URI CONTEXT =
URI.create("http://localhost:8080/");
public static final String BASE_PATH = "ssevents";
public static void main(String[] args) {
try {
final ResourceConfig resourceConfig = new
ResourceConfig(SseResource.class);
final HttpServer server =
GrizzlyHttpServerFactory.createHttpServer(CONTEXT,
resourceConfig, false);
server.start();
System.out.println(String.format("Mock Server started
at %s%s", CONTEXT, BASE_PATH));
Thread.currentThread().join();
} catch (IOException | InterruptedException ex) {
System.out.println(ex.getMessage());
}
}
}
- Then, we create a JAX-RS endpoint to send the events to the clients:
@Path(ServerMock.BASE_PATH)
public class SseResource {
private static volatile SseEventSink SINK = null;
@GET
@Produces(MediaType.SERVER_SENT_EVENTS)
public void getMessageQueue(@Context SseEventSink sink) {
SseResource.SINK = sink;
}
@POST
public void addMessage(final String message, @Context Sse sse)
throws IOException {
if (SINK != null) {
SINK.send(sse.newEventBuilder()
.name("sse-message")
.id(String.valueOf(System.currentTimeMillis()))
.data(String.class, message)
.comment("")
.build());
}
}
}
- Then, we create a client class to consume the events generated from the server:
public class ClientConsumer {
public static final Client CLIENT = ClientBuilder.newClient();
public static final WebTarget WEB_TARGET =
CLIENT.target(ServerMock.CONTEXT
+ BASE_PATH);
public static void main(String[] args) {
consume();
}
private static void consume() {
try (final SseEventSource sseSource =
SseEventSource
.target(WEB_TARGET)
.build()) {
sseSource.register(System.out::println);
sseSource.open();
for (int counter=0; counter < 5; counter++) {
System.out.println(" ");
for (int innerCounter=0; innerCounter < 5;
innerCounter++) {
WEB_TARGET.request().post(Entity.json("event "
+ innerCounter));
}
Thread.sleep(1000);
}
CLIENT.close();
System.out.println("\n All messages consumed");
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
- For you to try it out, first run the ServerMock class and then the ClientConsumer class. If everything worked well, you should see something like this:
InboundEvent{name='sse-message', id='1502228257736', comment='', data=event 0}
InboundEvent{name='sse-message', id='1502228257753', comment='', data=event 1}
InboundEvent{name='sse-message', id='1502228257758', comment='', data=event 2}
InboundEvent{name='sse-message', id='1502228257763', comment='', data=event 3}
InboundEvent{name='sse-message', id='1502228257768', comment='', data=event 4}
These are the messages sent from the server to the client.
推薦閱讀
- Spring Boot 2實戰之旅
- Advanced Quantitative Finance with C++
- Unreal Engine Physics Essentials
- iOS面試一戰到底
- CockroachDB權威指南
- vSphere High Performance Cookbook
- 樂學Web編程:網站制作不神秘
- Oracle數據庫從入門到運維實戰
- Java EE 7 Development with NetBeans 8
- Web Development with MongoDB and Node(Third Edition)
- Learning ArcGIS for Desktop
- 青少年信息學競賽
- Swift 4從零到精通iOS開發
- Delphi開發典型模塊大全(修訂版)
- Python+Office:輕松實現Python辦公自動化