Search in sources :

Example 1 with ConfigurationException

use of org.sdase.commons.server.kafka.exception.ConfigurationException 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;
}
Also used : MessageListener(org.sdase.commons.server.kafka.consumer.MessageListener) ArrayList(java.util.ArrayList) MismatchedTopicConfigException(org.sdase.commons.server.kafka.topicana.MismatchedTopicConfigException) ListenerConfig(org.sdase.commons.server.kafka.config.ListenerConfig) ConfigurationException(org.sdase.commons.server.kafka.exception.ConfigurationException) ComparisonResult(org.sdase.commons.server.kafka.topicana.ComparisonResult)

Aggregations

ArrayList (java.util.ArrayList)1 ListenerConfig (org.sdase.commons.server.kafka.config.ListenerConfig)1 MessageListener (org.sdase.commons.server.kafka.consumer.MessageListener)1 ConfigurationException (org.sdase.commons.server.kafka.exception.ConfigurationException)1 ComparisonResult (org.sdase.commons.server.kafka.topicana.ComparisonResult)1 MismatchedTopicConfigException (org.sdase.commons.server.kafka.topicana.MismatchedTopicConfigException)1