Spring Kafka Without spring boot consumer not consuming messages

ぐ巨炮叔叔 提交于 2020-05-17 06:22:26

问题


Consumer using Spring’s JavaConfig class as follows:

@Configuration
@EnableKafka
public class KafkaConfig {

    public static final String TOPIC = "test-1";
    private String bootstrapServers = "localhost:9092";

    @Bean
    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }

    @Bean
    public ConsumerFactory<String, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }

    @Bean
    public Map<String, Object> consumerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "group");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        return props;
    }

Kafka topic listener using @KafkaListener annotation as follows:

@Component
public class MessageListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(MessageListener.class);

    @KafkaListener(topics = KafkaConfig.TOPIC)
    public void handle(ConsumerRecord<?, ?> cr) {
        LOGGER.info("Message: "+cr.key()+"="+cr.value());
    }
}

And my pom includes dependency:

 <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

Now When i package to war and deploy to tomcat, It doesn't show any errors not even in debug mode just deploys war and nothing.

Please help me understand if i am missing some configuration that triggers the kafkalistner.

Thanks Gary I addedd the context.xml and web.xml but i get bean errors mentioned in below comments

Web.XML

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring Web MVC Application</display-name>


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

Context.XML

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/util
     http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <context:component-scan base-package="com.mkyong.common.controller.*" />
    <context:annotation-config></context:annotation-config>

</beans>

回答1:


When deploying in a war, you need to bootstrap the application context via the web.xml. See https://docs.spring.io/spring/docs/5.2.5.RELEASE/spring-framework-reference/web.html#web-integration-common

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

...
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/context.xml</param-value>
</context-param>

In the context.xml add <context:component-scan/>.

See https://docs.spring.io/spring/docs/5.2.5.RELEASE/spring-framework-reference/core.html#beans-scanning-autodetection




回答2:


Issue is that spring by default uses XmlWebApplicationContext, so i had to explicitly tell Spring to create application context for web applications by using java clases as input for bean definitions instead of xml files.

Below is the change done to context.xml. This Worked for me.

`

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.test.common.controller.KafkaConfig</param-value>
</context-param>`


来源:https://stackoverflow.com/questions/61026659/spring-kafka-without-spring-boot-consumer-not-consuming-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!