Example usage for org.springframework.kafka.config MethodKafkaListenerEndpoint setBean

List of usage examples for org.springframework.kafka.config MethodKafkaListenerEndpoint setBean

Introduction

In this page you can find the example usage for org.springframework.kafka.config MethodKafkaListenerEndpoint setBean.

Prototype

public void setBean(Object bean) 

Source Link

Document

Set the object instance that should manage this endpoint.

Usage

From source file:org.springframework.kafka.annotation.KafkaListenerAnnotationBeanPostProcessor.java

protected void processListener(MethodKafkaListenerEndpoint<?, ?> endpoint, KafkaListener kafkaListener,
        Object bean, Object adminTarget, String beanName) {
    endpoint.setBean(bean);
    endpoint.setMessageHandlerMethodFactory(this.messageHandlerMethodFactory);
    endpoint.setId(getEndpointId(kafkaListener));
    endpoint.setTopicPartitions(resolveTopicPartitions(kafkaListener));
    endpoint.setTopics(resolveTopics(kafkaListener));
    endpoint.setTopicPattern(resolvePattern(kafkaListener));
    String group = kafkaListener.group();
    if (StringUtils.hasText(group)) {
        Object resolvedGroup = resolveExpression(group);
        if (resolvedGroup instanceof String) {
            endpoint.setGroup((String) resolvedGroup);
        }/*w  ww. ja v  a  2 s  .c  o m*/
    }

    KafkaListenerContainerFactory<?> factory = null;
    String containerFactoryBeanName = resolve(kafkaListener.containerFactory());
    if (StringUtils.hasText(containerFactoryBeanName)) {
        Assert.state(this.beanFactory != null,
                "BeanFactory must be set to obtain container factory by bean name");
        try {
            factory = this.beanFactory.getBean(containerFactoryBeanName, KafkaListenerContainerFactory.class);
        } catch (NoSuchBeanDefinitionException ex) {
            throw new BeanInitializationException(
                    "Could not register Kafka listener endpoint on [" + adminTarget + "] for bean " + beanName
                            + ", no " + KafkaListenerContainerFactory.class.getSimpleName() + " with id '"
                            + containerFactoryBeanName + "' was found in the application context",
                    ex);
        }
    }

    this.registrar.registerEndpoint(endpoint, factory);
}