LogThread.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.yihu.ehr.thread;
  2. import com.yihu.ehr.config.ThreadConfig;
  3. import com.yihu.ehr.dbhelper.jdbc.DBHelper;
  4. import com.yihu.ehr.util.HttpsClientUtil;
  5. import com.yihu.ehr.util.ZipUtil;
  6. import com.yihu.ehr.util.log.LogUtil;
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.message.BasicNameValuePair;
  9. import org.springframework.util.StringUtils;
  10. import org.springframework.web.client.RestTemplate;
  11. import java.io.File;
  12. import java.util.*;
  13. /**
  14. * Created by chenweida on 2016/2/27.
  15. * 日志线程,定时把生成的日志发送出去
  16. */
  17. public class LogThread implements Runnable {
  18. private String orgCode = "";
  19. private String systemCode = "";
  20. private int sleepTime = 60 * 1000;
  21. private DBHelper db = new DBHelper();
  22. private String logFileName = "esb_mini.zip";
  23. private String token;
  24. @Override
  25. public void run() {
  26. while (ThreadManage.logIsRunning) {
  27. try {
  28. token = HttpsClientUtil.getToken();
  29. if (!StringUtils.isEmpty(token)) {
  30. initParam();
  31. //判断该机构是否需要上传日志
  32. if (isUpload()) {
  33. //初始化参数
  34. LogUtil.info("-----------日志开始上传------------token:" + token);
  35. //得到日志文件
  36. String file = getLogFile();
  37. if (!StringUtils.isEmpty(file)) {
  38. //发送日志文件
  39. sendLogFile(file);
  40. //删除日志文件
  41. //deleteLogFile(file);
  42. }
  43. }
  44. }
  45. } catch (Exception e) {
  46. LogUtil.error("-----------日志采集失败------------:" + e.getMessage());
  47. } finally {
  48. //睡眠
  49. try {
  50. sleep();
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56. }
  57. private void initParam() {
  58. if (StringUtils.isEmpty(orgCode)) {
  59. try {
  60. List<org.json.JSONObject> listORG = db.query("select * from system_param where param_key='ORG_CODE'");
  61. List<org.json.JSONObject> listSYSTEM = db.query("select * from system_param where param_key='SYSTEM_CODE'");
  62. if (listORG != null && listORG.size() > 0) {
  63. orgCode = listORG.get(0).getString("param_value");
  64. }
  65. if (listSYSTEM != null && listSYSTEM.size() > 0) {
  66. systemCode = listSYSTEM.get(0).getString("param_value");
  67. } else {
  68. String sql = "insert into system_param (id,param_key,param_value) values " +
  69. "('" + UUID.randomUUID() + "'," +
  70. " 'SYSTEM_CODE' ," +
  71. " '" + ThreadConfig.SYSTEM_CODE + "'" +
  72. ")";
  73. db.execute(sql);
  74. }
  75. } catch (Exception e) {
  76. LogUtil.error("初始化参数失败:" + e.getMessage());
  77. }
  78. }
  79. LogUtil.info("初始化参数成功orgCode:" + orgCode + "---systemCode:" + systemCode);
  80. }
  81. private boolean sendLogFile(String file) {
  82. try {
  83. //String booleanString =HttpClientUtil.sendFile(file, ThreadConfig.getURL(ThreadConfig.LOG_THREAD_UPLOAD), ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD);
  84. String token = HttpsClientUtil.getToken();
  85. List<NameValuePair> formParams = new ArrayList<NameValuePair>();
  86. formParams.add(new BasicNameValuePair("ip", ""));
  87. formParams.add(new BasicNameValuePair("access_token", token));
  88. formParams.add(new BasicNameValuePair("orgCode", orgCode));
  89. String booleanString = HttpsClientUtil.postFile(ThreadConfig.SERVICE_URL + ThreadConfig.LOG_THREAD_UPLOAD, file, formParams, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD);
  90. LogUtil.info("日志发送成功:" + booleanString);
  91. return true;
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. LogUtil.error("日志发送失败:");
  95. return false;
  96. }
  97. }
  98. private void deleteLogFile(File file) {
  99. String filePath = file.getAbsolutePath();
  100. if (file.delete()) {
  101. LogUtil.info("日志删除成功");
  102. } else {
  103. LogUtil.error("日志删除失败,日志路径:" + file.getAbsolutePath());
  104. }
  105. }
  106. public String getLogFile() {
  107. String home = System.getProperty("catalina.home").replace('\\', '/');
  108. String srcPath = home.substring(0, home.lastIndexOf('/') + 1) + "log4j";
  109. String filePath = srcPath + File.separator + logFileName;
  110. ZipUtil.zip(srcPath, filePath);
  111. LogUtil.info("日志文件路径:" + filePath);
  112. File file = new File(filePath);
  113. if (file.exists()) {
  114. LogUtil.info("-----------得到日志成功------------");
  115. return filePath;
  116. } else {
  117. LogUtil.error("-----------得到日志失败------------");
  118. return "";
  119. }
  120. }
  121. private void sleep() throws Exception {
  122. LogUtil.info("日志线程开始睡眠,睡眠时间(分钟):" + ThreadConfig.LOG_THREAD_SLEEP_TIME);
  123. Thread.sleep(sleepTime * ThreadConfig.SQL_THREAD_SLEEP_TIME);
  124. }
  125. public static void main(String[] args) {
  126. RestTemplate r = new RestTemplate();
  127. }
  128. public boolean isUpload() {
  129. try {
  130. Map<String, Object> params = new HashMap<String, Object>();
  131. params.put("orgCode", orgCode);
  132. params.put("systemCode", systemCode);
  133. params.put("access_token", token);
  134. String booleanString = HttpsClientUtil.get(ThreadConfig.SERVICE_URL + ThreadConfig.LOG_THREAD_GETUPLOADFLAG, params, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD);
  135. LogUtil.info("判断日志是否需要上传:" + booleanString);
  136. return Boolean.valueOf(booleanString);
  137. } catch (Exception e) {
  138. LogUtil.error("判断日志是否需要上传失败" + e.getMessage());
  139. return false;
  140. }
  141. }
  142. }