I'm creating the cloud streams and consumers for it dynamically can't get consumer defined properly as information about type is lost.
In the example below consumer defined with @Bean annotation works fine, it receive message with all headers and payload. Consumer defined dynamically receive byte[] as input because when it created information about generic type is lost as method registerSingleton(String beanName, Object singletonObject) receive object as parameter.
I've tried to manually deserialize the message, but spring send serialized payload, not message with payload, to dynamically created consumer so I can't access headers. This mean that I'm receiving serialized SitePayload in the dynamic consumer instead of serialized Message.
Any advise on how to create consumer dynamically so it can access message headers?
public class ConsumersCreator implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
for (int i = 0; i < 5; i++) {
beanFactory.registerSingleton("consumer" + i, createConsumer(i));
}
}
private Consumer<Message<SitePayload>> createConsumer(int id) {
return message -> log.info("Consumer id: {} message received: {}", id, message);
}
@Bean
public Consumer<Message<SitePayload>> consumer() {
return message -> log.error("Message received: {}", message);
}
}