A single producer produces messages to a single partition sequentially.
For a single client instance (a producer object created with the new
command), the total number of connections established between it and all servers ranges from one to n (n refers to the number of brokers).
Each Java producer manages TCP connections as follows:
Sender
thread will be initiated when a KafkaProducer
instance is created to establish TCP connections to all brokers in bootstrap.servers
.KafkaProducer
instance is updated, TCP connections to all brokers in the cluster will be established again.connections.max.idle.ms
parameter on the producer to a value above 0, TCP connections established in step 1 will be closed automatically. The parameter value is 9 minutes by default; that is, if no requests are sent through a TCP connection in 9 minutes, Kafka will automatically close the connection. If you set the parameter to -1, TCP connections established in step 1 cannot be closed and will become "zombie" connections.After sending a message, most clients will return a Callback
or Future
. A successful callback indicates that the message is successfully sent.
You can also check whether a message is successfully sent in the console by the following methods:
You can print the partition information returned by the send
method to check whether the message is successfully sent:
Future<RecordMetadata> future = producer.send(new ProducerRecord<>(topic, messageKey, messageStr));
RecordMetadata recordMetadata = future.get();
log.info("partition: {}", recordMetadata.partition());
log.info("offset: {}", recordMetadata.offset());
If the partition and offset information can be printed out, the currently sent message has been correctly saved on the server. At this time, you can use the message query tool to query the information of the relevant offset.
If the partition and offset information cannot be printed out, the message has not been saved on the server, and the client needs to retry.
Was this page helpful?