Package com.github.shyiko.mysql.binlog
Class BinaryLogClient
java.lang.Object
com.github.shyiko.mysql.binlog.BinaryLogClient
- All Implemented Interfaces:
BinaryLogClientMXBean
MySQL replication stream client.
- Author:
- Stanley Shyiko
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDefault (no-op) implementation ofBinaryLogClient.LifecycleListener.static interfaceBinaryLogClient's event listener.static interfaceBinaryLogClient's lifecycle listener. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected PacketChannelprotected Objectprotected GtidSetprotected final Object -
Constructor Summary
ConstructorsConstructorDescriptionBinaryLogClient(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
Modifier and TypeMethodDescriptionprotected voidcheckError(byte[] packet) protected voidcommitGtid(String sql) voidconnect()Connect to the replication stream.voidconnect(long timeout) Connect to the replication stream in a separate thread.voidDisconnect from the replication stream.protected voidensureEventDataDeserializer(EventType eventType, Class<? extends EventDataDeserializer> eventDataDeserializerClass) protected voidlonglonglonglonglongDeprecated.longintlonglongbooleanbooleanbooleanbooleanbooleanbooleanvoidregisterEventListener(BinaryLogClient.EventListener eventListener) Register event listener.voidregisterLifecycleListener(BinaryLogClient.LifecycleListener lifecycleListener) Register lifecycle listener.protected voidprotected voidrequestBinaryLogStreamMaria(long serverId) voidsetBinlogFilename(String binlogFilename) voidsetBinlogPosition(long binlogPosition) voidsetBlocking(boolean blocking) voidsetConnectTimeout(long connectTimeout) voidsetEventDeserializer(EventDeserializer eventDeserializer) voidsetGtidSet(String gtidStr) voidsetGtidSetFallbackToPurged(boolean gtidSetFallbackToPurged) voidsetHeartbeatInterval(long heartbeatInterval) voidsetKeepAlive(boolean keepAlive) voidsetKeepAliveConnectTimeout(long connectTimeout) Deprecated.in favour ofsetConnectTimeout(long)voidsetKeepAliveInterval(long keepAliveInterval) voidsetMariaDbSlaveCapability(int mariaDbSlaveCapability) Set the client's MariaDB slave compatibility level.voidsetServerId(long serverId) voidsetSocketFactory(SocketFactory socketFactory) voidsetSSLMode(SSLMode sslMode) voidsetSslSocketFactory(SSLSocketFactory sslSocketFactory) voidsetThreadFactory(ThreadFactory threadFactory) protected voidApply additional options for connection before requesting binlog stream.protected voidvoidsetUseBinlogFilenamePositionInGtidMode(boolean useBinlogFilenamePositionInGtidMode) voidsetUseNonGracefulDisconnect(boolean useNonGracefulDisconnect) voidsetUseSendAnnotateRowsEvent(boolean useSendAnnotateRowsEvent) voidunregisterEventListener(BinaryLogClient.EventListener eventListener) Unregister single event listener.voidunregisterEventListener(Class<? extends BinaryLogClient.EventListener> listenerClass) Unregister all event listener of specific type.voidunregisterLifecycleListener(BinaryLogClient.LifecycleListener eventListener) Unregister single lifecycle listener.voidunregisterLifecycleListener(Class<? extends BinaryLogClient.LifecycleListener> listenerClass) Unregister all lifecycle listener of specific type.protected voidupdateGtidSet(Event event)
-
Field Details
-
gtidSet
-
gtidSetAccessLock
-
gtid
-
channel
-
-
Constructor Details
-
BinaryLogClient
Alias for BinaryLogClient("localhost", 3306, <no schema> = null, username, password).- Parameters:
username- login namepassword- password- See Also:
-
BinaryLogClient
Alias for BinaryLogClient("localhost", 3306, schema, username, password).- Parameters:
schema- database name, nullableusername- login namepassword- password- See Also:
-
BinaryLogClient
Alias for BinaryLogClient(hostname, port, <no schema> = null, username, password).- Parameters:
hostname- mysql server hostnameport- mysql server portusername- login namepassword- password- See Also:
-
BinaryLogClient
- 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 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
-
setSSLMode
-
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
- Specified by:
getBinlogFilenamein 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
- Specified by:
setBinlogFilenamein 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:
-
getBinlogPosition
public long getBinlogPosition()- Specified by:
getBinlogPositionin 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
public void setBinlogPosition(long binlogPosition) - Specified by:
setBinlogPositionin interfaceBinaryLogClientMXBean- 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
- Returns:
- GTID set. Note that this value changes with each received GTID event (provided client is in GTID mode).
- See Also:
-
setGtidSet
- 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 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
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 ofgetConnectTimeout()- Returns:
- "keep alive" connect timeout in milliseconds.
- See Also:
-
setKeepAliveConnectTimeout
public void setKeepAliveConnectTimeout(long connectTimeout) Deprecated.in favour ofsetConnectTimeout(long)- 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 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:
-
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
- Parameters:
eventDeserializer- custom event deserializer
-
setSocketFactory
- Parameters:
socketFactory- custom socket factory. If not provided, socket will be created with "new Socket()".
-
setSslSocketFactory
- Parameters:
sslSocketFactory- custom ssl socket factory
-
setThreadFactory
- Parameters:
threadFactory- custom thread factory. If not provided, threads will be created using simple "new Thread()".
-
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
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
Apply additional options for connection before requesting binlog stream.- Throws:
IOException
-
checkError
- Throws:
IOException
-
requestBinaryLogStream
- Throws:
IOException
-
requestBinaryLogStreamMaria
- Throws:
IOException
-
ensureEventDataDeserializer
protected void ensureEventDataDeserializer(EventType eventType, Class<? extends EventDataDeserializer> eventDataDeserializerClass) -
ensureGtidEventDataDeserializer
protected void ensureGtidEventDataDeserializer() -
connect
Connect to the replication stream in a separate thread.- Specified by:
connectin 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:
isConnectedin interfaceBinaryLogClientMXBean- Returns:
- true if client is connected, false otherwise
-
setupGtidSet
- Throws:
IOException
-
updateGtidSet
-
commitGtid
-
getEventListeners
- Returns:
- registered event listeners
-
registerEventListener
Register event listener. Note that multiple event listeners will be called in order they where registered.- Parameters:
eventListener- event listener
-
unregisterEventListener
Unregister all event listener of specific type.- Parameters:
listenerClass- event listener class to unregister
-
unregisterEventListener
Unregister single event listener.- Parameters:
eventListener- event listener to unregister
-
getLifecycleListeners
- Returns:
- registered lifecycle listeners
-
registerLifecycleListener
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
Unregister single lifecycle listener.- Parameters:
eventListener- lifecycle listener to unregister
-
disconnect
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:
disconnectin interfaceBinaryLogClientMXBean- Throws:
IOException
-
getConnectTimeout()