Send messages using Spring Cloud Stream’s StreamBridge

In the previous tutorial, I introduced you to Spring Cloud Stream with a binding mechanism that easily allows us to send and receive messages to any message broker automatically. If you want to send messages to a message broker manually, you can use Spring Cloud Stream’s StreamBridge class by ingesting its bean and using its send() method to send messages. For example:

There are two parameters in the send() method, as you can see above:

  • The first parameter is the logical binding name that you configure in the application.yaml file, as I shared with you in the previous article:

With the above configuration, Spring will automatically bind the “logical binding name” that you passed in the send() method to the exchange in RabbitMQ, allowing Spring to send messages!

  • The second parameter is the message you want to send.

The result when I run my example is as follows:

If you pass the binding name incorrectly, for example:

then an exchange will also be initialized in RabbitMQ:

but because there’s no queue information, any messages you send will be lost!

See more in the video here:

Add Comment