/** * Send the object formatted as a single SSE "data" line. It's equivalent to: * <pre> * // static import of SseEmitter.* * * SseEmitter emitter = new SseEmitter(); * emitter.send(event().data(myObject)); * </pre> * * <p>Please, see {@link ResponseBodyEmitter#send(Object) parent Javadoc} * for important notes on exception handling. * * @param object the object to write * @throws IOException raised when an I/O error occurs * @throws java.lang.IllegalStateException wraps any other errors */ @Override public void send(Object object) throws IOException { send(object, null); }
/** * Send the object formatted as a single SSE "data" line. It's equivalent to: * <pre> * // static import of SseEmitter.* * * SseEmitter emitter = new SseEmitter(); * emitter.send(event().data(myObject)); * </pre> * * <p>Please, see {@link ResponseBodyEmitter#send(Object) parent Javadoc} * for important notes on exception handling. * * @param object the object to write * @throws IOException raised when an I/O error occurs * @throws java.lang.IllegalStateException wraps any other errors */ @Override public void send(Object object) throws IOException { send(object, null); }
private void notify(RateLimiterEventDTO rateLimiterEventDTO){ try { sseEmitter.send(rateLimiterEventDTO, MediaType.APPLICATION_JSON); } catch (IOException e) { LOG.warn("Failed to send circuitbreaker event", e); } }
private void notify(CircuitBreakerEventDTO circuitBreakerEvent){ try { sseEmitter.send(circuitBreakerEvent, MediaType.APPLICATION_JSON); } catch (IOException e) { LOG.warn("Failed to send circuitbreaker event", e); } }
/** * Send the object formatted as a single SSE "data" line. It's equivalent to: * <pre> * // static import of SseEmitter.* * * SseEmitter emitter = new SseEmitter(); * emitter.send(event().data(myObject, MediaType.APPLICATION_JSON)); * </pre> * * <p>Please, see {@link ResponseBodyEmitter#send(Object) parent Javadoc} * for important notes on exception handling. * * @param object the object to write * @param mediaType a MediaType hint for selecting an HttpMessageConverter * @throws IOException raised when an I/O error occurs */ @Override public void send(Object object, @Nullable MediaType mediaType) throws IOException { send(event().data(object, mediaType)); }
@Override protected void send(Object element) throws IOException { if (element instanceof ServerSentEvent) { ServerSentEvent<?> event = (ServerSentEvent<?>) element; ((SseEmitter) getEmitter()).send(adapt(event)); } else { getEmitter().send(element, MediaType.APPLICATION_JSON); } }
/** * Send the object formatted as a single SSE "data" line. It's equivalent to: * <pre> * // static import of SseEmitter.* * * SseEmitter emitter = new SseEmitter(); * emitter.send(event().data(myObject, MediaType.APPLICATION_JSON)); * </pre> * * <p>Please, see {@link ResponseBodyEmitter#send(Object) parent Javadoc} * for important notes on exception handling. * * @param object the object to write * @param mediaType a MediaType hint for selecting an HttpMessageConverter * @throws IOException raised when an I/O error occurs */ @Override public void send(Object object, @Nullable MediaType mediaType) throws IOException { send(event().data(object, mediaType)); }
@Override protected void send(Object element) throws IOException { if (element instanceof ServerSentEvent) { ServerSentEvent<?> event = (ServerSentEvent<?>) element; ((SseEmitter) getEmitter()).send(adapt(event)); } else { getEmitter().send(element, MediaType.APPLICATION_JSON); } }
@Test public void sendEventEmpty() throws Exception { this.emitter.send(event()); this.handler.assertSentObjectCount(0); }
@Test public void send() throws Exception { this.emitter.send("foo"); this.handler.assertSentObjectCount(3); this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(1, "foo"); this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN); }
@Test public void sendWithMediaType() throws Exception { this.emitter.send("foo", MediaType.TEXT_PLAIN); this.handler.assertSentObjectCount(3); this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(1, "foo", MediaType.TEXT_PLAIN); this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN); }
@Test public void sendEventWithDataLine() throws Exception { this.emitter.send(event().data("foo")); this.handler.assertSentObjectCount(3); this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(1, "foo"); this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN); }
@Test public void sendEventWithTwoDataLines() throws Exception { this.emitter.send(event().data("foo").data("bar")); this.handler.assertSentObjectCount(5); this.handler.assertObject(0, "data:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(1, "foo"); this.handler.assertObject(2, "\ndata:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(3, "bar"); this.handler.assertObject(4, "\n\n", SseEmitter.TEXT_PLAIN); }
@Test public void sendEventFull() throws Exception { this.emitter.send(event().comment("blah").name("test").reconnectTime(5000L).id("1").data("foo")); this.handler.assertSentObjectCount(3); this.handler.assertObject(0, ":blah\nevent:test\nretry:5000\nid:1\ndata:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(1, "foo"); this.handler.assertObject(2, "\n\n", SseEmitter.TEXT_PLAIN); }
@Test public void sendEventFullWithTwoDataLinesInTheMiddle() throws Exception { this.emitter.send(event().comment("blah").data("foo").data("bar").name("test").reconnectTime(5000L).id("1")); this.handler.assertSentObjectCount(5); this.handler.assertObject(0, ":blah\ndata:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(1, "foo"); this.handler.assertObject(2, "\ndata:", SseEmitter.TEXT_PLAIN); this.handler.assertObject(3, "bar"); this.handler.assertObject(4, "\nevent:test\nretry:5000\nid:1\n\n", SseEmitter.TEXT_PLAIN); }
@Test public void sseEmitter() throws Exception { MethodParameter type = on(TestController.class).resolveReturnType(SseEmitter.class); SseEmitter emitter = new SseEmitter(); this.handler.handleReturnValue(emitter, type, this.mavContainer, this.webRequest); assertTrue(this.request.isAsyncStarted()); assertEquals(200, this.response.getStatus()); assertEquals("text/event-stream;charset=UTF-8", this.response.getContentType()); SimpleBean bean1 = new SimpleBean(); bean1.setId(1L); bean1.setName("Joe"); SimpleBean bean2 = new SimpleBean(); bean2.setId(2L); bean2.setName("John"); emitter.send(SseEmitter.event(). comment("a test").name("update").id("1").reconnectTime(5000L).data(bean1).data(bean2)); assertEquals(":a test\n" + "event:update\n" + "id:1\n" + "retry:5000\n" + "data:{\"id\":1,\"name\":\"Joe\"}\n" + "data:{\"id\":2,\"name\":\"John\"}\n" + "\n", this.response.getContentAsString()); }
@EventListener public void onClientApplicationEvent(ClientApplicationEvent event) { for (SseEmitter emitter : new ArrayList<>(emitters)) { try { emitter.send(event, MediaType.APPLICATION_JSON); } catch (Exception ex) { LOGGER.debug("Error sending event to client ", ex); } } }
@RequestMapping(path = "/test") public SseEmitter getJournalEvents() { final SseEmitter emitter = new SseEmitter(); try { emitter.send("This commits the response and now let the request timeout to see the exception."); } catch (IOException ex) { LOGGER.debug("error sending event", ex); } return emitter; }
@Scheduled(fixedRate = 1000) void timerHandler() { try { emitter.send(new Message(new Date().toString()), MediaType.APPLICATION_JSON); } catch (Exception e) { emitter.completeWithError(e); } } }
@Override protected void send(Object element) throws IOException { if (element instanceof ServerSentEvent) { ServerSentEvent<?> event = (ServerSentEvent<?>) element; ((SseEmitter) getEmitter()).send(adapt(event)); } else { getEmitter().send(element, MediaType.APPLICATION_JSON); } }