Class ChainedReader
- java.lang.Object
-
- io.debezium.connector.mysql.legacy.ChainedReader
-
- All Implemented Interfaces:
Reader
@ThreadSafe public final class ChainedReader extends Object implements Reader
AReaderimplementation that runs one or more otherReaders in a consistently, completely, and sequentially. This reader ensures that all records generated by one of its containedReaders are all passed through to callers viapollingbefore 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 classChainedReader.Builder-
Nested classes/interfaces inherited from interface io.debezium.connector.mysql.legacy.Reader
Reader.State
-
-
Field Summary
Fields Modifier and Type Field Description private AtomicBooleancompletedprivate StringcompletionMessageprivate AtomicReference<Reader>currentReaderprivate org.slf4j.Loggerloggerprivate List<Reader>readersprivate LinkedList<Reader>remainingReadersprivate AtomicBooleanrunningprivate AtomicReference<Consumer<MySqlPartition>>uponCompletion
-
Constructor Summary
Constructors Modifier Constructor Description privateChainedReader(List<Reader> readers, String completionMessage)Create a new chained reader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddestroy()After the reader has stopped, there may still be some resources we want left available until the connector task is destroyed.voidinitialize()Perform any initialization of the reader before being started.Stringname()Get the name of this reader.List<org.apache.kafka.connect.source.SourceRecord>poll()Poll for the next batch of source records.private voidreaderCompletedPolling(MySqlPartition partition)Called when the previously-started reader has returned all of its records viapolling.voidstart(MySqlPartition partition)Start the reader and return immediately.private booleanstartNextReader(MySqlPartition partition)Start the next reader.Reader.Statestate()Get the current state of this reader.voidstop()Stop the reader from running and transition to theReader.State.STOPPINGstate until all remaining records areconsumed, at which point its state transitions toReader.State.STOPPED.voiduponCompletion(Consumer<MySqlPartition> handler)Set the function that should be called when this reader transitions from theReader.State.STOPPINGtoReader.State.STOPPEDstate, which is after all generated records have been consumed via thepollmethod.
-
-
-
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:ReaderSet the function that should be called when this reader transitions from theReader.State.STOPPINGtoReader.State.STOPPEDstate, which is after all generated records have been consumed via thepollmethod.This method should only be called while the reader is in the
Reader.State.STOPPEDstate.- Specified by:
uponCompletionin interfaceReader- Parameters:
handler- the function; may not be null
-
initialize
public void initialize()
Description copied from interface:ReaderPerform 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:
initializein interfaceReader
-
destroy
public void destroy()
Description copied from interface:ReaderAfter 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:ReaderStart the reader and return immediately. Once started, theSourceRecords 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:ReaderStop the reader from running and transition to theReader.State.STOPPINGstate until all remaining records areconsumed, at which point its state transitions toReader.State.STOPPED.
-
state
public Reader.State state()
Description copied from interface:ReaderGet the current state of this reader.
-
poll
public List<org.apache.kafka.connect.source.SourceRecord> poll() throws InterruptedException
Description copied from interface:ReaderPoll for the next batch of source records. This method returnsnullonly when all records generated by this reader have been processed, following the natural or explicitstoppingof 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:
pollin interfaceReader- Returns:
- the list of source records that may or may not be empty; or
nullwhen 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:
trueif the next reader was started, orfalseif there are no more readers
-
-