--- name: eventhub-kafka-producer description: Create or fix a Kafka producer that publishes to Azure Event Hubs (Kafka endpoint, port 9093, SASL_SSL). Use when adding Event Hubs publishing, or when you see "Failed to construct kafka producer", auth or timeout errors. --- # Event Hubs Kafka producer ## Required settings - `bootstrap.servers`: `.servicebus.windows.net:9093` - `security.protocol`: `SASL_SSL` - `sasl.mechanism`: `PLAIN` (connection string) or `OAUTHBEARER` (Entra ID / managed identity - preferred) - `sasl.jaas.config` for PLAIN: `org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="";` - `acks=all`, `enable.idempotence=true`, `compression.type=lz4` or `snappy` - `request.timeout.ms=60000`, `delivery.timeout.ms=120000`, `max.request.size<=1048576` (1 MB Standard tier) ## Rules 1. Create ONE producer per application (singleton bean) and reuse it. Never create a producer per message. 2. The connection string is loaded from a secret store / env var. Never hard-code it. Watch for shell `$` expansion of `$ConnectionString` - quote it correctly. 3. A SAS key scoped with `EntityPath=` works only for that hub; the topic name must equal the event hub name. 4. Use a meaningful message key (for example the business id) to keep per-entity ordering within a partition. 5. Handle the send callback: log failures with the key and topic, retry transient errors, never log the payload if it contains PII. 6. Close the producer on application shutdown (`@PreDestroy`). ## Troubleshooting checklist - DNS: namespace resolves to the private endpoint IP when public access is disabled. - Network: port 9093 open from the app's egress IP to the private endpoint. - Auth: correct policy (Send rights), correct hub, JAAS string quoting. - Verify arrival: Event Hubs metrics -> Incoming Messages (not "Requests").