SCS is a framework for building highly scalable event-driven microservices connected with shared messaging systems. The benefit of using Spring Cloud Stream is loose coupling between message broker and application. We can change the message broker without doing any changes in the code, the only thing we to change properties in application.properties file. For example, if we don't use Spring Cloud Stream then we need to create one config class where we should declare the following beans as below to declare Queue, Topic Exchange (Binding both of these with routing key), and Amqp Template.
If we use Spring Cloud Stream then the entire above work will be handled by Spring Cloud Stream. Let us check the sample project for that. Our POM.xml file looks like majorly we require Spring Cloud Stream and Message Broker dependency (in this case it is RabbitMQ).
Our application.properties file look like this:
Here point to be noted is while binding any binder we should use <binder_name>-in or out-index no., where in or out means flow from application to broker and index is used for the same binder. By using this we can bind any number of the bean as we want.
Message Producer is looking like this:
Here StreamBridge will send the message to “producer-out-0” binding which is bound with queue sample.queue.processor refer application.properties.
Our Processor looks like this:
Here our processor will listen to sample.queue.processor and send the response to sample.queue.consumer after performing our business logic.
Last but not least our consumer looks like this:
It will listen to queue sample.queue.consumer for any message.
In this, we haven't hardcode any configuration which is entirely handled by Spring Cloud Stream. When we require we can change the message broker by changing some properties in application.properties file.
There are a few more properties, I'm leaving here for your ready reference which save someone's some time:
To customize the delivery tag of the message:
To enable batching in application: