Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ss.ss1.ss2.ss3;
- import java.sql.*;
- import java.util.Properties;
- import java.io.*;
- /**
- * This class is used to execute commands on mysql server
- */
- public class MysqlCommandExecutor implements Runnable {
- public static final String EXECUTE_COMMAND = "SHOW STATUS";
- public static final String DEFAULT_DB_SERVER = "jdbc:mysql://db1.sermo.red:3306/";
- public static final String DEFAULT_USERNAME = "sermo";
- public static final String DEFAULT_PASS = "SuperSermo!";
- private String executeCommand = EXECUTE_COMMAND;
- private boolean repeat = Boolean.TRUE;
- private String dbServer = DEFAULT_DB_SERVER;
- private String userName = DEFAULT_USERNAME;
- private String userPass = DEFAULT_PASS;
- private GraphComponent component;
- private boolean consoleOutput;
- public MysqlCommandExecutor() {
- super();
- }
- public void run() {
- execute();
- }
- protected void execute() {
- FileWriter fileWriter = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- File fileToSave = new File(getFileNameToSave());
- try {
- fileWriter = new FileWriter(fileToSave, true);
- } catch (IOException e) {
- e.printStackTrace();
- }
- Connection connection = DriverManager.getConnection(getDbServer(), getUserName(), getUserPass());
- Statement statement = connection.createStatement();
- while (isRepeat()) {
- ResultSet result = statement.executeQuery(getExecuteCommand());
- while (result.next()) {
- if (result.getString(1).trim().startsWith("Connections")) {
- if (!isConsoleOutput()) {
- getComponent().bindValue(result.getString(2));
- } else {
- if (fileWriter != null) {
- fileWriter.write(result.getString(2) + ",");
- fileWriter.flush();
- }
- }
- }
- }
- try {
- Thread.sleep(5000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- protected String getFileNameToSave() throws IOException {
- Properties props = new Properties();
- props.load(new FileInputStream("./config/monitor.properties"));
- return (String) props.get("com.sermo.servermon.mysqlstat.fileToSave");
- }
- public MysqlCommandExecutor(String executeCommand) {
- this.executeCommand = executeCommand;
- }
- public String getExecuteCommand() {
- return executeCommand;
- }
- public void setExecuteCommand(String executeCommand) {
- this.executeCommand = executeCommand;
- }
- public boolean isRepeat() {
- return repeat;
- }
- public void setRepeat(boolean repeat) {
- this.repeat = repeat;
- }
- public String getDbServer() {
- return dbServer;
- }
- public void setDbServer(String dbServer) {
- this.dbServer = dbServer;
- }
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public String getUserPass() {
- return userPass;
- }
- public void setUserPass(String userPass) {
- this.userPass = userPass;
- }
- public GraphComponent getComponent() {
- return component;
- }
- public void setComponent(GraphComponent component) {
- this.component = component;
- }
- public boolean isConsoleOutput() {
- return consoleOutput;
- }
- public void setConsoleOutput(boolean consoleOutput) {
- this.consoleOutput = consoleOutput;
- }
- }
Add Comment
Please, Sign In to add comment