Closing a Kafka connection

独自空忆成欢 提交于 2019-12-11 09:35:48

问题


I have an application that should send a finite number of messages to Kafka and then quit. For some reason, the Kafka connection stays up even if I close the producer. My implementation (in Scala) is more or less

object Kafka {
  private val props = new Properties()

  props.put("compression.codec", DefaultCompressionCodec.codec.toString)
  props.put("producer.type", "sync")
  props.put("metadata.broker.list", "localhost:9092")
  props.put("batch.num.messages", "200")
  props.put("message.send.max.retries", "3")
  props.put("request.required.acks", "-1")
  props.put("client.id", "myclient")

  private val producer = new Producer[Array[Byte], Array[Byte]](new ProducerConfig(props))
  private def encode(msg: Message) = new KeyedMessage("topic", msg.id.getBytes, write(msg).getBytes)

  def send(msg: Message) = Try(producer.send(encode(msg)))
  def close() = producer.close()
}

Here Message is a simple case class, and how I convert it to byte array is not really relevant.

The messages do arrive, but when I eventually call Kafka.close(), the application does not exit, and the connection does not seem to be released.

Is there a way to explicitly ask Kafka to terminate the connection?


回答1:


def close() = producer.close()

This creates a function called "close" that calls producer.close() I see no evidence that your code actually closes the producer.

You need to just call: producer.close



来源:https://stackoverflow.com/questions/26776659/closing-a-kafka-connection

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