0

I'm trying to use Spring Cloud Stream with a Kafka binder to consume messages from a topic.

Before I used annotations to create the consumer. Now I have use the functional approach, because the annotation is no more available.

These are the dependecies I used:

implementation("org.springframework.cloud:spring-cloud-stream-binder-kafka:4.0.0")
implementation("org.springframework.cloud:spring-cloud-function-kotlin:4.0.0")

This is the application.yaml

spring:
  cloud:
    function:
      definition: consumeMessage
    stream:
      kafka:
        binder:
          brokers: localhost:9092
          autoAddPartitions: true
      bindings:
        consumeMessage-in-0:
          destination: message
          group: message-group

I tried to use a Bean for the consumer itself, but no messages are revieved:

@Service
class MessageListener() {

    @Bean
    fun consumeMessage(): Consumer<String> = java.util.function.Consumer { payload ->
       println(payload)
    }
}

I'm using Spring Boot 3 as project base.

It is no possible to reveive any message via the listener. Does anybody know how to solve the problem?

Sebastian A.
  • 105
  • 1
  • 8
  • The `@Bean` needs to be in a `@Configuration` class, not a `@Service`. – Gary Russell Dec 21 '22 at 16:21
  • I changed it to a @Configuration, but I still not get any messages. Is it possible to get any debug logs, to see if the Kafka client connects? – Sebastian A. Dec 21 '22 at 17:05
  • Is the `@Configuration` class on Boot's config class path? (i.e. same package as the application, or a sub package)? Yes `--debug` (or `logging.level.root=debug` in app props) will enable debugging of all bean loading, as well as Spring Cloud Stream logging. – Gary Russell Dec 21 '22 at 17:10
  • After some more investigation it seems to be an integration test issue. Is it possible to use a "real" Kafka in test scope and consume real messages? – Sebastian A. Dec 21 '22 at 22:31
  • Yes, of course. – Gary Russell Dec 22 '22 at 12:49

1 Answers1

0

This was only an issue with the integration test. If you are running unit/integration tests, an internal mock/broker is used to perform the tests.

If you want to run the test with a "real" broker, you can use the @Import(KafkaBinderConfiguration::class) annotation.

Sebastian A.
  • 105
  • 1
  • 8