Guest User

Untitled

a guest
Jun 1st, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. package com.ss.ss1.ss2.ss3;
  2.  
  3. import java.sql.*;
  4. import java.util.Properties;
  5. import java.io.*;
  6.  
  7. /**
  8. * This class is used to execute commands on mysql server
  9. */
  10. public class MysqlCommandExecutor implements Runnable {
  11.  
  12. public static final String EXECUTE_COMMAND = "SHOW STATUS";
  13. public static final String DEFAULT_DB_SERVER = "jdbc:mysql://db1.sermo.red:3306/";
  14. public static final String DEFAULT_USERNAME = "sermo";
  15. public static final String DEFAULT_PASS = "SuperSermo!";
  16.  
  17. private String executeCommand = EXECUTE_COMMAND;
  18. private boolean repeat = Boolean.TRUE;
  19. private String dbServer = DEFAULT_DB_SERVER;
  20. private String userName = DEFAULT_USERNAME;
  21. private String userPass = DEFAULT_PASS;
  22. private GraphComponent component;
  23. private boolean consoleOutput;
  24.  
  25. public MysqlCommandExecutor() {
  26. super();
  27. }
  28.  
  29. public void run() {
  30. execute();
  31. }
  32.  
  33. protected void execute() {
  34. FileWriter fileWriter = null;
  35. try {
  36. Class.forName("com.mysql.jdbc.Driver");
  37.  
  38. File fileToSave = new File(getFileNameToSave());
  39. try {
  40. fileWriter = new FileWriter(fileToSave, true);
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44.  
  45. Connection connection = DriverManager.getConnection(getDbServer(), getUserName(), getUserPass());
  46. Statement statement = connection.createStatement();
  47. while (isRepeat()) {
  48. ResultSet result = statement.executeQuery(getExecuteCommand());
  49. while (result.next()) {
  50. if (result.getString(1).trim().startsWith("Connections")) {
  51. if (!isConsoleOutput()) {
  52. getComponent().bindValue(result.getString(2));
  53. } else {
  54. if (fileWriter != null) {
  55. fileWriter.write(result.getString(2) + ",");
  56. fileWriter.flush();
  57. }
  58. }
  59. }
  60. }
  61. try {
  62. Thread.sleep(5000);
  63. } catch (InterruptedException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67.  
  68. } catch (ClassNotFoundException e) {
  69. e.printStackTrace();
  70. } catch (SQLException e) {
  71. e.printStackTrace();
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75. }
  76.  
  77. protected String getFileNameToSave() throws IOException {
  78. Properties props = new Properties();
  79. props.load(new FileInputStream("./config/monitor.properties"));
  80. return (String) props.get("com.sermo.servermon.mysqlstat.fileToSave");
  81. }
  82.  
  83. public MysqlCommandExecutor(String executeCommand) {
  84. this.executeCommand = executeCommand;
  85. }
  86.  
  87. public String getExecuteCommand() {
  88. return executeCommand;
  89. }
  90.  
  91. public void setExecuteCommand(String executeCommand) {
  92. this.executeCommand = executeCommand;
  93. }
  94.  
  95. public boolean isRepeat() {
  96. return repeat;
  97. }
  98.  
  99. public void setRepeat(boolean repeat) {
  100. this.repeat = repeat;
  101. }
  102.  
  103. public String getDbServer() {
  104. return dbServer;
  105. }
  106.  
  107. public void setDbServer(String dbServer) {
  108. this.dbServer = dbServer;
  109. }
  110.  
  111. public String getUserName() {
  112. return userName;
  113. }
  114.  
  115. public void setUserName(String userName) {
  116. this.userName = userName;
  117. }
  118.  
  119. public String getUserPass() {
  120. return userPass;
  121. }
  122.  
  123. public void setUserPass(String userPass) {
  124. this.userPass = userPass;
  125. }
  126.  
  127. public GraphComponent getComponent() {
  128. return component;
  129. }
  130.  
  131. public void setComponent(GraphComponent component) {
  132. this.component = component;
  133. }
  134.  
  135. public boolean isConsoleOutput() {
  136. return consoleOutput;
  137. }
  138.  
  139. public void setConsoleOutput(boolean consoleOutput) {
  140. this.consoleOutput = consoleOutput;
  141. }
  142. }
Add Comment
Please, Sign In to add comment