O'Reilly logo

Learning Spring Boot 2.0 - Second Edition by Greg L. Turnquist

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required