-
Notifications
You must be signed in to change notification settings - Fork 994
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from zendesk/config_to_context
REFACTOR-ONLY refactor some functionality out of MaxwellConfig
- Loading branch information
Showing
9 changed files
with
140 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.zendesk.maxwell; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
import com.zendesk.maxwell.producer.*; | ||
import com.zendesk.maxwell.schema.SchemaPosition; | ||
|
||
import snaq.db.ConnectionPool; | ||
|
||
public class MaxwellContext { | ||
private final ConnectionPool connectionPool; | ||
private final MaxwellConfig config; | ||
private SchemaPosition schemaPosition; | ||
private Long serverID; | ||
private BinlogPosition initialPosition; | ||
|
||
public MaxwellContext(MaxwellConfig config) { | ||
this.config = config; | ||
this.connectionPool = new ConnectionPool("MaxwellConnectionPool", 10, 0, 10, | ||
config.getConnectionURI(), config.mysqlUser, config.mysqlPassword); | ||
} | ||
|
||
public MaxwellConfig getConfig() { | ||
return this.config; | ||
} | ||
|
||
public ConnectionPool getConnectionPool() { | ||
return this.connectionPool; | ||
} | ||
|
||
public void terminate() { | ||
if ( this.schemaPosition != null ) { | ||
this.schemaPosition.stop(); | ||
this.schemaPosition = null; | ||
} | ||
this.connectionPool.release(); | ||
} | ||
|
||
private SchemaPosition getSchemaPosition() throws SQLException { | ||
if ( this.schemaPosition == null ) { | ||
this.schemaPosition = new SchemaPosition(this.getConnectionPool(), this.getServerID()); | ||
this.schemaPosition.start(); | ||
} | ||
return this.schemaPosition; | ||
} | ||
|
||
public BinlogPosition getInitialPosition() throws FileNotFoundException, IOException, SQLException { | ||
if ( this.initialPosition != null ) | ||
return this.initialPosition; | ||
|
||
this.initialPosition = getSchemaPosition().get(); | ||
return this.initialPosition; | ||
} | ||
|
||
public void setInitialPosition(BinlogPosition position) throws SQLException { | ||
this.getSchemaPosition().set(position); | ||
} | ||
|
||
public Long getServerID() throws SQLException { | ||
if ( this.serverID != null) | ||
return this.serverID; | ||
|
||
try ( Connection c = getConnectionPool().getConnection() ) { | ||
ResultSet rs = c.createStatement().executeQuery("SELECT @@server_id as server_id"); | ||
if ( !rs.next() ) { | ||
throw new RuntimeException("Could not retrieve server_id!"); | ||
} | ||
this.serverID = rs.getLong("server_id"); | ||
return this.serverID; | ||
} | ||
} | ||
|
||
public AbstractProducer getProducer() throws IOException { | ||
switch ( this.config.producerType ) { | ||
case "file": | ||
return new FileProducer(this, this.config.outputFile); | ||
case "kafka": | ||
return new MaxwellKafkaProducer(this, this.config.getKafkaProperties(), this.config.kafkaTopic); | ||
case "stdout": | ||
default: | ||
return new StdoutProducer(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
src/main/java/com/zendesk/maxwell/producer/AbstractProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.