Interface Reader

All Known Implementing Classes:
AbstractReader, BinlogReader, BlockingReader, ChainedReader, ParallelSnapshotReader, ReconcilingBinlogReader, SnapshotReader, TimedBlockingReader

public interface Reader
A component that reads a portion of the MySQL server history or current state.

A reader starts out in the stopped state, and when start(MySqlPartition) () started} transitions to a Reader.State.RUNNING state. The reader may either complete its work or be explicitly stopped, at which point the reader transitions to a {@value State#STOPPING stopping} state until all already-generated SourceRecords are consumed by the client via the poll method. Only after all records are consumed does the reader transition to the stopped state and call the uponCompletion(Consumer) method.

See ChainedReader if multiple Reader implementations are to be run in-sequence while keeping the correct start, stop, and completion semantics.

Author:
Randall Hauch
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static enum 
    The possible states of a reader.
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    After the reader has stopped, there may still be some resources we want left available until the connector task is destroyed.
    default void
    Perform any initialization of the reader before being started.
    Get the name of this reader.
    List<org.apache.kafka.connect.source.SourceRecord>
    Poll for the next batch of source records.
    void
    start(MySqlPartition partition)
    Start the reader and return immediately.
    Get the current state of this reader.
    void
    Stop the reader from running and transition to the Reader.State.STOPPING state until all remaining records are consumed, at which point its state transitions to Reader.State.STOPPED.
    void
    Set the function that should be called when this reader transitions from the Reader.State.STOPPING to Reader.State.STOPPED state, which is after all generated records have been consumed via the poll method.
  • Method Details

    • name

      String name()
      Get the name of this reader.
      Returns:
      the reader's name; never null
    • state

      Reader.State state()
      Get the current state of this reader.
      Returns:
      the state; never null
    • uponCompletion

      void uponCompletion(Consumer<MySqlPartition> handler)
      Set the function that should be called when this reader transitions from the Reader.State.STOPPING to Reader.State.STOPPED state, which is after all generated records have been consumed via the poll method.

      This method should only be called while the reader is in the Reader.State.STOPPED state.

      Parameters:
      handler - the function; may not be null
    • initialize

      default void initialize()
      Perform any initialization of the reader before being started. This method should be called exactly once before start(MySqlPartition) ()} is called, and it should block until all initialization is completed.
    • destroy

      default void destroy()
      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 of initialize(), performing any de-initialization of the reader entity before shutdown. This method should be called exactly once after stop() is called, and it should block until all de-initialization is completed.
    • start

      void start(MySqlPartition partition)
      Start the reader and return immediately. Once started, the SourceRecords generated by the reader can be obtained by periodically calling poll() until that method returns null.

      This method does nothing if it is already running.

    • stop

      void stop()
      Stop the reader from running and transition to the Reader.State.STOPPING state until all remaining records are consumed, at which point its state transitions to Reader.State.STOPPED.
    • poll

      List<org.apache.kafka.connect.source.SourceRecord> poll() throws InterruptedException
      Poll for the next batch of source records. This method returns null only when all records generated by this reader have been processed, following the natural or explicit stopping 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 returns null.
      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 completely Reader.State.STOPPED.
      Throws:
      InterruptedException - if this thread is interrupted while waiting for more records
      org.apache.kafka.connect.errors.ConnectException - if there is an error while this reader is running