Class ChainedReader
- java.lang.Object
-
- io.debezium.connector.mysql.legacy.ChainedReader
-
- All Implemented Interfaces:
Reader
@ThreadSafe public final class ChainedReader extends Object implements Reader
AReader
implementation that runs one or more otherReader
s in a consistently, completely, and sequentially. This reader ensures that all records generated by one of its containedReader
s are all passed through to callers viapolling
before the next reader is started. And, when this reader isstopped
, this class ensures that current reader is stopped and that no additional readers will be started.- Author:
- Randall Hauch
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ChainedReader.Builder
-
Nested classes/interfaces inherited from interface io.debezium.connector.mysql.legacy.Reader
Reader.State
-
-
Field Summary
Fields Modifier and Type Field Description private AtomicBoolean
completed
private String
completionMessage
private AtomicReference<Reader>
currentReader
private org.slf4j.Logger
logger
private List<Reader>
readers
private LinkedList<Reader>
remainingReaders
private AtomicBoolean
running
private AtomicReference<Consumer<MySqlPartition>>
uponCompletion
-
Constructor Summary
Constructors Modifier Constructor Description private
ChainedReader(List<Reader> readers, String completionMessage)
Create a new chained reader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
destroy()
After the reader has stopped, there may still be some resources we want left available until the connector task is destroyed.void
initialize()
Perform any initialization of the reader before being started.String
name()
Get the name of this reader.List<org.apache.kafka.connect.source.SourceRecord>
poll()
Poll for the next batch of source records.private void
readerCompletedPolling(MySqlPartition partition)
Called when the previously-started reader has returned all of its records viapolling
.void
start(MySqlPartition partition)
Start the reader and return immediately.private boolean
startNextReader(MySqlPartition partition)
Start the next reader.Reader.State
state()
Get the current state of this reader.void
stop()
Stop the reader from running and transition to theReader.State.STOPPING
state until all remaining records areconsumed
, at which point its state transitions toReader.State.STOPPED
.void
uponCompletion(Consumer<MySqlPartition> handler)
Set the function that should be called when this reader transitions from theReader.State.STOPPING
toReader.State.STOPPED
state, which is after all generated records have been consumed via thepoll
method.
-
-
-
Field Detail
-
logger
private final org.slf4j.Logger logger
-
completionMessage
private final String completionMessage
-
remainingReaders
private final LinkedList<Reader> remainingReaders
-
running
private final AtomicBoolean running
-
completed
private final AtomicBoolean completed
-
currentReader
private final AtomicReference<Reader> currentReader
-
uponCompletion
private final AtomicReference<Consumer<MySqlPartition>> uponCompletion
-
-
Method Detail
-
uponCompletion
public void uponCompletion(Consumer<MySqlPartition> handler)
Description copied from interface:Reader
Set the function that should be called when this reader transitions from theReader.State.STOPPING
toReader.State.STOPPED
state, which is after all generated records have been consumed via thepoll
method.This method should only be called while the reader is in the
Reader.State.STOPPED
state.- Specified by:
uponCompletion
in interfaceReader
- Parameters:
handler
- the function; may not be null
-
initialize
public void initialize()
Description copied from interface:Reader
Perform any initialization of the reader before being started. This method should be called exactly once beforeReader.start(MySqlPartition)
()} is called, and it should block until all initialization is completed.- Specified by:
initialize
in interfaceReader
-
destroy
public void destroy()
Description copied from interface:Reader
After the reader has stopped, there may still be some resources we want left available until the connector task is destroyed. This method is used to clean up those remaining resources upon shutdown. This method is effectively the opposite ofReader.initialize()
, performing any de-initialization of the reader entity before shutdown. This method should be called exactly once afterReader.stop()
is called, and it should block until all de-initialization is completed.
-
start
public void start(MySqlPartition partition)
Description copied from interface:Reader
Start the reader and return immediately. Once started, theSourceRecord
s generated by the reader can be obtained by periodically callingReader.poll()
until that method returnsnull
.This method does nothing if it is already running.
-
stop
public void stop()
Description copied from interface:Reader
Stop the reader from running and transition to theReader.State.STOPPING
state until all remaining records areconsumed
, at which point its state transitions toReader.State.STOPPED
.
-
state
public Reader.State state()
Description copied from interface:Reader
Get the current state of this reader.
-
poll
public List<org.apache.kafka.connect.source.SourceRecord> poll() throws InterruptedException
Description copied from interface:Reader
Poll for the next batch of source records. This method returnsnull
only when all records generated by this reader have been processed, following the natural or explicitstopping
of this reader. Note that this method may block if no additional records are available but the reader may produce more, thus callers should call this method continually until this method returnsnull
.- Specified by:
poll
in interfaceReader
- Returns:
- the list of source records that may or may not be empty; or
null
when there will be no more records because the reader has completelyReader.State.STOPPED
. - Throws:
InterruptedException
- if this thread is interrupted while waiting for more records
-
readerCompletedPolling
private void readerCompletedPolling(MySqlPartition partition)
Called when the previously-started reader has returned all of its records viapolling
. Only when this method is called is the now-completed reader removed as the current reader, and this is what guarantees that all records produced by the now-completed reader have been polled.
-
startNextReader
private boolean startNextReader(MySqlPartition partition)
Start the next reader.- Returns:
true
if the next reader was started, orfalse
if there are no more readers
-
-