1

As you know, there are two kinds of ways to send message - synchronous and asynchronous.

When we code with synchronous mode, the code looks the following

producer.send(new ProducerRecord<Long, Event>(topicName, event)).get();

Reading from Kafka document https://kafka.apache.org/10/javadoc/org/apache/kafka/clients/producer/KafkaProducer.html#send-org.apache.kafka.clients.producer.ProducerRecord-, it defines as following:

public java.util.concurrent.Future<RecordMetadata> send(ProducerRecord<K,V> record)

Asynchronously send a record to a topic. Equivalent to send(record, null). See send(ProducerRecord, Callback) for details.

Specified by:
    send in interface Producer<K,V> 

So, basically send() method return a Futher, then once I use .get() for this future, it becomes synchronous behavour.

My question is, from definition, I don't see an exception definition, how to capture exception under synchronous send()? Seem there doesn't define any exception. Could anybody help clarify for that?

Joe
  • 623
  • 7
  • 16

1 Answers1

0

If the record being sent is somehow invalid and can be rejected before communicating with Kafka cluster, then .send will throw a KafkaException immediately (e.g. if the record is too large you get RecordTooLargeException).

If there are issues on the cluster-side, such as partition not available, you'll get a KafkaException wrapped by .get()'s ExecutionException.

There are also corner cases like throwing BufferExhaustedException or generic Exceptions - you might need to look into the source (especially KafkaProducer's doSend), unfortunately the documentation is not perfect.

See the comment from the source in that method:

            // handling exceptions and record the errors;
            // for API exceptions return them in the future,
            // for other exceptions throw directly
Adam Kotwasinski
  • 4,377
  • 3
  • 17
  • 40