Code examples for sending and receiving messages
To better understand how all this fits together, let’s look at some source code examples.Spring Cloud Stream comes with two programming models: one older and nowadays deprecated model based on the use of annotations (for example, @EnableBinding
, @Output
, and @StreamListener
) and one newer model based on writing functions. In this book, we will use functional implementations.To implement a publisher, we only need to implement the java.util.function.Supplier
functional interface as a Spring bean. For example, the following is a publisher that publishes messages as a string:
@Bean
public Supplier<String> myPublisher() {
return () -> new Date().toString();
}
A subscriber is implemented as a Spring bean implementing the java.util.function.Consumer
functional interface. For example, the following is a subscriber that consumes messages as strings:
@Bean
public Consumer<String> mySubscriber() {
return s -> System...