Can Kafka topic names be parameterised?

374 Views Asked by At

I've recently started experimenting with AsyncAPI as part of a project at work. My task is to see if we can use AsyncAPI to generate code to send test objects to our existing Kafka queues. I'm not familiar with Kafka.

I'm starting with the API specification at AsyncAPI playground, and trying to generate some Java Spring code from it. However, I've run into an issue in that, in the playground specification, channel names (which will correspond to Kafka topics) are parameterised, e.g. as:

smartylighting/streetlights/1/0/event/{streetlightId}/lighting/measured

Is it possible to parameterise the Kafka topic in this way, such that the generated service has the following annotation?

@KafkaListener(topics = "smartylighting.streetlights.1.0.action.{streetlightId}.lighting.measured", groupId = "myGroupId")

Or does this specification (which again, I'm just using to learn AsyncAPI) need to be changed so that topic names are invariant, and instead provide the street light ID as payload data?

Thanks in advance.

2

There are 2 best solutions below

1
On

I am not familiar with Async API, but with Spring for Apache Kafka, topic names can contain property placeholders

topics = "foo.${some.property}.bar"

Where some.property is available in the Spring environment e.g. -Dsome.property command line or a spring Boot application.properties or yaml file.

You can also perform more advanced configuration using the Spring Expression Language.

topics = "#{someBean.somePropeerty}"
0
On

This is currently one of missed features of the java-spring template.

parameters for topics are not supported

Please consider creation of a new issue with detail description of your case.