@ThreadSafe @Internal public final class Handover extends Object implements Closeable
interrupting
threads.
This class is used in the Flink Kafka Consumer to hand over data and exceptions between the thread that runs the KafkaConsumer class and the main thread.
The Handover has the notion of "waking up" the producer thread with a Handover.WakeupException
rather than a thread interrupt.
The Handover can also be "closed", signalling from one thread to the other that it the thread has terminated.
Modifier and Type | Class and Description |
---|---|
static class |
Handover.ClosedException
An exception thrown by the Handover in the
pollNext() or produce(ConsumerRecords) method, after the Handover was closed via close() . |
static class |
Handover.WakeupException
A special exception thrown bv the Handover in the
produce(ConsumerRecords) method
when the producer is woken up from a blocking call via wakeupProducer() . |
Constructor and Description |
---|
Handover() |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the handover.
|
org.apache.kafka.clients.consumer.ConsumerRecords<byte[],byte[]> |
pollNext()
Polls the next element from the Handover, possibly blocking until the next element is
available.
|
void |
produce(org.apache.kafka.clients.consumer.ConsumerRecords<byte[],byte[]> element)
Hands over an element from the producer.
|
void |
reportError(Throwable t)
Reports an exception.
|
void |
wakeupProducer()
Wakes the producer thread up.
|
@Nonnull public org.apache.kafka.clients.consumer.ConsumerRecords<byte[],byte[]> pollNext() throws Exception
If an exception was handed in by the producer (reportError(Throwable)
), then that
exception is thrown rather than an element being returned.
Handover.ClosedException
- Thrown if the Handover was closed
.Exception
- Rethrows exceptions from the reportError(Throwable)
method.public void produce(org.apache.kafka.clients.consumer.ConsumerRecords<byte[],byte[]> element) throws InterruptedException, Handover.WakeupException, Handover.ClosedException
This behavior is similar to a "size one" blocking queue.
element
- The next element to hand over.InterruptedException
- Thrown, if the thread is interrupted while blocking for the
Handover to be empty.Handover.WakeupException
- Thrown, if the wakeupProducer()
method is called while
blocking for the Handover to be empty.Handover.ClosedException
- Thrown if the Handover was closed or concurrently being closed.public void reportError(Throwable t)
pollNext()
method, or the next time it calls that method.
After this method has been called, no call to either produce(ConsumerRecords)
or
pollNext()
will ever return regularly any more, but will always return
exceptionally.
If another exception was already reported, this method does nothing.
For the producer, the Handover will appear as if it was closed
.
t
- The exception to report.public void close()
produce(ConsumerRecords)
method and the pollNext()
will throw a Handover.ClosedException
on any currently blocking and future
invocations.
If an exception was previously reported via the reportError(Throwable)
method,
that exception will not be overridden. The consumer thread will throw that exception upon
calling pollNext()
, rather than the ClosedException
.
close
in interface Closeable
close
in interface AutoCloseable
public void wakeupProducer()
produce(ConsumerRecords)
method, it will exit the method throwing a Handover.WakeupException
.Copyright © 2014–2023 The Apache Software Foundation. All rights reserved.