use of org.sdase.commons.server.kafka.config.ListenerConfig in project sda-dropwizard-commons by SDA-SE.
the class KafkaBundle method createMessageListener.
/**
* Creates a number of message listeners with the parameters given in the {@link
* MessageListenerRegistration}.
*
* <p>The depricated fields of the {@link ListenerConfig} from {@link MessageListenerRegistration}
* are not considered here
*
* @param registration the registration configuration
* @param <K> the key object type
* @param <V> the value object type
* @return the newly registered message listeners
*/
public <K, V> List<MessageListener<K, V>> createMessageListener(MessageListenerRegistration<K, V> registration) {
if (kafkaConfiguration.isDisabled()) {
return Collections.emptyList();
}
checkInit();
if (registration.isCheckTopicConfiguration()) {
ComparisonResult comparisonResult = checkTopics(registration.getTopics());
if (!comparisonResult.ok()) {
throw new MismatchedTopicConfigException(comparisonResult);
}
}
ListenerConfig listenerConfig = registration.getListenerConfig();
if (listenerConfig == null && registration.getListenerConfigName() != null) {
listenerConfig = kafkaConfiguration.getListenerConfig().get(registration.getListenerConfigName());
if (listenerConfig == null) {
throw new ConfigurationException(String.format("Listener config with name '%s' cannot be found within the current configuration.", registration.getListenerConfigName()));
}
}
if (listenerConfig == null) {
throw new ConfigurationException("No valid listener config given within the MessageHandlerRegistration");
}
if (registration.getStrategy() == null) {
throw new IllegalStateException("A strategy is mandatory for message listeners.");
}
List<MessageListener<K, V>> listener = new ArrayList<>(listenerConfig.getInstances());
for (int i = 0; i < listenerConfig.getInstances(); i++) {
registration.getStrategy().init(topicConsumerHistogram);
MessageListener<K, V> instance = new MessageListener<>(registration.getTopicsNames(), createConsumer(registration, i), listenerConfig, registration.getStrategy());
listener.add(instance);
Thread t = new Thread(instance);
t.start();
threadedMessageListeners.add(new ThreadedMessageListener<>(instance, t));
}
messageListeners.addAll(listener);
return listener;
}
use of org.sdase.commons.server.kafka.config.ListenerConfig in project sda-dropwizard-commons by SDA-SE.
the class AutocommitStrategyTest method setupListener.
private void setupListener(int topicWaitTime) {
ListenerConfig lc = ListenerConfig.builder().withTopicMissingRetryMs(topicWaitTime).build(1);
final AutocommitMLS<String, String> strategy = new AutocommitMLS<>(handler, errorHandler);
strategy.init(histogram);
MessageListenerRegistration<String, String> registration = MessageListenerRegistration.builder().withListenerConfig(lc).forTopics(Arrays.asList(TOPICS)).withDefaultConsumer().withListenerStrategy(strategy).build();
listener = new MessageListener<>(registration.getTopicsNames(), consumer, lc, strategy);
}
Aggregations