Class BinaryLogClient

java.lang.Object
com.github.shyiko.mysql.binlog.BinaryLogClient
All Implemented Interfaces:
BinaryLogClientMXBean

public class BinaryLogClient extends Object implements BinaryLogClientMXBean
MySQL replication stream client.
Author:
Stanley Shyiko
  • Field Details

    • gtidSet

      protected GtidSet gtidSet
    • gtidSetAccessLock

      protected final Object gtidSetAccessLock
    • gtid

      protected Object gtid
    • channel

      protected volatile PacketChannel channel
  • Constructor Details

    • BinaryLogClient

      public BinaryLogClient(String username, String password)
      Alias for BinaryLogClient("localhost", 3306, <no schema> = null, username, password).
      Parameters:
      username - login name
      password - password
      See Also:
    • BinaryLogClient

      public BinaryLogClient(String schema, String username, String password)
      Alias for BinaryLogClient("localhost", 3306, schema, username, password).
      Parameters:
      schema - database name, nullable
      username - login name
      password - password
      See Also:
    • BinaryLogClient

      public BinaryLogClient(String hostname, int port, String username, String password)
      Alias for BinaryLogClient(hostname, port, <no schema> = null, username, password).
      Parameters:
      hostname - mysql server hostname
      port - mysql server port
      username - login name
      password - password
      See Also:
    • BinaryLogClient

      public BinaryLogClient(String hostname, int port, String schema, String username, String password)
      Parameters:
      hostname - mysql server hostname
      port - mysql server port
      schema - database name, nullable. Note that this parameter has nothing to do with event filtering. It's used only during the authentication.
      username - login name
      password - password
  • Method Details

    • isBlocking

      public boolean isBlocking()
    • setBlocking

      public void setBlocking(boolean blocking)
      Parameters:
      blocking - blocking mode. If set to false - BinaryLogClient will disconnect after the last event.
    • getSSLMode

      public SSLMode getSSLMode()
    • setSSLMode

      public void setSSLMode(SSLMode sslMode)
    • setUseNonGracefulDisconnect

      public void setUseNonGracefulDisconnect(boolean useNonGracefulDisconnect)
    • getMasterServerId

      public long getMasterServerId()
    • getServerId

      public long getServerId()
      Returns:
      server id (65535 by default)
      See Also:
    • setServerId

      public void setServerId(long serverId)
      Parameters:
      serverId - server id (in the range from 1 to 2^32 - 1). This value MUST be unique across whole replication group (that is, different from any other server id being used by any master or slave). Keep in mind that each binary log client (mysql-binlog-connector-java/BinaryLogClient, mysqlbinlog, etc) should be treated as a simplified slave and thus MUST also use a different server id.
      See Also:
    • getBinlogFilename

      public String getBinlogFilename()
      Specified by:
      getBinlogFilename in interface BinaryLogClientMXBean
      Returns:
      binary log filename, nullable (and null be default). Note that this value is automatically tracked by the client and thus is subject to change (in response to EventType.ROTATE, for example).
      See Also:
    • setBinlogFilename

      public void setBinlogFilename(String binlogFilename)
      Specified by:
      setBinlogFilename in interface BinaryLogClientMXBean
      Parameters:
      binlogFilename - binary log filename. Special values are:
      • null, which turns on automatic resolution (resulting in the last known binlog and position). This is what happens by default when you don't specify binary log filename explicitly.
      • "" (empty string), which instructs server to stream events starting from the oldest known binlog.
      See Also:
    • getBinlogPosition

      public long getBinlogPosition()
      Specified by:
      getBinlogPosition in interface BinaryLogClientMXBean
      Returns:
      binary log position of the next event, 4 by default (which is a position of first event). Note that this value changes with each incoming event.
      See Also:
    • setBinlogPosition

      public void setBinlogPosition(long binlogPosition)
      Specified by:
      setBinlogPosition in interface BinaryLogClientMXBean
      Parameters:
      binlogPosition - binary log position. Any value less than 4 gets automatically adjusted to 4 on connect.
      See Also:
    • getConnectionId

      public long getConnectionId()
      Returns:
      thread id
    • getGtidSet

      public String getGtidSet()
      Returns:
      GTID set. Note that this value changes with each received GTID event (provided client is in GTID mode).
      See Also:
    • setGtidSet

      public void setGtidSet(String gtidStr)
      Parameters:
      gtidStr - GTID set string (can be an empty string).

      NOTE #1: Any value but null will switch BinaryLogClient into a GTID mode (this will also set binlogFilename to "" (provided it's null) forcing MySQL to send events starting from the oldest known binlog (keep in mind that connection will fail if gtid_purged is anything but empty (unless setGtidSetFallbackToPurged(boolean) is set to true))).

      NOTE #2: GTID set is automatically updated with each incoming GTID event (provided GTID mode is on).

      See Also:
    • isGtidSetFallbackToPurged

      public boolean isGtidSetFallbackToPurged()
      Returns:
      whether gtid_purged is used as a fallback
      See Also:
    • setGtidSetFallbackToPurged

      public void setGtidSetFallbackToPurged(boolean gtidSetFallbackToPurged)
      Parameters:
      gtidSetFallbackToPurged - true if gtid_purged should be used as a fallback when gtidSet is set to "" and MySQL server has purged some of the binary logs, false otherwise (default).
    • isUseBinlogFilenamePositionInGtidMode

      public boolean isUseBinlogFilenamePositionInGtidMode()
      Returns:
      value of useBinlogFilenamePostionInGtidMode
      See Also:
    • setUseBinlogFilenamePositionInGtidMode

      public void setUseBinlogFilenamePositionInGtidMode(boolean useBinlogFilenamePositionInGtidMode)
      Parameters:
      useBinlogFilenamePositionInGtidMode - true if MySQL server should start streaming events from a given getBinlogFilename() and getBinlogPosition() instead of "the oldest known binlog" when getGtidSet() is set, false otherwise (default).
    • isKeepAlive

      public boolean isKeepAlive()
      Returns:
      true if "keep alive" thread should be automatically started (default), false otherwise.
      See Also:
    • setKeepAlive

      public void setKeepAlive(boolean keepAlive)
      Parameters:
      keepAlive - true if "keep alive" thread should be automatically started (recommended and true by default), false otherwise.
      See Also:
    • getKeepAliveInterval

      public long getKeepAliveInterval()
      Returns:
      "keep alive" interval in milliseconds, 1 minute by default.
      See Also:
    • setKeepAliveInterval

      public void setKeepAliveInterval(long keepAliveInterval)
      Parameters:
      keepAliveInterval - "keep alive" interval in milliseconds.
      See Also:
    • getKeepAliveConnectTimeout

      public long getKeepAliveConnectTimeout()
      Deprecated.
      in favour of getConnectTimeout()
      Returns:
      "keep alive" connect timeout in milliseconds.
      See Also:
    • setKeepAliveConnectTimeout

      public void setKeepAliveConnectTimeout(long connectTimeout)
      Deprecated.
      Parameters:
      connectTimeout - "keep alive" connect timeout in milliseconds.
      See Also:
    • getHeartbeatInterval

      public long getHeartbeatInterval()
      Returns:
      heartbeat period in milliseconds (0 if not set (default)).
      See Also:
    • setHeartbeatInterval

      public void setHeartbeatInterval(long heartbeatInterval)
      Parameters:
      heartbeatInterval - heartbeat period in milliseconds.

      If set (recommended)

      • HEARTBEAT event will be emitted every "heartbeatInterval".
      • if setKeepAlive(boolean) is on then keepAlive thread will attempt to reconnect if no HEARTBEAT events were received within setKeepAliveInterval(long) (instead of trying to send PING every setKeepAliveInterval(long), which is fundamentally flawed - https://github.com/shyiko/mysql-binlog-connector-java/issues/118).
      Note that when used together with keepAlive heartbeatInterval MUST be set less than keepAliveInterval.
      See Also:
    • getConnectTimeout

      public long getConnectTimeout()
      Returns:
      connect timeout in milliseconds, 3 seconds by default.
      See Also:
    • setConnectTimeout

      public void setConnectTimeout(long connectTimeout)
      Parameters:
      connectTimeout - connect timeout in milliseconds.
      See Also:
    • setEventDeserializer

      public void setEventDeserializer(EventDeserializer eventDeserializer)
      Parameters:
      eventDeserializer - custom event deserializer
    • setSocketFactory

      public void setSocketFactory(SocketFactory socketFactory)
      Parameters:
      socketFactory - custom socket factory. If not provided, socket will be created with "new Socket()".
    • setSslSocketFactory

      public void setSslSocketFactory(SSLSocketFactory sslSocketFactory)
      Parameters:
      sslSocketFactory - custom ssl socket factory
    • setThreadFactory

      public void setThreadFactory(ThreadFactory threadFactory)
      Parameters:
      threadFactory - custom thread factory. If not provided, threads will be created using simple "new Thread()".
    • getMariaDB

      public Boolean getMariaDB()
      Returns:
      true/false depending on whether we've connected to MariaDB. NULL if not connected.
    • isUseSendAnnotateRowsEvent

      public boolean isUseSendAnnotateRowsEvent()
    • setUseSendAnnotateRowsEvent

      public void setUseSendAnnotateRowsEvent(boolean useSendAnnotateRowsEvent)
    • getMariaDbSlaveCapability

      public int getMariaDbSlaveCapability()
      Returns:
      the configured MariaDB slave compatibility level, defaults to 4.
    • setMariaDbSlaveCapability

      public void setMariaDbSlaveCapability(int mariaDbSlaveCapability)
      Set the client's MariaDB slave compatibility level. This only applies when connecting to MariaDB.
      Parameters:
      mariaDbSlaveCapability - the expected compatibility level
    • connect

      public void connect() throws IOException, IllegalStateException
      Connect to the replication stream. Note that this method blocks until disconnected.
      Throws:
      AuthenticationException - if authentication fails
      ServerException - if MySQL server responds with an error
      IOException - if anything goes wrong while trying to connect
      IllegalStateException - if binary log client is already connected
    • setupConnection

      protected void setupConnection() throws IOException
      Apply additional options for connection before requesting binlog stream.
      Throws:
      IOException
    • checkError

      protected void checkError(byte[] packet) throws IOException
      Throws:
      IOException
    • requestBinaryLogStream

      protected void requestBinaryLogStream() throws IOException
      Throws:
      IOException
    • requestBinaryLogStreamMaria

      protected void requestBinaryLogStreamMaria(long serverId) throws IOException
      Throws:
      IOException
    • ensureEventDataDeserializer

      protected void ensureEventDataDeserializer(EventType eventType, Class<? extends EventDataDeserializer> eventDataDeserializerClass)
    • ensureGtidEventDataDeserializer

      protected void ensureGtidEventDataDeserializer()
    • connect

      public void connect(long timeout) throws IOException, TimeoutException
      Connect to the replication stream in a separate thread.
      Specified by:
      connect in interface BinaryLogClientMXBean
      Parameters:
      timeout - timeout in milliseconds
      Throws:
      AuthenticationException - if authentication fails
      ServerException - if MySQL server responds with an error
      IOException - if anything goes wrong while trying to connect
      TimeoutException - if client was unable to connect within given time limit
    • isConnected

      public boolean isConnected()
      Specified by:
      isConnected in interface BinaryLogClientMXBean
      Returns:
      true if client is connected, false otherwise
    • setupGtidSet

      protected void setupGtidSet() throws IOException
      Throws:
      IOException
    • updateGtidSet

      protected void updateGtidSet(Event event)
    • commitGtid

      protected void commitGtid(String sql)
    • getEventListeners

      public List<BinaryLogClient.EventListener> getEventListeners()
      Returns:
      registered event listeners
    • registerEventListener

      public void registerEventListener(BinaryLogClient.EventListener eventListener)
      Register event listener. Note that multiple event listeners will be called in order they where registered.
      Parameters:
      eventListener - event listener
    • unregisterEventListener

      public void unregisterEventListener(Class<? extends BinaryLogClient.EventListener> listenerClass)
      Unregister all event listener of specific type.
      Parameters:
      listenerClass - event listener class to unregister
    • unregisterEventListener

      public void unregisterEventListener(BinaryLogClient.EventListener eventListener)
      Unregister single event listener.
      Parameters:
      eventListener - event listener to unregister
    • getLifecycleListeners

      public List<BinaryLogClient.LifecycleListener> getLifecycleListeners()
      Returns:
      registered lifecycle listeners
    • registerLifecycleListener

      public void registerLifecycleListener(BinaryLogClient.LifecycleListener lifecycleListener)
      Register lifecycle listener. Note that multiple lifecycle listeners will be called in order they where registered.
      Parameters:
      lifecycleListener - lifecycle listener to register
    • unregisterLifecycleListener

      public void unregisterLifecycleListener(Class<? extends BinaryLogClient.LifecycleListener> listenerClass)
      Unregister all lifecycle listener of specific type.
      Parameters:
      listenerClass - lifecycle listener class to unregister
    • unregisterLifecycleListener

      public void unregisterLifecycleListener(BinaryLogClient.LifecycleListener eventListener)
      Unregister single lifecycle listener.
      Parameters:
      eventListener - lifecycle listener to unregister
    • disconnect

      public void disconnect() throws IOException
      Disconnect from the replication stream. Note that this does not cause binlogFilename/binlogPosition to be cleared out. As the result following connect() resumes client from where it left off.
      Specified by:
      disconnect in interface BinaryLogClientMXBean
      Throws:
      IOException