public RateLimiterEventsEmitter(Flux<RateLimiterEventDTO> eventStream) { this.sseEmitter = new SseEmitter(); this.sseEmitter.onCompletion(this::unsubscribe); this.sseEmitter.onTimeout(this::unsubscribe); this.disposable = eventStream.subscribe(this::notify, this.sseEmitter::completeWithError, this.sseEmitter::complete); }
private CircuitBreakerEventEmitter(Flux<CircuitBreakerEventDTO> eventStream) { this.sseEmitter = new SseEmitter(); this.sseEmitter.onCompletion(this::unsubscribe); this.sseEmitter.onTimeout(this::unsubscribe); this.disposable = eventStream.subscribe(this::notify, this.sseEmitter::completeWithError, this.sseEmitter::complete); }
ServerSentEvent.class.isAssignableFrom(elementClass)) { logExecutorWarning(returnType); SseEmitter emitter = new SseEmitter(STREAMING_TIMEOUT_VALUE); new SseEmitterSubscriber(emitter, this.taskExecutor).connect(adapter, returnValue); return emitter;
@Before public void setup() throws IOException { this.handler = new TestHandler(); this.emitter = new SseEmitter(); this.emitter.initialize(this.handler); }
ServerSentEvent.class.isAssignableFrom(elementClass)) { logExecutorWarning(returnType); SseEmitter emitter = new SseEmitter(STREAMING_TIMEOUT_VALUE); new SseEmitterSubscriber(emitter, this.taskExecutor).connect(adapter, returnValue); return emitter;
@Test public void responseEntitySse() throws Exception { MethodParameter type = on(TestController.class).resolveReturnType(ResponseEntity.class, SseEmitter.class); ResponseEntity<SseEmitter> entity = ResponseEntity.ok().header("foo", "bar").body(new SseEmitter()); this.handler.handleReturnValue(entity, type, this.mavContainer, this.webRequest); assertTrue(this.request.isAsyncStarted()); assertEquals(200, this.response.getStatus()); assertEquals("text/event-stream;charset=UTF-8", this.response.getContentType()); assertEquals("bar", this.response.getHeader("foo")); }
@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()); }
private final List<SseEmitter> sseEmitter = new LinkedList<>(); @RequestMapping (path = "/register", method = RequestMethod.GET) public SseEmitter register() throws IOException { log.info("Registering a stream."); SseEmitter emitter = new SseEmitter(); synchronized (sseEmitter) { sseEmitter.add(emitter); } emitter.onCompletion(() -> sseEmitter.remove(emitter)); return emitter; }
@RequestMapping(path = "/stream", method = RequestMethod.GET) public SseEmitter stream() throws IOException { SseEmitter emitter = new SseEmitter(); emitters.add(emitter); emitter.onCompletion(() -> emitters.remove(emitter)); return emitter; }
@RequestMapping(produces = "text/event-stream") public SseEmitter getJournalEvents() { final SseEmitter emitter = new SseEmitter(); emitter.onCompletion(new Runnable() { @Override public void run() { emitters.remove(emitter); } }); emitters.add(emitter); return emitter; }
@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; }
@RequestMapping(value = "/temperature-stream", method = RequestMethod.GET) public SseEmitter events(HttpServletRequest request) { log.info("SSE stream opened for client: " + request.getRemoteAddr()); SseEmitter emitter = new SseEmitter(SSE_SESSION_TIMEOUT); clients.add(emitter); // Remove SseEmitter from active clients on error or client disconnect emitter.onTimeout(() -> clients.remove(emitter)); emitter.onCompletion(() -> clients.remove(emitter)); return emitter; }
@RequestMapping("/listener/{id}") public SseEmitter listen(@PathVariable("id") String id){ if(this.listenerType == null) throw new ListenerTypeNotFound(); final SseEmitter sseEmitter = new SseEmitter(); applicationEventListener.addSseEmitters(this.listenerType , id, sseEmitter); return sseEmitter; }
public SseEmitter getFiniteMessages(int count) { final SseEmitter emitter = new SseEmitter(); processor.processMany(count, executorService) .subscribe( value -> onNext(emitter, value), emitter::completeWithError, emitter::complete ); return emitter; }
private SseEmitter emit(Route route, Message<Collection<Object>> message) throws IOException { SseEmitter emitter = new SseEmitter(Long.MAX_VALUE); String path = route.getPath(); if (!emitters.containsKey(path)) { emitters.putIfAbsent(path, new HashSet<>()); } emitters.get(path).add(emitter); emitter.onCompletion(() -> emitters.get(path).remove(emitter)); emitter.onTimeout(() -> emitters.get(path).remove(emitter)); for (Object body : message.getPayload()) { emitter.send(body); } return emitter; }
@GetMapping("/kafka-messages") public SseEmitter getKafkaMessages() { SseEmitter emitter = new SseEmitter( 1 * 60 * 1000L ); emitters.add(emitter); emitter.onCompletion(new Runnable() { @Override public void run() { emitters.remove(emitter); } }); emitter.onTimeout(new Runnable() { @Override public void run() { emitters.remove(emitter); } }); return emitter; }
@RequestMapping("/whatever") public SseEmitter index( SseEmitter emitter = new SseEmitter(); Publisher<String> responsePublisher = someResponseGenerator.getPublisher(); Observable<String> responseObservable = RxReactiveStreams.toObservable(responsePublisher); responseObservable.subscribe( str -> { try { emitter.send(str); } catch (IOException ex) { emitter.completeWithError(ex); } }, error -> { emitter.completeWithError(error); }, emitter::complete ); return emitter; };
//... private SseEmitter emitter; @RequestMapping(value = "/{imei}", method = RequestMethod.GET) public User getUser(@PathVariable String imei) { User myUser = null; // .. do resolving of myUser (e.g. database etc). // Send message to "connected" web page: if (emitter != null) { emitter.send(myUser.toString()); // Or format otherwise, e.g. JSON. } // This return value goes back as a response to your android device // i.e. the caller of the getUser rest service. return myUser; } @RequestMapping(value = "/sse") public SseEmitter getSseEmitter() { emitter = new SseEmitter(); return emitter; }
@RequestMapping(value = "/getNotificationCount/{userId}",method = RequestMethod.GET) public SseEmitter getNotificationCount(@PathVariable("userId") String userId, HttpServletResponse response){ SseEmitter emitter = null; try { emitter = new SseEmitter(); while(true) { try{ int count= myService.getNotificationCount(Integer.parseInt(userId)); emitter.send(count); Thread.sleep(30 * // minutes to sleep 60 * // seconds to a minute 1000); // milliseconds to a second }catch(ClientAbortException cae){ LOGGER.info("ClientAbortException Breaking the notification stream"); break; } } //Closes the stream emitter.complete(); } catch (Exception e) { //Closes the stream emitter.complete(); } return emitter; }
@RequestMapping(value = BASE_PATH + "/getAccountBalances", method = RequestMethod.GET) public SseEmitter getAccountBalances(@QueryParam("accountId") Integer[] accountIds) { final SseEmitter emitter = new SseEmitter(); new Thread(new Runnable() { @Override public void run() { try { for (int xx = 0; xx < ids.length; xx++) { Thread.sleep(2000L + rand.nextInt(2000)); BalanceDetails balance = new BalanceDetails(); ... emitter.send(emitter.event().name("accountBalance").id(String.valueOf(accountIds[xx])) .data(balance, MediaType.APPLICATION_JSON)); } emitter.send(SseEmitter.event().name("complete").data("complete")); emitter.complete(); } catch (Exception ee) { ee.printStackTrace(); emitter.completeWithError(ee); } } }).start(); return emitter; }