Example usage for org.springframework.web.servlet.mvc.method.annotation SseEmitter send

List of usage examples for org.springframework.web.servlet.mvc.method.annotation SseEmitter send

Introduction

In this page you can find the example usage for org.springframework.web.servlet.mvc.method.annotation SseEmitter send.

Prototype

@Override
public void send(Object object, @Nullable MediaType mediaType) throws IOException 

Source Link

Document

Send the object formatted as a single SSE "data" line.

Usage

From source file:de.codecentric.boot.admin.journal.web.JournalController.java

@EventListener
public void onClientApplicationEvent(ClientApplicationEvent event) {
    for (SseEmitter emitter : new ArrayList<>(emitters)) {
        try {/*from www  .j  av  a 2  s.c om*/
            emitter.send(event, MediaType.APPLICATION_JSON);
        } catch (Exception ex) {
            LOGGER.debug("Error sending event to client ", ex);
        }
    }
}

From source file:io.github.proxyprint.kitchen.utils.NotificationManager.java

public void sendNotification(String username, Notification notification) {
    Consumer consumer = this.consumers.findByUsername(username);
    notification = notifications.save(notification);
    consumer.addNotifications(notification);
    consumers.save(consumer);//from  w  w w  .  j  a v  a2 s  .co  m

    if (this.subscriptions.containsKey(username)) {
        for (SseEmitter sse : this.subscriptions.get(username)) {
            try {
                sse.send(notification, MediaType.APPLICATION_JSON);
            } catch (IOException ex) {
                this.logger.warn("Broken SSE Emitter..discarding it...");
            }
        }
    }
}

From source file:io.spring.isomorphic.CommentController.java

@RequestMapping(value = "/", method = RequestMethod.POST, produces = "application/json")
@ResponseBody//from   w w w  .  j av  a 2  s .  c  om
Comment jsonCreate(Comment comment) throws IOException {
    Comment newComment = this.commentRepository.save(comment);
    for (SseEmitter sseEmitter : this.sseEmitters) {
        // Servlet containers don't always detect ghost connection, so we must catch exceptions ...
        try {
            sseEmitter.send(newComment, MediaType.APPLICATION_JSON);
        } catch (Exception e) {
        }
    }
    return comment;
}