# New Spring Cloud Stream Dynamic Topic Publish Sample!

**URL:** <https://community.solace.com/t/new-spring-cloud-stream-dynamic-topic-publish-sample/347>\
**Category:** Connectors & Integrations\
**Tags:** spring, java, spring-cloud-stream\
**Created:** [July 31, 2020, 5:41pm UTC](https://community.solace.com/t/new-spring-cloud-stream-dynamic-topic-publish-sample/347 "2020-07-31T17:41:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marc](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/marc/32/2191_2.png) [@marc](https://community.solace.com/u/marc)\
**Post date:** [July 31, 2020, 5:41pm UTC](https://community.solace.com/t/new-spring-cloud-stream-dynamic-topic-publish-sample/347/1 "2020-07-31T17:41:24Z")

</div>

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:

- [DynamicDestinationProcessor.java](https://github.com/SolaceSamples/solace-samples-spring/blob/master/cloud-stream-dynamic-destination-processor/src/main/java/com/solace/samples/spring/scs/DynamicDestinationProcessor.java)
- [application.yml](https://github.com/SolaceSamples/solace-samples-spring/blob/master/cloud-stream-dynamic-destination-processor/src/main/resources/application.yml)

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/solace/original/1X/1248f38eea018157e66cc024e8284f3331a54f94.png) [@system](https://community.solace.com/u/system)\
**Post date:** [August 9, 2020, 12:12am UTC](https://community.solace.com/t/new-spring-cloud-stream-dynamic-topic-publish-sample/347/2 "2020-08-09T00:12:29Z")

</div>

That’s pretty interesting. I think I can use this nicely with my [GitHub - solacese/topic-serialization: Example library demonstrating a serialization layer abstraction for hierarchical topics with complex filtering.](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)?

```auto
	@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);
		};
	}

```

---

<div class="post-metadata">

**Author:** ![marc](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/marc/32/2191_2.png) [@marc](https://community.solace.com/u/marc)\
**Post date:** [August 10, 2020, 3:42pm UTC](https://community.solace.com/t/new-spring-cloud-stream-dynamic-topic-publish-sample/347/3 "2020-08-10T15:42:57Z")

</div>

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](https://github.com/SolaceSamples/solace-samples-spring/blob/master/cloud-stream-dynamic-destination-processor/src/main/java/com/solace/samples/spring/scs/DynamicDestinationProcessor.java#L56) 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
