Consider exposing auto-start flag for binders
There are times when we need start the apps in the idle mode. See this SO discussion for one of the cases.
@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.
I have modified bot the title and the description
Thanks @olegz
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
Related to https://github.com/spring-cloud/spring-cloud-stream/issues/1568