I am using Spring Cloud Stream Kafka. I have StreamListeners for topics that are predefined. My new requirement is to create and stop StreamListeners runtime for a user defined topic name. So from user interface user will determine and update which topic names will be listened and with topic listeners (StreamListeners) will be stopped. Is there any way to have flexible StreamListeners runtime? I tried to use BinderAwareChannelResolver, but while setting different binders to different bindings I get UnknownBinder Configuration Error. I could not find a detailed example covering my requirements using BinderAwareChannelResolver.
@Autowired
private SubscribableChannelBindingTargetFactory bindingTargetFactory;
@Autowired
private BindingService bindingService;
@Autowired
BinderFactory binderFactory;
BindingServiceProperties properties = bindingService.getBindingServiceProperties();
properties.getConsumerProperties(channelName ).setBatchMode(true);
String binderConfigurationName = properties.getBinder(channelName);
Binder<SubscribableChannel, ConsumerProperties, ?> binder = (Binder<SubscribableChannel, ConsumerProperties, ?>) binderFactory.getBinder(binderConfigurationName, channel.getClass());
copyExtendedConsumerProperties(binder, channelName);
bindingService.bindConsumer(channel, channelName);
channel.subscribe(new DynamicMessageHandler());
private void copyExtendedConsumerProperties(Binder binder, String channelName) {
KafkaConsumerProperties extension = (KafkaConsumerProperties) ((ExtendedPropertiesBinder) binder).getExtendedConsumerProperties(channelName);
extension.getConfiguration().put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, PersonDeserializer.class.getName());
extension.getConfiguration().put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, "3000");
extension.getConfiguration().put(ConsumerConfig.FETCH_MIN_BYTES_CONFIG, "900000");
extension.getConfiguration().put(ConsumerConfig.FETCH_MAX_WAIT_MS_CONFIG, "5000");
extension.getConfiguration().put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
}
public class DynamicMessageHandler implements MessageHandler {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
List<Person> personList = (List<Person>) message.getPayload();
System.out.println(personList.size());
}
}