123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- 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<org.json.JSONObject> listORG = db.query("select * from system_param where param_key='ORG_CODE'");
- List<org.json.JSONObject> 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<NameValuePair> formParams = new ArrayList<NameValuePair>();
- 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<String, Object> params = new HashMap<String, Object>();
- 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;
- }
- }
- }
|