Simplifying Spring Cloud Stream
In Spring framework, we always talk about reducing the boilerplate code. In case of Spring Cloud Stream applications, there has been a shift towards a function-based programming architecture as opposed to the traditional annotation-based approach. This shift is aligned with Spring Boot principles and simplifies the development.
Annotation based approach:
Java
@SpringBootApplication @EnableBinding (Processor. class ) public class MyApplication { @StreamListener (Processor.INPUT) @SendTo (Processor.OUTPUT) public String uppercase(String value) { return value.toUpperCase(); } public static void main(String[] args) { SpringApplication.run(MyApplication. class , args); } } |
In this annotation-based approach, the @EnableBinding annotation is used to specify the messaging channels. @StreamListener annotation is used to make a method as listener for messages arriving on the input channels. @SendTo is used to specify the output channel where the processed message should be sent.
Function based approach:
Java
@SpringBootApplication public class MyApplication { @Bean public Function<String, String> uppercase() { return value -> value.toUpperCase(); } public static void main(String[] args) { SpringApplication.run(MyApplication. class , args); } } |
Here as we can see we are not using any of the annotations like EnableBinding or StreamListener, instead functions are defined as Java beans, where each function acts as a processor that take an input and provides output. Here we have taken upperCase() method as an example which tales a string input and returns its uppercase version as output.
Both the techniques used here are correct and provides same output for processed message. The function-based programming model here simplifies the development process by reducing explicit annotations which improves development experience.
Spring Cloud Stream – Demystified and Simplified
Spring framework was introduced for ease of Java application development. Later spring boot gained popularity for its easy-to-configure nature. To make the framework survive in the industry with demand for event-driver streaming architecture, Spring introduced a new framework named Spring Cloud Stream. People started calling it a lightweight Spring integration, but it is not and never was.
In this article, we will be exploring the Spring Cloud Stream demystifies the purpose of Spring Cloud Stream and will try to simplify the understanding of this framework.
Contact Us