| 
 | 1 | +package applications;  | 
 | 2 | +/**  | 
 | 3 | +* The Application2 program implements an application that:  | 
 | 4 | +- Initialize a server instance (S1)  | 
 | 5 | +- Start S1 in a separate thread as replication master, passing the required additional options  | 
 | 6 | +- Initialize another server instance (S2)  | 
 | 7 | +- Start S2 in a separate thread as replication slave, passing the required additional options  | 
 | 8 | +- Set the master (S1) configuration in the slave (S2)  | 
 | 9 | +- Start replication in the slave (S2)  | 
 | 10 | +- Create a table in the master (S1)  | 
 | 11 | +- Wait 2 seconds (for replication to occur)  | 
 | 12 | +- Check that the table exists in the slave (S2)  | 
 | 13 | +- Stop S2 and S1  | 
 | 14 | +*  | 
 | 15 | +* @author Wagner Jose Franchin  | 
 | 16 | +* @version 1.0  | 
 | 17 | +* @since 2015年03月28日   | 
 | 18 | +*/  | 
 | 19 | + | 
 | 20 | +import java.io.File;  | 
 | 21 | +import java.io.IOException;  | 
 | 22 | +import java.sql.*;  | 
 | 23 | + | 
 | 24 | +import mysqlserver.*;  | 
 | 25 | + | 
 | 26 | +public class Application2 {  | 
 | 27 | + | 
 | 28 | + private static String connectionStringMaster;  | 
 | 29 | + private static String connectionStringSlave;  | 
 | 30 | + private static String username;  | 
 | 31 | + private static String password;  | 
 | 32 | + | 
 | 33 | + private static Connection connectionMaster, connectionSlave;  | 
 | 34 | + private static Statement commandMaster, commandSlave;  | 
 | 35 | + private static ResultSet data;  | 
 | 36 | + | 
 | 37 | + private static final String USER_TABLE = "PERSON";  | 
 | 38 | + | 
 | 39 | + public static void main(String[] args) throws SQLException, InterruptedException, IOException {  | 
 | 40 | + | 
 | 41 | + // Change below path to point to your MySQL binaries  | 
 | 42 | + File baseDir = new File(Utils.MYSQL_PATH);   | 
 | 43 | + | 
 | 44 | + File installDirMaster = new File("bdApp2Master");   | 
 | 45 | + File installDirSlave = new File("bdApp2Slave");   | 
 | 46 | + | 
 | 47 | + int master_port = 3306;  | 
 | 48 | + int slave_port = 3307;  | 
 | 49 | + | 
 | 50 | + connectionStringMaster = "jdbc:mysql://localhost:" + master_port + "/mysql" + Utils.CONNECT_PROPS;  | 
 | 51 | + connectionStringSlave = "jdbc:mysql://localhost:" + slave_port + "/mysql" + Utils.CONNECT_PROPS;  | 
 | 52 | + | 
 | 53 | + username = "root";  | 
 | 54 | + password = "";  | 
 | 55 | + | 
 | 56 | + final MySQLServer s1 = new MasterMySQLServer(baseDir, installDirMaster, master_port, 1, "mysql-bin");  | 
 | 57 | + final MySQLServer s2 = new SlaveMySQLServer(baseDir, installDirSlave, slave_port, 2);  | 
 | 58 | + | 
 | 59 | + try {  | 
 | 60 | + | 
 | 61 | + /*Initialize a server instance (S1)  | 
 | 62 | + Start S1 in a separate thread as replication master, passing the required additional options*/  | 
 | 63 | + initAndStarServer(s1);  | 
 | 64 | + | 
 | 65 | + /*Initialize a server instance (S2)  | 
 | 66 | + Start S2 in a separate thread as replication master, passing the required additional options*/  | 
 | 67 | + initAndStarServer(s2);  | 
 | 68 | + | 
 | 69 | + //Test to verify if the server instances are ready  | 
 | 70 | + while (s1.ping() != 0 || s2.ping() != 0) {  | 
 | 71 | + System.out.println("wait...");  | 
 | 72 | + Thread.sleep(500);  | 
 | 73 | + }  | 
 | 74 | + | 
 | 75 | + //Create DB connections  | 
 | 76 | + connectionMaster = DriverManager.getConnection(connectionStringMaster, username, password);  | 
 | 77 | + connectionSlave = DriverManager.getConnection(connectionStringSlave, username, password);  | 
 | 78 | + | 
 | 79 | + commandMaster = connectionMaster.createStatement();  | 
 | 80 | + commandSlave = connectionSlave.createStatement();  | 
 | 81 | + | 
 | 82 | + //Set the master (S1) configuration in the slave (S2)  | 
 | 83 | + commandSlave.execute("CHANGE MASTER TO MASTER_HOST='localhost', MASTER_PORT=" + master_port + ", MASTER_USER='" + username + "';");  | 
 | 84 | + | 
 | 85 | + //Start replication in the slave (S2)  | 
 | 86 | + commandSlave.execute("START SLAVE;");  | 
 | 87 | + | 
 | 88 | + testReplication();  | 
 | 89 | + | 
 | 90 | + }  | 
 | 91 | + finally {  | 
 | 92 | + | 
 | 93 | + if (data != null)  | 
 | 94 | + data.close();  | 
 | 95 | + | 
 | 96 | + if (commandMaster != null)  | 
 | 97 | + commandMaster.close();  | 
 | 98 | + if (commandSlave != null)  | 
 | 99 | + commandSlave.close();  | 
 | 100 | + | 
 | 101 | + if (connectionMaster != null)  | 
 | 102 | + connectionMaster.close();  | 
 | 103 | + if (connectionSlave != null)  | 
 | 104 | + connectionSlave.close();  | 
 | 105 | + | 
 | 106 | + //Stop S2  | 
 | 107 | + s2.stop();  | 
 | 108 | + System.out.println("S2 stopped!");  | 
 | 109 | + | 
 | 110 | + //Stop S1  | 
 | 111 | + s1.stop();  | 
 | 112 | + System.out.println("S1 stopped!");  | 
 | 113 | + }  | 
 | 114 | + }  | 
 | 115 | + | 
 | 116 | + private static void initAndStarServer(MySQLServer s) throws IOException, InterruptedException {  | 
 | 117 | + | 
 | 118 | + //Server instance initialize  | 
 | 119 | + if (s.init() == 0) {  | 
 | 120 | + System.out.println("Server instance initialized!");  | 
 | 121 | + }  | 
 | 122 | + | 
 | 123 | + //Start S in a new thread  | 
 | 124 | + new Thread(new RunnableServer(s)).start();  | 
 | 125 | + }  | 
 | 126 | + | 
 | 127 | + /**  | 
 | 128 | + * Method to run the replication test  | 
 | 129 | + *   | 
 | 130 | + * @throws SQLException  | 
 | 131 | + * @throws InterruptedException  | 
 | 132 | + */  | 
 | 133 | + private static void testReplication() throws SQLException, InterruptedException {  | 
 | 134 | + | 
 | 135 | + System.out.println("\nTEST Replication");  | 
 | 136 | + | 
 | 137 | + try {  | 
 | 138 | + //Check that the table exists in the slave (S2)  | 
 | 139 | + executeQuery(commandSlave, "DESCRIBE " + USER_TABLE + ";", Kind.SLAVE);  | 
 | 140 | + }catch(Exception e) {  | 
 | 141 | + System.err.println(e.getMessage());  | 
 | 142 | + }  | 
 | 143 | + | 
 | 144 | + //Create a table in the master (S1)  | 
 | 145 | + String command = "CREATE TABLE " + USER_TABLE + " (ID INT NOT NULL, NAME VARCHAR(20), LASTNAME VARCHAR(20));";  | 
 | 146 | + commandMaster.execute(command);  | 
 | 147 | + System.out.println("[" + Kind.MASTER.getName() + "]# " + command);  | 
 | 148 | + | 
 | 149 | + //Check that the table exists in the slave (S1)  | 
 | 150 | + executeQuery(commandMaster, "DESCRIBE " + USER_TABLE + ";", Kind.MASTER);  | 
 | 151 | + | 
 | 152 | + //Wait 2 seconds (for replication)  | 
 | 153 | + Thread.sleep(2000);  | 
 | 154 | + | 
 | 155 | + //Check that the table exists in the slave (S2)  | 
 | 156 | + executeQuery(commandSlave, "DESCRIBE " + USER_TABLE + ";", Kind.SLAVE);  | 
 | 157 | + }  | 
 | 158 | + | 
 | 159 | + /**  | 
 | 160 | + * Method to execute queries  | 
 | 161 | + *   | 
 | 162 | + * @param command  | 
 | 163 | + * @param script  | 
 | 164 | + * @param kind  | 
 | 165 | + * @throws SQLException  | 
 | 166 | + */  | 
 | 167 | + private static void executeQuery(Statement command, String script, Kind kind) throws SQLException {  | 
 | 168 | + System.out.println("\n[" + kind.getName() + "]# " + script);  | 
 | 169 | + data = command.executeQuery(script);  | 
 | 170 | + | 
 | 171 | + while (data.next()) {  | 
 | 172 | + System.out.println(data.getString(1) + " " + data.getString(2) + " " + data.getString(3));  | 
 | 173 | + }  | 
 | 174 | + System.out.println("*****************************");  | 
 | 175 | + }  | 
 | 176 | + | 
 | 177 | + private enum Kind {  | 
 | 178 | + MASTER("Master"), SLAVE("Slave");  | 
 | 179 | + | 
 | 180 | + private String name;  | 
 | 181 | + private Kind(String name) {  | 
 | 182 | + this.name = name;  | 
 | 183 | + }  | 
 | 184 | + public String getName() {  | 
 | 185 | + return name;  | 
 | 186 | + }  | 
 | 187 | + }  | 
 | 188 | +}  | 
0 commit comments