com.github.shyiko.mysql.binlog
Class BinaryLogClient

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

public class BinaryLogClient
extends Object
implements BinaryLogClientMXBean

MySQL replication stream client.

Author:
Stanley Shyiko

Nested Class Summary
static class BinaryLogClient.AbstractLifecycleListener
          Default (no-op) implementation of BinaryLogClient.LifecycleListener.
static interface BinaryLogClient.EventListener
          BinaryLogClient's event listener.
static interface BinaryLogClient.LifecycleListener
          BinaryLogClient's lifecycle listener.
 
Constructor Summary
BinaryLogClient(String hostname, int port, String username, String password)
          Alias for BinaryLogClient(hostname, port, <no schema> = null, username, password).
BinaryLogClient(String hostname, int port, String schema, String username, String password)
           
BinaryLogClient(String username, String password)
          Alias for BinaryLogClient("localhost", 3306, <no schema> = null, username, password).
BinaryLogClient(String schema, String username, String password)
          Alias for BinaryLogClient("localhost", 3306, schema, username, password).
 
Method Summary
 void connect()
          Connect to the replication stream.
 void connect(long timeoutInMilliseconds)
          Connect to the replication stream in a separate thread.
 void disconnect()
          Disconnect from the replication stream.
 String getBinlogFilename()
           
 long getBinlogPosition()
           
 List<BinaryLogClient.EventListener> getEventListeners()
           
 String getGtidSet()
           
 long getKeepAliveConnectTimeout()
           
 long getKeepAliveInterval()
           
 List<BinaryLogClient.LifecycleListener> getLifecycleListeners()
           
 long getServerId()
           
 boolean isConnected()
           
 boolean isKeepAlive()
           
 void registerEventListener(BinaryLogClient.EventListener eventListener)
          Register event listener.
 void registerLifecycleListener(BinaryLogClient.LifecycleListener lifecycleListener)
          Register lifecycle listener.
 void setBinlogFilename(String binlogFilename)
           
 void setBinlogPosition(long binlogPosition)
           
 void setEventDeserializer(EventDeserializer eventDeserializer)
           
 void setGtidSet(String gtidSet)
           
 void setKeepAlive(boolean keepAlive)
           
 void setKeepAliveConnectTimeout(long keepAliveConnectTimeout)
           
 void setKeepAliveInterval(long keepAliveInterval)
           
 void setServerId(long serverId)
           
 void setSocketFactory(SocketFactory socketFactory)
           
 void setThreadFactory(ThreadFactory threadFactory)
           
 void unregisterEventListener(BinaryLogClient.EventListener eventListener)
          Unregister single event listener.
 void unregisterEventListener(Class<? extends BinaryLogClient.EventListener> listenerClass)
          Unregister all event listener of specific type.
 void unregisterLifecycleListener(BinaryLogClient.LifecycleListener eventListener)
          Unregister single lifecycle listener.
 void unregisterLifecycleListener(Class<? extends BinaryLogClient.LifecycleListener> listenerClass)
          Unregister all lifecycle listener of specific type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BinaryLogClient

public BinaryLogClient(String username,
                       String password)
Alias for BinaryLogClient("localhost", 3306, <no schema> = null, username, password).

See Also:
BinaryLogClient(String, int, String, String, String)

BinaryLogClient

public BinaryLogClient(String schema,
                       String username,
                       String password)
Alias for BinaryLogClient("localhost", 3306, schema, username, password).

See Also:
BinaryLogClient(String, int, String, String, String)

BinaryLogClient

public BinaryLogClient(String hostname,
                       int port,
                       String username,
                       String password)
Alias for BinaryLogClient(hostname, port, <no schema> = null, username, password).

See Also:
BinaryLogClient(String, int, String, String, String)

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 Detail

getServerId

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

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:
getServerId()

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(String)

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:
getBinlogFilename()

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(long)

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:
getBinlogPosition()

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(String)

setGtidSet

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

NOTE #1: Any value but null will switch BinaryLogClient into a GTID mode (in which case GTID set will be updated with each incoming GTID event) as well as set binlogFilename to "" (empty string) (meaning BinaryLogClient will request events "outside of the set" starting from the oldest known binlog).

NOTE #2: setBinlogFilename(String) and setBinlogPosition(long) can be used to specify the exact position from which MySQL server should start streaming events (taking into account GTID set).

See Also:
getGtidSet()

isKeepAlive

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

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:
isKeepAlive()

getKeepAliveInterval

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

setKeepAliveInterval

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

getKeepAliveConnectTimeout

public long getKeepAliveConnectTimeout()
Returns:
"keep alive" connect timeout in milliseconds, 3 seconds by default.
See Also:
setKeepAliveConnectTimeout(long)

setKeepAliveConnectTimeout

public void setKeepAliveConnectTimeout(long keepAliveConnectTimeout)
Parameters:
keepAliveConnectTimeout - "keep alive" connect timeout in milliseconds.
See Also:
getKeepAliveConnectTimeout()

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()".

setThreadFactory

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

connect

public void connect()
             throws IOException
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

connect

public void connect(long timeoutInMilliseconds)
             throws IOException,
                    TimeoutException
Connect to the replication stream in a separate thread.

Specified by:
connect in interface BinaryLogClientMXBean
Parameters:
timeoutInMilliseconds - 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

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.


unregisterEventListener

public void unregisterEventListener(Class<? extends BinaryLogClient.EventListener> listenerClass)
Unregister all event listener of specific type.


unregisterEventListener

public void unregisterEventListener(BinaryLogClient.EventListener eventListener)
Unregister single event listener.


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.


unregisterLifecycleListener

public void unregisterLifecycleListener(Class<? extends BinaryLogClient.LifecycleListener> listenerClass)
Unregister all lifecycle listener of specific type.


unregisterLifecycleListener

public void unregisterLifecycleListener(BinaryLogClient.LifecycleListener eventListener)
Unregister single lifecycle listener.


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


Copyright © 2015. All Rights Reserved.