New Spring Cloud Stream Dynamic Topic Publish Sample!

Options
marc
marc Member, Administrator, Moderator, Employee Posts: 920 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.

Comments

  • [Deleted User]
    Options

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

    Stores/{state}/{storeNumber}/Order/{orderId}/{action}

    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)?

        @Autowired
        TopicStrategy<Order> strategy;
        @Bean
        public Consumer<Order> functionUsingStreamBridge(StreamBridge streamBridge) {
            return order -> {
                String topic = strategy.makeTopic(order);
                log.info("Processing order: " + order);
                streamBridge.send(topic, order);
            };
        }
    
  • marc
    marc Member, Administrator, Moderator, Employee Posts: 920 admin
    Options

    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
    -Marc