io.microsphere.spring.test.kafka.KafkaTestConfiguration Maven / Gradle / Ivy
package io.microsphere.spring.test.kafka;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.GenericMessageListenerContainer;
import org.springframework.kafka.listener.KafkaMessageListenerContainer;
import org.springframework.kafka.support.LoggingProducerListener;
import org.springframework.kafka.support.ProducerListener;
import org.springframework.kafka.support.converter.RecordMessageConverter;
import org.springframework.kafka.test.EmbeddedKafkaBroker;
import org.springframework.kafka.test.utils.ContainerTestUtils;
import org.springframework.kafka.test.utils.KafkaTestUtils;
import java.util.Map;
import java.util.StringJoiner;
/**
* Kafka Test configuration
*
* @param Key type
* @param Value type
* @author Mercy
* @since 1.0.0
*/
public class KafkaTestConfiguration {
private final RecordMessageConverter messageConverter;
private final ProducerListener kafkaProducerListener;
public KafkaTestConfiguration(ObjectProvider messageConverter,
ObjectProvider> kafkaProducerListener) {
this.messageConverter = messageConverter.getIfAvailable();
this.kafkaProducerListener = kafkaProducerListener.getIfAvailable(LoggingProducerListener::new);
}
@Bean
public KafkaTemplate kafkaTemplate(EmbeddedKafkaBroker embeddedKafka) {
Map producerProps = KafkaTestUtils.producerProps(embeddedKafka);
ProducerFactory pf = new DefaultKafkaProducerFactory<>(producerProps);
KafkaTemplate kafkaTemplate = new KafkaTemplate(pf);
if (messageConverter != null) {
kafkaTemplate.setMessageConverter(messageConverter);
}
kafkaTemplate.setProducerListener(kafkaProducerListener);
return kafkaTemplate;
}
@Bean
public GenericMessageListenerContainer messageListenerContainer(EmbeddedKafkaBroker embeddedKafka,
CompositeMessageListener compositeMessageListener) {
String[] topics = embeddedKafka.getTopics().toArray(new String[0]);
StringJoiner nameBuilder = new StringJoiner("-");
nameBuilder.add("group");
for (String topic : topics) {
nameBuilder.add(topic);
}
String beanName = nameBuilder.toString();
Map consumerProps = KafkaTestUtils.consumerProps(beanName, "false", embeddedKafka);
DefaultKafkaConsumerFactory cf = new DefaultKafkaConsumerFactory<>(consumerProps);
ContainerProperties containerProperties = new ContainerProperties(topics);
KafkaMessageListenerContainer container = new KafkaMessageListenerContainer<>(cf, containerProperties);
container.setBeanName(beanName);
container.setupMessageListener(compositeMessageListener);
container.start();
ContainerTestUtils.waitForAssignment(container, embeddedKafka.getPartitionsPerTopic());
return container;
}
@Bean
@Primary
// @Autowired(required = false)
public CompositeMessageListener compositeMessageListener() {
CompositeMessageListener compositeMessageListener = new CompositeMessageListener<>();
// compositeMessageListener.addMessageListeners(messageListeners);
return compositeMessageListener;
}
}