Package com.github.shyiko.mysql.binlog
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BinaryLogClient.AbstractLifecycleListener
Default (no-op) implementation ofBinaryLogClient.LifecycleListener
.static interface
BinaryLogClient.EventListener
BinaryLogClient
's event listener.static interface
BinaryLogClient.LifecycleListener
BinaryLogClient
's lifecycle listener.
-
Field Summary
Fields Modifier and Type Field Description protected PacketChannel
channel
protected Object
gtid
protected GtidSet
gtidSet
protected Object
gtidSetAccessLock
-
Constructor Summary
Constructors Constructor Description 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
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
checkError(byte[] packet)
protected void
commitGtid(String sql)
void
connect()
Connect to the replication stream.void
connect(long timeout)
Connect to the replication stream in a separate thread.void
disconnect()
Disconnect from the replication stream.protected void
ensureEventDataDeserializer(EventType eventType, Class<? extends EventDataDeserializer> eventDataDeserializerClass)
protected void
ensureGtidEventDataDeserializer()
String
getBinlogFilename()
long
getBinlogPosition()
long
getConnectionId()
long
getConnectTimeout()
List<BinaryLogClient.EventListener>
getEventListeners()
String
getGtidSet()
long
getHeartbeatInterval()
long
getKeepAliveConnectTimeout()
Deprecated.in favour ofgetConnectTimeout()
long
getKeepAliveInterval()
List<BinaryLogClient.LifecycleListener>
getLifecycleListeners()
Boolean
getMariaDB()
int
getMariaDbSlaveCapability()
long
getMasterServerId()
long
getServerId()
SSLMode
getSSLMode()
boolean
isBlocking()
boolean
isConnected()
boolean
isGtidSetFallbackToPurged()
boolean
isKeepAlive()
boolean
isUseBinlogFilenamePositionInGtidMode()
boolean
isUseSendAnnotateRowsEvent()
void
registerEventListener(BinaryLogClient.EventListener eventListener)
Register event listener.void
registerLifecycleListener(BinaryLogClient.LifecycleListener lifecycleListener)
Register lifecycle listener.protected void
requestBinaryLogStream()
protected void
requestBinaryLogStreamMaria(long serverId)
void
setBinlogFilename(String binlogFilename)
void
setBinlogPosition(long binlogPosition)
void
setBlocking(boolean blocking)
void
setConnectTimeout(long connectTimeout)
void
setEventDeserializer(EventDeserializer eventDeserializer)
void
setGtidSet(String gtidStr)
void
setGtidSetFallbackToPurged(boolean gtidSetFallbackToPurged)
void
setHeartbeatInterval(long heartbeatInterval)
void
setKeepAlive(boolean keepAlive)
void
setKeepAliveConnectTimeout(long connectTimeout)
Deprecated.in favour ofsetConnectTimeout(long)
void
setKeepAliveInterval(long keepAliveInterval)
void
setMariaDbSlaveCapability(int mariaDbSlaveCapability)
Set the client's MariaDB slave compatibility level.void
setServerId(long serverId)
void
setSocketFactory(SocketFactory socketFactory)
void
setSSLMode(SSLMode sslMode)
void
setSslSocketFactory(SSLSocketFactory sslSocketFactory)
void
setThreadFactory(ThreadFactory threadFactory)
protected void
setupConnection()
Apply additional options for connection before requesting binlog stream.protected void
setupGtidSet()
void
setUseBinlogFilenamePositionInGtidMode(boolean useBinlogFilenamePositionInGtidMode)
void
setUseNonGracefulDisconnect(boolean useNonGracefulDisconnect)
void
setUseSendAnnotateRowsEvent(boolean useSendAnnotateRowsEvent)
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.protected void
updateGtidSet(Event event)
-
-
-
Field Detail
-
gtidSet
protected GtidSet gtidSet
-
gtidSetAccessLock
protected final Object gtidSetAccessLock
-
gtid
protected Object gtid
-
channel
protected volatile PacketChannel channel
-
-
Constructor Detail
-
BinaryLogClient
public BinaryLogClient(String username, String password)
Alias for BinaryLogClient("localhost", 3306, <no schema> = null, username, password).- Parameters:
username
- login namepassword
- 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).- Parameters:
schema
- database name, nullableusername
- login namepassword
- 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).- Parameters:
hostname
- mysql server hostnameport
- mysql server portusername
- login namepassword
- 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 hostnameport
- mysql server portschema
- database name, nullable. Note that this parameter has nothing to do with event filtering. It's used only during the authentication.username
- login namepassword
- password
-
-
Method Detail
-
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(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 interfaceBinaryLogClientMXBean
- 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 interfaceBinaryLogClientMXBean
- 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 interfaceBinaryLogClientMXBean
- 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 interfaceBinaryLogClientMXBean
- Parameters:
binlogPosition
- binary log position. Any value less than 4 gets automatically adjusted to 4 on connect.- See Also:
getBinlogPosition()
-
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(String)
-
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:
getGtidSet()
,setGtidSetFallbackToPurged(boolean)
-
isGtidSetFallbackToPurged
public boolean isGtidSetFallbackToPurged()
- Returns:
- whether gtid_purged is used as a fallback
- See Also:
setGtidSetFallbackToPurged(boolean)
-
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(boolean)
-
setUseBinlogFilenamePositionInGtidMode
public void setUseBinlogFilenamePositionInGtidMode(boolean useBinlogFilenamePositionInGtidMode)
- Parameters:
useBinlogFilenamePositionInGtidMode
- true if MySQL server should start streaming events from a givengetBinlogFilename()
andgetBinlogPosition()
instead of "the oldest known binlog" whengetGtidSet()
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(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()
,setKeepAliveInterval(long)
-
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()
,setHeartbeatInterval(long)
-
getKeepAliveConnectTimeout
public long getKeepAliveConnectTimeout()
Deprecated.in favour ofgetConnectTimeout()
- Returns:
- "keep alive" connect timeout in milliseconds.
- See Also:
setKeepAliveConnectTimeout(long)
-
setKeepAliveConnectTimeout
public void setKeepAliveConnectTimeout(long connectTimeout)
Deprecated.in favour ofsetConnectTimeout(long)
- Parameters:
connectTimeout
- "keep alive" connect timeout in milliseconds.- See Also:
getKeepAliveConnectTimeout()
-
getHeartbeatInterval
public long getHeartbeatInterval()
- Returns:
- heartbeat period in milliseconds (0 if not set (default)).
- See Also:
setHeartbeatInterval(long)
-
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 withinsetKeepAliveInterval(long)
(instead of trying to send PING everysetKeepAliveInterval(long)
, which is fundamentally flawed - https://github.com/shyiko/mysql-binlog-connector-java/issues/118).
- See Also:
getHeartbeatInterval()
-
getConnectTimeout
public long getConnectTimeout()
- Returns:
- connect timeout in milliseconds, 3 seconds by default.
- See Also:
setConnectTimeout(long)
-
setConnectTimeout
public void setConnectTimeout(long connectTimeout)
- Parameters:
connectTimeout
- connect timeout in milliseconds.- See Also:
getConnectTimeout()
-
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 failsServerException
- if MySQL server responds with an errorIOException
- if anything goes wrong while trying to connectIllegalStateException
- 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 interfaceBinaryLogClientMXBean
- Parameters:
timeout
- timeout in milliseconds- Throws:
AuthenticationException
- if authentication failsServerException
- if MySQL server responds with an errorIOException
- if anything goes wrong while trying to connectTimeoutException
- if client was unable to connect within given time limit
-
isConnected
public boolean isConnected()
- Specified by:
isConnected
in interfaceBinaryLogClientMXBean
- 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 followingconnect()
resumes client from where it left off.- Specified by:
disconnect
in interfaceBinaryLogClientMXBean
- Throws:
IOException
-
-