我已经阅读了 spring-kafka 文档、我找到的示例和一半的 stackoverflow,但我不明白 @EmbeddedKafka 应该由谁工作。特别是对于集成测试。
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@EnableKafka
@EmbeddedKafka(controlledShutdown = true
// brokerProperties = {"log.dir=${kafka.broker.logs-dir}",
// "listeners=PLAINTEXT://localhost:${kafka.broker.port:3333}",
// "auto.create.topics.enable=${kafka.broker.topics-enable:true}"}
)
我必须在测试 application.properties 中添加以下内容:
….kafka.bootstrap-servers=${spring.embedded.kafka.brokers}
有了这一切,当测试它执行时,它抱怨几行关于 kafka 不可访问:
[Producer clientId=producer-2] Connection to node 0 could not be established. Broker may not be available.
但是在屏幕的一半之后,测试成功并完成了最后一条令人不快的消息:
2019-06-17 12:31:54.438 WARN [testing,,,] 32448 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase : The web application [testing] appears to have started a thread named [kafka-producer-network-thread | producer-3] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
org.apache.kafka.common.network.Selector.select(Selector.java:674)
org.apache.kafka.common.network.Selector.poll(Selector.java:396)
org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:460)
org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:239)
org.apache.kafka.clients.producer.internals.Sender.run(Sender.java:163)
java.lang.Thread.run(Thread.java:748)
问题:1/为什么在每个方法开始时都会有关于代理不可用的消息,即使上下文没有脏并重新初始化?2/为什么会出现带有未关闭线程的丑陋消息?怎么了?