{"id":13416,"date":"2025-09-10T12:44:46","date_gmt":"2025-09-10T12:44:46","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=13416"},"modified":"2025-09-10T12:45:22","modified_gmt":"2025-09-10T12:45:22","slug":"streambridge-and-transactions-in-spring-cloud-stream","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/cloud\/streambridge-and-transactions-in-spring-cloud-stream","title":{"rendered":"Spring Cloud Stream: StreamBridge and Transaction"},"content":{"rendered":"<p>When using Spring Cloud Stream with RabbitMQ, and trying to send multiple messages in a transactional way (e.g. via StreamBridge), it is important to ensure the entire flow is transactional \u2014 including message consumption and any downstream message publication.<\/p>\n<h3>Step 1: Define function<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">@Component(\"myfunction\")\r\npublic class MyFunction implements Consumer&lt;String&gt; {\r\n\r\n    private final StreamBridge streamBridge;\r\n    public MyFunction(StreamBridge streamBridge) {\r\n        this.streamBridge = streamBridge;\r\n    }\r\n\r\n    @Override\r\n    @Transactional\r\n    public void accept(String message) {\r\n        streamBridge.send(\"myfunction-out-0\", message);\r\n        if (message.equals(\"error\")) {\r\n            throw new RuntimeException(\"Simulated failure\");\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<h3>Step 2: Configuration, application.yml<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nspring:\r\n  cloud:\r\n    function:\r\n      definition: myfunction\r\n\r\n    stream:\r\n      bindings:\r\n        myfunction-in-0:\r\n          destination: test.request\r\n          group: test-group\r\n        myfunction-out-0:\r\n          destination: test.response\r\n\r\n      rabbit:\r\n        bindings:\r\n          myfunction-in:\r\n            consumer:\r\n              transacted: true\r\n          myfunction-out:\r\n            producer:\r\n              transacted: true\r\n\r\n  rabbitmq:\r\n    host: <your-host>\r\n    port: <your-port>\r\n    username: <your-username>\r\n    password: <your-password>\r\n<\/pre>\n<p><strong>Note: <\/strong><br \/>\nUse myfunction-in-0 and myfunction-out-0 in stream.bindings<br \/>\nUse myfunction-in and myfunction-out in rabbit.bindings<\/p>\n<h3>Step 3: Transaction Manager Bean<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\n@Configuration\r\npublic class TransactionConfig {\r\n    @Bean\r\n    public RabbitTransactionManager rabbitTransactionManager(ConnectionFactory connectionFactory) {\r\n        return new RabbitTransactionManager(connectionFactory);\r\n    }\r\n}\r\n<\/pre>\n<p>The -in-0 style works for stream.bindings, but not for rabbit.bindings, which expects the logical binding name (myfunction-in)<\/p>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/cloud-maturity-model\" target=\"_blank\">Cloud Maturity Model<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>When using Spring Cloud Stream with RabbitMQ, and trying to send multiple messages in a transactional way (e.g. via StreamBridge), it is important to ensure the entire flow is transactional \u2014 including message consumption and any downstream message publication. Step 1: Define function @Component(&#8220;myfunction&#8221;) public class MyFunction implements Consumer&lt;String&gt; { private final StreamBridge streamBridge; public [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13417,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-13416","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13416"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=13416"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13416\/revisions"}],"predecessor-version":[{"id":13419,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13416\/revisions\/13419"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/13417"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=13416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=13416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=13416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}