Try PubSub+

New Spring Cloud Stream Dynamic Topic Publish Sample!

marcmarc Member, Administrator, Moderator, Employee Posts: 469 admin

Heads up for everyone using Spring Cloud Stream I created a new sample that shows how to publish to dynamic topics! I'll be creating a tutorial or blog at some point to add more explanation, but for now you can check out the code on github:

Note that this is already setup for v1.1.1 of solace-spring-cloud which is currently waiting for mvn central to sync (It's taking waay longer than I expected!). If you have a compile error just change that to v1.1.0 while we wait.


  • kovkov Member, Employee Posts: 16 Solace Employee

    That's pretty interesting. I think I can use this nicely with my project. It provides a reflection-based implementation of topic serialization via a template string like:


    I use it in Publishers to dynamically construct a topic string by passing in an object, and in Subscribers construct an intelligent subscription string based on a map of queries. Separates out the dynamic topic serialization just like we normally do with payload serialization; guessing it would look something like this (not a Spring guru)?

        TopicStrategy<Order> strategy;
        public Consumer<Order> functionUsingStreamBridge(StreamBridge streamBridge) {
            return order -> {
                String topic = strategy.makeTopic(order);
      "Processing order: " + order);
                streamBridge.send(topic, order);
  • marcmarc Member, Administrator, Moderator, Employee Posts: 469 admin

    Hi @kov,
    Yep, seems like you can use this for your use case. I need to write a blog on this at some point, but depending on the level of performance you require you might consider using the target dest header option shown by functionUsingTargetDestHeader instead as with that option the dynamic part is then essentially handled by the Solace binder instead of within Spring. This approach has less overhead by avoiding the creation of a spring integration channel, caching it, etc. for each publish. This does of course does have a few downsides such as not being supported by all cloud stream binders and also not having the Spring Channel available for monitoring/metrics

Sign In or Register to comment.