Dynamic Kafka Topics in Quarkus

This post describes sending Kafka events to dynamic topics in Quarkus.

Previously I suggested using the KafkaProducer, but it is pretty much obsolete since Quarkus 2, and all metadata is ignored. I ended up with serialization issues.

The solution is to use a standard Emitter and add metadata to the message. For example, this class uses a channel called reflector and routes one or more dynamic topics:

The reflector channel is initialized in the application.properties resource:

mp.messaging.outgoing.reflector.cloud-events-source=reflectorservice
mp.messaging.outgoing.reflector.connector=smallrye-kafka
mp.messaging.outgoing.reflector.value.serializer=org.apache.kafka.common.serialization.StringSerializer
mp.messaging.outgoing.reflector.cloud-events-mode=structured
mp.messaging.outgoing.reflector.cloud-events-type=reflector

The emit function simply creates metadata for the specific topic, then creates a message, adds the metadata, and sends it via the emitter.

I hope this post may be useful.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s