Broadcasting saved comments

To consume messages sent via Spring Cloud Stream, the chat application needs its own CommentService:

    @Service 
    @EnableBinding(Sink.class) 
    public class CommentService implements WebSocketHandler { 
 
      private final static Logger log = 
        LoggerFactory.getLogger(CommentService.class); 
        ... 
    } 

The preceding code can be described as follows:

  • @Service marks this as a Spring bean, picked up automatically when the chat microservice starts
  • @EnableBinding(Sink.class) shows this to be a receiver for Spring Cloud Stream messages
  • Our service implements WebSocketHandler, a WebFlux interface that comes with a handle(WebSocketSession) method (which we'll use shortly)
  • An Slf4j Logger is used to print out traffic passing through

This ...

Get Developing Java Applications with Spring and Spring Boot now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.