-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kafka): add avro publication support to example
- Loading branch information
Showing
5 changed files
with
87 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...thub/stavshamir/springwolf/example/kafka/configuration/CustomSpringwolfKafkaProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package io.github.stavshamir.springwolf.example.kafka.configuration; | ||
|
||
import io.github.stavshamir.springwolf.producer.SpringwolfKafkaTemplateFromProperties; | ||
import io.github.stavshamir.springwolf.producer.SpringwolfKafkaTemplateProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.kafka.core.DefaultKafkaProducerFactory; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
@Component | ||
@Primary | ||
@RequiredArgsConstructor | ||
public class CustomSpringwolfKafkaProducer implements SpringwolfKafkaTemplateProvider { | ||
private final SpringwolfKafkaTemplateFromProperties springwolfKafkaTemplateFromProperties; | ||
|
||
@Value("${KAFKA_SCHEMA_REGISTRY_URL:http://localhost:8081}") | ||
private String kafkaSchemaRegistryUrl; | ||
|
||
@Override | ||
public boolean isPresent() { | ||
return springwolfKafkaTemplateFromProperties.isPresent(); | ||
} | ||
|
||
@Override | ||
public Optional<KafkaTemplate<Object, Object>> get(String topic) { | ||
return springwolfKafkaTemplateFromProperties | ||
.get(topic) | ||
.map(objectObjectKafkaTemplate -> customize(objectObjectKafkaTemplate, topic)); | ||
} | ||
|
||
private KafkaTemplate<Object, Object> customize(KafkaTemplate<Object, Object> kafkaTemplate, String topic) { | ||
if (topic.contains("avro")) { | ||
Map<String, Object> producerProperties = | ||
new HashMap<>(kafkaTemplate.getProducerFactory().getConfigurationProperties()); | ||
|
||
// configure the producerProperties to use avro serializer/deserializer | ||
producerProperties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); | ||
producerProperties.put("value.serializer", "io.confluent.kafka.serializers.KafkaAvroSerializer"); | ||
producerProperties.put("schema.registry.url", kafkaSchemaRegistryUrl); | ||
|
||
DefaultKafkaProducerFactory<Object, Object> producerFactory = | ||
new DefaultKafkaProducerFactory<>(producerProperties); | ||
return new KafkaTemplate<>(producerFactory); | ||
} | ||
return kafkaTemplate; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters