spring-cloud-stream icon indicating copy to clipboard operation
spring-cloud-stream copied to clipboard

Consider exposing auto-start flag for binders

Open jimmi-skaria opened this issue 8 years ago • 5 comments

There are times when we need start the apps in the idle mode. See this SO discussion for one of the cases.

jimmi-skaria avatar Nov 21 '18 21:11 jimmi-skaria

@jimmi-skaria you have asked this question several times on SO and it was answered. There is no need to double post it as a question. I did suggest in one of the replies that we may consider doing something, but that means it has to be a request for feature not a question as it confuses the community since we do not use GitHub for answering questions.

olegz avatar Nov 23 '18 07:11 olegz

I have modified bot the title and the description

olegz avatar Nov 23 '18 07:11 olegz

Thanks @olegz

jimmi-skaria avatar Nov 23 '18 17:11 jimmi-skaria

This seems to work for now for the kafka binder:

package com.paychex.kafkademo.config;

import java.util.Collection;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.common.TopicPartition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.binder.BinderFactory;
import org.springframework.cloud.stream.binder.kafka.KafkaBindingRebalanceListener;
import org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder;
import org.springframework.cloud.stream.endpoint.BindingsEndpoint;
import org.springframework.cloud.stream.endpoint.BindingsEndpoint.State;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.MessageChannel;

@Configuration
public class KafkaConfiguration {


    @Autowired
    private BinderFactory binderFactory;

    @Autowired
    private BindingsEndpoint bindingsEndpoint;

    @Bean
    public KafkaBindingRebalanceListener kafkaBindingRebalanceListener(
        @Value("${autoStartupKafkaListeners:true}")
            boolean autoStartup) {
        return new KafkaBindingRebalanceListener() {
            Set<String> pausedBindings = new HashSet<>();
            @Override
            public void onPartitionsAssigned(String bindingName, Consumer<?, ?> consumer, Collection<TopicPartition> partitions, boolean initial) {
                KafkaMessageChannelBinder binder = (KafkaMessageChannelBinder) binderFactory.getBinder("kafka", MessageChannel.class);
                boolean autoStartupBinding = Boolean.parseBoolean(binder.getExtendedConsumerProperties(bindingName)
                                                                        .getConfiguration()
                                                                        .get("autoStartup"));
                if (initial && !autoStartupBinding) {
                    //had to do this initially otherwise a message can make its way through
                    consumer.pause(partitions);
                }

                Optional<String> topicName = partitions.stream().map(partition -> partition.topic()).findFirst();
                if (!pausedBindings.contains(bindingName) && (!autoStartup || (!autoStartupBinding && topicName.isPresent()))) {
                    bindingsEndpoint.changeState(topicName.get(), State.PAUSED);
                    pausedBindings.add(bindingName);
                }
            }
        };
    }
}

Also not sure if this should be a bug or not, but the BindingsEndpoint will only allow you to pause the first binding with that name. So for example if you had a kafka stream listener and a kafka listener on the same topic it kept hitting the stream listener one and saying that it wasn't pausable basically

mtj8033 avatar Dec 18 '18 19:12 mtj8033

Related to https://github.com/spring-cloud/spring-cloud-stream/issues/1568

olegz avatar Jan 22 '19 07:01 olegz