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