问题
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