0

I have a project using spring-boot-cloud and apache-kafka, I have a list of integration test covering the topology logic, thanks to EmbeddedBroker.

I recently discovered that there are many noise in the log when running these tests.

e.g. [Producer clientId=producer-2] Connection to node 0 (localhost/127.0.0.1:63267) could not be established. Broker may not be available.

After some trial and error it appears that these were the producers created by the spring-cloud-stream bindings. Somehow with @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) on the class level they do not appear to be cleaned up after each test.

Thus I figured if I can get access to the producer factory I can then manually clean them up inside the @AfterEach clause of my test class. I've tried to autowire KafkaTemplate but it didn't help. I don't know how I can access the producer factory since it's created implicitly by the framework.

Please be noted that these do not appear to affect the test result since they only show up at the end of the test phase.

Thanks in advance!

AttitudeL
  • 189
  • 2
  • 16

1 Answers1

1

You can add a ProducerMessageHandlerCustomizer bean and get a reference to the producer factory that way.

@Bean
ProducerMessageHandlerCustomizer<KafkaProducerMessageHandler> cust() {
    return (handler, dest) -> {
        this.pfMap.put(dest, handler.getKafkaTemplate().getProducerFactory());
    }
}

Store the PF in a map in the test case, then reset() it when you want to close the producer(s).

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Oh thanks so much for the quick answer. Do I need to add this piece of code in the production in order to collect the producer factory reference? – AttitudeL Mar 11 '21 at 22:16
  • No; you should be able to just add it as a bean in a supplemental `@Configuration` class in your test cases. By the way, there was a recent patch on master to close the producers when the bindings are stopped. https://github.com/spring-cloud/spring-cloud-stream-binder-kafka/commit/e2eca34e4b8f7fc0dab2f19a19094cce0dfda2e4 – Gary Russell Mar 11 '21 at 22:21
  • Ah I see, thanks so much for the info! Much appreciated. – AttitudeL Mar 11 '21 at 22:24