package com.yihu.ehr.thread; import com.yihu.ehr.config.ThreadConfig; import com.yihu.ehr.dbhelper.jdbc.DBHelper; import com.yihu.ehr.util.HttpsClientUtil; import com.yihu.ehr.util.ZipUtil; import com.yihu.ehr.util.log.LogUtil; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.springframework.util.StringUtils; import org.springframework.web.client.RestTemplate; import java.io.File; import java.util.*; /** * Created by chenweida on 2016/2/27. * 日志线程,定时把生成的日志发送出去 */ public class LogThread implements Runnable { private String orgCode = ""; private String systemCode = ""; private int sleepTime = 60 * 1000; private DBHelper db = new DBHelper(); private String logFileName = "esb_mini.zip"; private String token; @Override public void run() { while (ThreadManage.logIsRunning) { try { token = HttpsClientUtil.getToken(); if (!StringUtils.isEmpty(token)) { initParam(); //判断该机构是否需要上传日志 if (isUpload()) { //初始化参数 LogUtil.info("-----------日志开始上传------------token:" + token); //得到日志文件 String file = getLogFile(); if (!StringUtils.isEmpty(file)) { //发送日志文件 sendLogFile(file); //删除日志文件 //deleteLogFile(file); } } } } catch (Exception e) { LogUtil.error("-----------日志采集失败------------:" + e.getMessage()); } finally { //睡眠 try { sleep(); } catch (Exception e) { e.printStackTrace(); } } } } private void initParam() { if (StringUtils.isEmpty(orgCode)) { try { List listORG = db.query("select * from system_param where param_key='ORG_CODE'"); List listSYSTEM = db.query("select * from system_param where param_key='SYSTEM_CODE'"); if (listORG != null && listORG.size() > 0) { orgCode = listORG.get(0).getString("param_value"); } if (listSYSTEM != null && listSYSTEM.size() > 0) { systemCode = listSYSTEM.get(0).getString("param_value"); } else { String sql = "insert into system_param (id,param_key,param_value) values " + "('" + UUID.randomUUID() + "'," + " 'SYSTEM_CODE' ," + " '" + ThreadConfig.SYSTEM_CODE + "'" + ")"; db.execute(sql); } } catch (Exception e) { LogUtil.error("初始化参数失败:" + e.getMessage()); } } LogUtil.info("初始化参数成功orgCode:" + orgCode + "---systemCode:" + systemCode); } private boolean sendLogFile(String file) { try { //String booleanString =HttpClientUtil.sendFile(file, ThreadConfig.getURL(ThreadConfig.LOG_THREAD_UPLOAD), ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD); String token = HttpsClientUtil.getToken(); List formParams = new ArrayList(); formParams.add(new BasicNameValuePair("ip", "")); formParams.add(new BasicNameValuePair("access_token", token)); formParams.add(new BasicNameValuePair("orgCode", orgCode)); String booleanString = HttpsClientUtil.postFile(ThreadConfig.SERVICE_URL + ThreadConfig.LOG_THREAD_UPLOAD, file, formParams, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD); LogUtil.info("日志发送成功:" + booleanString); return true; } catch (Exception e) { e.printStackTrace(); LogUtil.error("日志发送失败:"); return false; } } private void deleteLogFile(File file) { String filePath = file.getAbsolutePath(); if (file.delete()) { LogUtil.info("日志删除成功"); } else { LogUtil.error("日志删除失败,日志路径:" + file.getAbsolutePath()); } } public String getLogFile() { String home = System.getProperty("catalina.home").replace('\\', '/'); String srcPath = home.substring(0, home.lastIndexOf('/') + 1) + "log4j"; String filePath = srcPath + File.separator + logFileName; ZipUtil.zip(srcPath, filePath); LogUtil.info("日志文件路径:" + filePath); File file = new File(filePath); if (file.exists()) { LogUtil.info("-----------得到日志成功------------"); return filePath; } else { LogUtil.error("-----------得到日志失败------------"); return ""; } } private void sleep() throws Exception { LogUtil.info("日志线程开始睡眠,睡眠时间(分钟):" + ThreadConfig.LOG_THREAD_SLEEP_TIME); Thread.sleep(sleepTime * ThreadConfig.SQL_THREAD_SLEEP_TIME); } public static void main(String[] args) { RestTemplate r = new RestTemplate(); } public boolean isUpload() { try { Map params = new HashMap(); params.put("orgCode", orgCode); params.put("systemCode", systemCode); params.put("access_token", token); String booleanString = HttpsClientUtil.get(ThreadConfig.SERVICE_URL + ThreadConfig.LOG_THREAD_GETUPLOADFLAG, params, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD); LogUtil.info("判断日志是否需要上传:" + booleanString); return Boolean.valueOf(booleanString); } catch (Exception e) { LogUtil.error("判断日志是否需要上传失败" + e.getMessage()); return false; } } }