Commit fcbe0ba3 authored by 张俊's avatar 张俊

优化日志输出

parent 41eb2865
...@@ -4,6 +4,8 @@ package net.cdkj.gjj.adapter.controller; ...@@ -4,6 +4,8 @@ package net.cdkj.gjj.adapter.controller;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import net.cdkj.gjj.adapter.domain.Json; import net.cdkj.gjj.adapter.domain.Json;
import net.cdkj.gjj.adapter.domain.TimeExpiredPoolCache; import net.cdkj.gjj.adapter.domain.TimeExpiredPoolCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
...@@ -15,27 +17,31 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -15,27 +17,31 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping(value = "Cache") @RequestMapping(value = "Cache")
public class CacheDemoController { public class CacheDemoController {
private static final Logger logger = LoggerFactory.getLogger(CacheDemoController.class);
/** /**
* 将从第三方拿到的token值存入java内存中,并设置对应的过期时间 * 将从第三方拿到的token值存入java内存中,并设置对应的过期时间
*
* @return * @return
*/ */
@RequestMapping(value = "/saveToken", method = RequestMethod.GET) @RequestMapping(value = "/saveToken", method = RequestMethod.GET)
public static String saveToken() { public static String saveToken() {
try { try {
//1.调用鉴权认证接口获取token值和过期时间的json字符串 // 1.调用鉴权认证接口获取token值和过期时间的json字符串
String str = TokenAcquisitionController.TokenAcquisition(); String str = TokenAcquisitionController.TokenAcquisition();
Json jsonentity = JSONObject.parseObject(str, Json.class); Json jsonentity = JSONObject.parseObject(str, Json.class);
if(jsonentity!=null){ if (jsonentity != null) {
if(jsonentity.isResult()){ if (jsonentity.isResult()) {
boolean result=jsonentity.isResult(); boolean result = jsonentity.isResult();
String access_token=jsonentity.getAccess_token(); String access_token = jsonentity.getAccess_token();
Long expires_in=jsonentity.getExpires_in(); Long expires_in = jsonentity.getExpires_in();
System.out.println("result:"+result+","+"token值:"+access_token+","+"有效时间:"+expires_in); logger.info("result:" + result + "," + "token值:" + access_token + "," + "有效时间:" + expires_in);
TimeExpiredPoolCache.getInstance().put("access_token",access_token, expires_in*1000L); TimeExpiredPoolCache.getInstance().put("access_token", access_token, expires_in * 1000L);
} }
} }
return "保存token进入缓冲成功"; return "保存token进入缓冲成功";
} catch(Exception e){ } catch (Exception e) {
return "保存token进入缓存失败"; return "保存token进入缓存失败";
} }
} }
...@@ -43,17 +49,18 @@ public class CacheDemoController { ...@@ -43,17 +49,18 @@ public class CacheDemoController {
/** /**
* 获取存入java内存中的token值 * 获取存入java内存中的token值
*
* @return * @return
*/ */
@RequestMapping(value = "/getToken", method = RequestMethod.GET) @RequestMapping(value = "/getToken", method = RequestMethod.GET)
public static String getToken(){ public static String getToken() {
try { try {
String token = TimeExpiredPoolCache.getInstance().get("access_token"); String token = TimeExpiredPoolCache.getInstance().get("access_token");
if (null!=token){ if (null != token) {
return token; return token;
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.error("{}", e);
} }
return null; return null;
} }
......
...@@ -5,6 +5,8 @@ import com.alibaba.fastjson.JSONObject; ...@@ -5,6 +5,8 @@ import com.alibaba.fastjson.JSONObject;
import net.cdkj.gjj.adapter.domain.HttpUtil; import net.cdkj.gjj.adapter.domain.HttpUtil;
import net.cdkj.gjj.adapter.domain.PropertyUtil; import net.cdkj.gjj.adapter.domain.PropertyUtil;
import net.cdkj.gjj.adapter.domain.UnitAccountOpeningInformation; import net.cdkj.gjj.adapter.domain.UnitAccountOpeningInformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
...@@ -14,7 +16,6 @@ import javax.servlet.http.HttpServletRequest; ...@@ -14,7 +16,6 @@ import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Logger;
import static net.cdkj.gjj.adapter.domain.GzipUtil.unzipString; import static net.cdkj.gjj.adapter.domain.GzipUtil.unzipString;
import static net.cdkj.gjj.adapter.domain.GzipUtil.zipString; import static net.cdkj.gjj.adapter.domain.GzipUtil.zipString;
...@@ -28,7 +29,7 @@ public class ProvidentFundServicesController { ...@@ -28,7 +29,7 @@ public class ProvidentFundServicesController {
// log4j 日志实现 // log4j 日志实现
private static final Logger logger = Logger.getLogger(ProvidentFundServicesController.class.toString()); private static final Logger logger = LoggerFactory.getLogger(ProvidentFundServicesController.class);
/** /**
* 拉取单位开户数据接口 * 拉取单位开户数据接口
...@@ -42,7 +43,6 @@ public class ProvidentFundServicesController { ...@@ -42,7 +43,6 @@ public class ProvidentFundServicesController {
// 调用获取token的方法 // 调用获取token的方法
String token = huoqutoken(); String token = huoqutoken();
String provifunserviurl = PropertyUtil.getPropValue(prop, "provifunserviurl") + token; String provifunserviurl = PropertyUtil.getPropValue(prop, "provifunserviurl") + token;
System.out.println(provifunserviurl);
logger.info("单位开户拉取url:" + provifunserviurl); logger.info("单位开户拉取url:" + provifunserviurl);
// 发起 post 请求,并对出参进行解压缩 // 发起 post 请求,并对出参进行解压缩
return HttpUtil.sendPost(provifunserviurl, data); return HttpUtil.sendPost(provifunserviurl, data);
...@@ -143,9 +143,9 @@ public class ProvidentFundServicesController { ...@@ -143,9 +143,9 @@ public class ProvidentFundServicesController {
public String ProvidentFundServices2(HttpServletRequest request) { public String ProvidentFundServices2(HttpServletRequest request) {
// 获取请求入参 // 获取请求入参
String str1 = HttpUtil.getReqData(request); String str1 = HttpUtil.getReqData(request);
System.out.println("第三方接收的公积金服务接口参数解压之前" + str1); logger.info("第三方接收的公积金服务接口参数解压之前" + str1);
String str2 = unzipString(str1);// 解压缩 String str2 = unzipString(str1);// 解压缩
System.out.println("第三方接收的公积金服务接口参数解压之后" + str2); logger.info("第三方接收的公积金服务接口参数解压之后" + str2);
//----------------------------------------------------- //-----------------------------------------------------
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("code", 0); jsonObject.put("code", 0);
......
...@@ -4,6 +4,8 @@ package net.cdkj.gjj.adapter.controller; ...@@ -4,6 +4,8 @@ package net.cdkj.gjj.adapter.controller;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import net.cdkj.gjj.adapter.domain.HttpUtil; import net.cdkj.gjj.adapter.domain.HttpUtil;
import net.cdkj.gjj.adapter.domain.PropertyUtil; import net.cdkj.gjj.adapter.domain.PropertyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
...@@ -22,6 +24,9 @@ import static net.cdkj.gjj.adapter.domain.GzipUtil.zipString; ...@@ -22,6 +24,9 @@ import static net.cdkj.gjj.adapter.domain.GzipUtil.zipString;
@RequestMapping(value = "FrontEndProcessor") @RequestMapping(value = "FrontEndProcessor")
public class TokenAcquisitionController { public class TokenAcquisitionController {
private static final Logger logger = LoggerFactory.getLogger(TokenAcquisitionController.class);
/** /**
* 测试静态方法读取配置文件属性值方法 * 测试静态方法读取配置文件属性值方法
*/ */
...@@ -29,7 +34,7 @@ public class TokenAcquisitionController { ...@@ -29,7 +34,7 @@ public class TokenAcquisitionController {
@PostMapping("test") @PostMapping("test")
public static void test() { public static void test() {
Properties prop = PropertyUtil.getConfig("application.properties"); Properties prop = PropertyUtil.getConfig("application.properties");
System.out.println(PropertyUtil.getPropValue(prop, "tokenurl")); logger.info(PropertyUtil.getPropValue(prop, "tokenurl"));
} }
/** /**
...@@ -48,7 +53,7 @@ public class TokenAcquisitionController { ...@@ -48,7 +53,7 @@ public class TokenAcquisitionController {
jsonObject.put("grant_type", "ip"); jsonObject.put("grant_type", "ip");
jsonObject.put("userid", "gjj"); jsonObject.put("userid", "gjj");
String str = jsonObject.toString(); String str = jsonObject.toString();
System.out.println("鉴权参数zip压缩处理之前要发送给第三方的报文:" + str); logger.info("鉴权参数zip压缩处理之前要发送给第三方的报文:" + str);
String tokenurl = PropertyUtil.getPropValue(prop, "tokenurl"); String tokenurl = PropertyUtil.getPropValue(prop, "tokenurl");
return HttpUtil.sendPost(tokenurl, str); return HttpUtil.sendPost(tokenurl, str);
} }
...@@ -87,10 +92,10 @@ public class TokenAcquisitionController { ...@@ -87,10 +92,10 @@ public class TokenAcquisitionController {
jsonObject.put("grant_type", "192.168.101.34"); jsonObject.put("grant_type", "192.168.101.34");
jsonObject.put("userid", "gjj"); jsonObject.put("userid", "gjj");
String str = jsonObject.toString(); String str = jsonObject.toString();
System.out.println("未经过压缩处理的json字符串:" + str); logger.info("未经过压缩处理的json字符串:" + str);
String s = zipString(str);// 进行zip压缩 String s = zipString(str);// 进行zip压缩
System.out.println("压缩处理之后的json字符串:" + s); logger.info("压缩处理之后的json字符串:" + s);
String s1 = unzipString(s);// 进行zip解压缩 String s1 = unzipString(s);// 进行zip解压缩
System.out.println("解压缩处理之后的json字符串:" + s1); logger.info("解压缩处理之后的json字符串:" + s1);
} }
} }
package net.cdkj.gjj.adapter.domain; package net.cdkj.gjj.adapter.domain;
import net.cdkj.gjj.adapter.controller.CacheDemoController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
...@@ -12,8 +15,13 @@ import java.util.zip.Inflater; ...@@ -12,8 +15,13 @@ import java.util.zip.Inflater;
* json字符串zip压缩,解压缩工具类 * json字符串zip压缩,解压缩工具类
*/ */
public class GzipUtil { public class GzipUtil {
private static final Logger logger = LoggerFactory.getLogger(CacheDemoController.class);
/** /**
* 压缩 * 压缩
*
* @param unzip * @param unzip
* @return * @return
*/ */
...@@ -23,6 +31,7 @@ public class GzipUtil { ...@@ -23,6 +31,7 @@ public class GzipUtil {
/** /**
* 对cpspJson进行解压 * 对cpspJson进行解压
*
* @param zip * @param zip
* @return * @return
*/ */
...@@ -30,13 +39,13 @@ public class GzipUtil { ...@@ -30,13 +39,13 @@ public class GzipUtil {
try { try {
byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(zip); byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(zip);
String unziqStr = unzipByte(decode); String unziqStr = unzipByte(decode);
//如果解压缩失败,返回原数据 // 如果解压缩失败,返回原数据
if (StringUtils.isEmpty(unziqStr)){ if (StringUtils.isEmpty(unziqStr)) {
return zip; return zip;
} }
return unziqStr; return unziqStr;
} catch (IOException ex) { } catch (IOException ex) {
System.out.println("解压文件失败"+ex); logger.error("解压文件失败" + ex);
return zip; return zip;
} }
} }
...@@ -47,12 +56,14 @@ public class GzipUtil { ...@@ -47,12 +56,14 @@ public class GzipUtil {
byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(zip); byte[] decode = new sun.misc.BASE64Decoder().decodeBuffer(zip);
return unzipByte(decode); return unzipByte(decode);
} catch (IOException ex) { } catch (IOException ex) {
System.out.println("解压文件失败"+ex); logger.error("解压文件失败" + ex);
return null; return null;
} }
} }
/** /**
* 压缩 * 压缩
*
* @param unzip * @param unzip
* @return * @return
*/ */
...@@ -87,7 +98,7 @@ public class GzipUtil { ...@@ -87,7 +98,7 @@ public class GzipUtil {
outputStream.write(bytes, 0, length); outputStream.write(bytes, 0, length);
} }
} catch (DataFormatException e) { } catch (DataFormatException e) {
System.out.println("解压缩失败"+e); logger.error("解压缩失败" + e);
return null; return null;
} finally { } finally {
inflater.end(); inflater.end();
......
package net.cdkj.gjj.adapter.domain; package net.cdkj.gjj.adapter.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
...@@ -9,6 +12,7 @@ import java.util.zip.GZIPInputStream; ...@@ -9,6 +12,7 @@ import java.util.zip.GZIPInputStream;
public class HttpUtil { public class HttpUtil {
private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
/** /**
* 获取请求入参数据 * 获取请求入参数据
...@@ -22,10 +26,10 @@ public class HttpUtil { ...@@ -22,10 +26,10 @@ public class HttpUtil {
json.append(line); json.append(line);
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println(e); logger.error("{}", e);
} }
String str = json.toString(); String str = json.toString();
System.out.println("要发送给第三方的公积金系统服务报文:" + str); logger.info("要发送给第三方的公积金系统服务报文:" + str);
return str; return str;
} }
...@@ -78,14 +82,13 @@ public class HttpUtil { ...@@ -78,14 +82,13 @@ public class HttpUtil {
while ((temp = br.readLine()) != null) { while ((temp = br.readLine()) != null) {
sb.append(temp); sb.append(temp);
} }
System.out.println("第三方返回的zip解压缩之后的报文:" + sb); logger.info("第三方返回的zip解压缩之后的报文:" + sb);
return sb.toString(); return sb.toString();
} else { } else {
System.out.println("请求失败!!!"); logger.info("请求失败!!!");
} }
} catch ( } catch (IOException e) {
IOException e) { logger.error("{}", e);
e.printStackTrace();
} finally { } finally {
if (out != null) { if (out != null) {
try { try {
......
package net.cdkj.gjj.adapter.domain; package net.cdkj.gjj.adapter.domain;
import net.cdkj.gjj.adapter.controller.CacheDemoController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
...@@ -8,43 +13,48 @@ import java.util.Properties; ...@@ -8,43 +13,48 @@ import java.util.Properties;
* 获取配置文件application.properties里的属性值所用到的工具类 * 获取配置文件application.properties里的属性值所用到的工具类
*/ */
public class PropertyUtil { public class PropertyUtil {
private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
/** /**
* 读取 classpath 下 指定的properties配置文件,加载到Properties并返回Properties * 读取 classpath 下 指定的properties配置文件,加载到Properties并返回Properties
*
* @param name 配置文件名,如:mongo.properties * @param name 配置文件名,如:mongo.properties
* @return * @return
*/ */
public static Properties getConfig(String name){ public static Properties getConfig(String name) {
Properties props=null; Properties props = null;
try{ try {
props = new Properties(); props = new Properties();
InputStream in = PropertyUtil.class.getClassLoader().getResourceAsStream(name); InputStream in = PropertyUtil.class.getClassLoader().getResourceAsStream(name);
BufferedReader bf = new BufferedReader(new InputStreamReader(in)); BufferedReader bf = new BufferedReader(new InputStreamReader(in));
props.load(bf); props.load(bf);
in.close(); in.close();
}catch(Exception ex){ } catch (Exception e) {
ex.printStackTrace(); logger.error("{}", e);
} }
return props; return props;
} }
public static String getPropValue(Properties prop,String key){ public static String getPropValue(Properties prop, String key) {
if(key == null || "".equals(key.trim())){ if (key == null || "".equals(key.trim())) {
return null; return null;
} }
String value = prop.getProperty(key); String value = prop.getProperty(key);
if(value == null){ if (value == null) {
return null; return null;
} }
value = value.trim(); value = value.trim();
//判断是否是环境变量配置属性,例如 server.env=${serverEnv:local} // 判断是否是环境变量配置属性,例如 server.env=${serverEnv:local}
if(value.startsWith("${") && value.endsWith("}") && value.contains(":")){ if (value.startsWith("${") && value.endsWith("}") && value.contains(":")) {
int indexOfColon = value.indexOf(":"); int indexOfColon = value.indexOf(":");
String envName = value.substring(2,indexOfColon); String envName = value.substring(2, indexOfColon);
//获取系统环境变量 envName 的内容,如果没有找到,则返回defaultValue // 获取系统环境变量 envName 的内容,如果没有找到,则返回defaultValue
String envValue = System.getenv(envName); String envValue = System.getenv(envName);
if(envValue == null){ if (envValue == null) {
//配置的默认值 // 配置的默认值
return value.substring(indexOfColon+1,value.length()-1); return value.substring(indexOfColon + 1, value.length() - 1);
} }
return envValue; return envValue;
} }
......
...@@ -11,8 +11,8 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -11,8 +11,8 @@ import java.util.concurrent.ConcurrentHashMap;
* Cache缓存类 * Cache缓存类
*/ */
public class TimeExpiredPoolCache { public class TimeExpiredPoolCache {
private static long defaultCachedMillis = 30 * 1000L;//过期时间默认10秒 private static long defaultCachedMillis = 30 * 1000L;// 过期时间默认10秒
private static long timerMillis = 30 * 1000L;//定时清理默认10秒 private static long timerMillis = 30 * 1000L;// 定时清理默认10秒
/** /**
* 对象池 * 对象池
*/ */
...@@ -21,25 +21,30 @@ public class TimeExpiredPoolCache { ...@@ -21,25 +21,30 @@ public class TimeExpiredPoolCache {
* 对象单例 * 对象单例
*/ */
private static TimeExpiredPoolCache instance = null; private static TimeExpiredPoolCache instance = null;
private TimeExpiredPoolCache() { private TimeExpiredPoolCache() {
dataPool = new ConcurrentHashMap<String, DataWrapper<?>>(); dataPool = new ConcurrentHashMap<String, DataWrapper<?>>();
} }
private static synchronized void syncInit() { private static synchronized void syncInit() {
if (instance == null) { if (instance == null) {
instance = new TimeExpiredPoolCache(); instance = new TimeExpiredPoolCache();
initTimer(); initTimer();
} }
} }
public static TimeExpiredPoolCache getInstance() { public static TimeExpiredPoolCache getInstance() {
if (instance == null) { if (instance == null) {
syncInit(); syncInit();
} }
return instance; return instance;
} }
/** /**
* 定时器定时清理过期缓存 * 定时器定时清理过期缓存
*/ */
private static Timer timer = new Timer(); private static Timer timer = new Timer();
private static void initTimer() { private static void initTimer() {
timer.scheduleAtFixedRate(new TimerTask() { timer.scheduleAtFixedRate(new TimerTask() {
@Override @Override
...@@ -47,7 +52,7 @@ public class TimeExpiredPoolCache { ...@@ -47,7 +52,7 @@ public class TimeExpiredPoolCache {
try { try {
clearExpiredCaches(); clearExpiredCaches();
} catch (Exception e) { } catch (Exception e) {
//logger.error("clearExpiredCaches error.", e); // logger.error("clearExpiredCaches error.", e);
} }
} }
}, timerMillis, timerMillis); }, timerMillis, timerMillis);
...@@ -55,6 +60,7 @@ public class TimeExpiredPoolCache { ...@@ -55,6 +60,7 @@ public class TimeExpiredPoolCache {
/** /**
* 缓存数据 * 缓存数据
*
* @param key key值 * @param key key值
* @param data 缓存数据 * @param data 缓存数据
* @param cachedMillis 过期时间 * @param cachedMillis 过期时间
...@@ -63,16 +69,16 @@ public class TimeExpiredPoolCache { ...@@ -63,16 +69,16 @@ public class TimeExpiredPoolCache {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T put(String key, T data, long cachedMillis, DataRenewer<T> dataRenewer) throws Exception { public <T> T put(String key, T data, long cachedMillis, DataRenewer<T> dataRenewer) throws Exception {
DataWrapper<T> dataWrapper = (DataWrapper<T>)dataPool.get(key); DataWrapper<T> dataWrapper = (DataWrapper<T>) dataPool.get(key);
if (data == null && dataRenewer != null) { if (data == null && dataRenewer != null) {
data = dataRenewer.renewData(); data = dataRenewer.renewData();
} }
//当重新获取数据为空,直接返回不做put // 当重新获取数据为空,直接返回不做put
if (data == null){ if (data == null) {
return null; return null;
} }
if (dataWrapper != null) { if (dataWrapper != null) {
//更新 // 更新
dataWrapper.update(data, cachedMillis); dataWrapper.update(data, cachedMillis);
} else { } else {
dataWrapper = new DataWrapper<T>(data, cachedMillis); dataWrapper = new DataWrapper<T>(data, cachedMillis);
...@@ -80,18 +86,20 @@ public class TimeExpiredPoolCache { ...@@ -80,18 +86,20 @@ public class TimeExpiredPoolCache {
} }
return data; return data;
} }
/** /**
* 直接设置缓存值和时间 * 直接设置缓存值和时间
*
* @param key * @param key
* @param data * @param data
* @param cachedMillis * @param cachedMillis
* @return * @return
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T put(String key, T data, long cachedMillis) throws Exception{ public <T> T put(String key, T data, long cachedMillis) throws Exception {
DataWrapper<T> dataWrapper = (DataWrapper<T>)dataPool.get(key); DataWrapper<T> dataWrapper = (DataWrapper<T>) dataPool.get(key);
if (dataWrapper != null) { if (dataWrapper != null) {
//更新 // 更新
dataWrapper.update(data, cachedMillis); dataWrapper.update(data, cachedMillis);
} else { } else {
dataWrapper = new DataWrapper<T>(data, cachedMillis); dataWrapper = new DataWrapper<T>(data, cachedMillis);
...@@ -99,8 +107,10 @@ public class TimeExpiredPoolCache { ...@@ -99,8 +107,10 @@ public class TimeExpiredPoolCache {
} }
return data; return data;
} }
/** /**
* 默认构造时间的缓存数据 * 默认构造时间的缓存数据
*
* @param key * @param key
* @param data * @param data
* @param dataRenewer * @param dataRenewer
...@@ -110,8 +120,10 @@ public class TimeExpiredPoolCache { ...@@ -110,8 +120,10 @@ public class TimeExpiredPoolCache {
public <T> T put(String key, T data, DataRenewer<T> dataRenewer) throws Exception { public <T> T put(String key, T data, DataRenewer<T> dataRenewer) throws Exception {
return put(key, data, defaultCachedMillis, dataRenewer); return put(key, data, defaultCachedMillis, dataRenewer);
} }
/** /**
* 获取缓存 * 获取缓存
*
* @param key * @param key
* @param cachedMillis * @param cachedMillis
* @param dataRenewer * @param dataRenewer
...@@ -119,20 +131,22 @@ public class TimeExpiredPoolCache { ...@@ -119,20 +131,22 @@ public class TimeExpiredPoolCache {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T get(String key, long cachedMillis, DataRenewer<T> dataRenewer) throws Exception { public <T> T get(String key, long cachedMillis, DataRenewer<T> dataRenewer) throws Exception {
DataWrapper<T> dataWrapper = (DataWrapper<T>)dataPool.get(key); DataWrapper<T> dataWrapper = (DataWrapper<T>) dataPool.get(key);
if (dataWrapper != null && !dataWrapper.isExpired()) { if (dataWrapper != null && !dataWrapper.isExpired()) {
return dataWrapper.data; return dataWrapper.data;
} }
return put(key, null, cachedMillis, dataRenewer); return put(key, null, cachedMillis, dataRenewer);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T get(String key) throws Exception { public <T> T get(String key) throws Exception {
DataWrapper<T> dataWrapper = (DataWrapper<T>)dataPool.get(key); DataWrapper<T> dataWrapper = (DataWrapper<T>) dataPool.get(key);
if (dataWrapper != null && !dataWrapper.isExpired()) { if (dataWrapper != null && !dataWrapper.isExpired()) {
return dataWrapper.data; return dataWrapper.data;
} }
return null; return null;
} }
/** /**
* 清除缓存 * 清除缓存
*/ */
...@@ -142,8 +156,8 @@ public class TimeExpiredPoolCache { ...@@ -142,8 +156,8 @@ public class TimeExpiredPoolCache {
/** /**
* 删除指定key的value * 删除指定key的value
* */ */
public void remove(String key){ public void remove(String key) {
dataPool.remove(key); dataPool.remove(key);
} }
...@@ -163,19 +177,24 @@ public class TimeExpiredPoolCache { ...@@ -163,19 +177,24 @@ public class TimeExpiredPoolCache {
* 缓存时间 * 缓存时间
*/ */
private long cachedMillis; private long cachedMillis;
private DataWrapper(T data, long cachedMillis) { private DataWrapper(T data, long cachedMillis) {
this.update(data, cachedMillis); this.update(data, cachedMillis);
} }
public void update(T data, long cachedMillis) { public void update(T data, long cachedMillis) {
this.data = data; this.data = data;
this.cachedMillis = cachedMillis; this.cachedMillis = cachedMillis;
this.updateExpiredTime(); this.updateExpiredTime();
} }
public void updateExpiredTime() { public void updateExpiredTime() {
this.expiredTime = System.currentTimeMillis() + cachedMillis; this.expiredTime = System.currentTimeMillis() + cachedMillis;
} }
/** /**
* 数据是否过期 * 数据是否过期
*
* @return * @return
*/ */
public boolean isExpired() { public boolean isExpired() {
...@@ -185,6 +204,7 @@ public class TimeExpiredPoolCache { ...@@ -185,6 +204,7 @@ public class TimeExpiredPoolCache {
return true; return true;
} }
} }
/** /**
* 数据构造 * 数据构造
*/ */
...@@ -198,7 +218,7 @@ public class TimeExpiredPoolCache { ...@@ -198,7 +218,7 @@ public class TimeExpiredPoolCache {
private static void clearExpiredCaches() { private static void clearExpiredCaches() {
List<String> expiredKeyList = new LinkedList<String>(); List<String> expiredKeyList = new LinkedList<String>();
for(Entry<String, DataWrapper<?>> entry : dataPool.entrySet()){ for (Entry<String, DataWrapper<?>> entry : dataPool.entrySet()) {
if (entry.getValue().isExpired()) { if (entry.getValue().isExpired()) {
expiredKeyList.add(entry.getKey()); expiredKeyList.add(entry.getKey());
} }
......
...@@ -21,3 +21,5 @@ dwxhPullUrl:http://59.208.149.225:18080/sjzt/api/dx/e39a08e0a52b413897f9d2335954 ...@@ -21,3 +21,5 @@ dwxhPullUrl:http://59.208.149.225:18080/sjzt/api/dx/e39a08e0a52b413897f9d2335954
#单位销户推送url #单位销户推送url
dwxhPushUrl:http://59.208.149.225:18080/sjzt/api/retGjjProgData?access_token= dwxhPushUrl:http://59.208.149.225:18080/sjzt/api/retGjjProgData?access_token=
# 日志配置
logging.config:classpath:logback.xml
\ No newline at end of file
...@@ -18,21 +18,13 @@ ...@@ -18,21 +18,13 @@
<!-- 循环政策:基于时间创建日志文件 --> <!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 --> <!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-info.%d{yyyy-MM-dd}.log</fileNamePattern> <fileNamePattern>${log.path}.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 --> <!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory> <maxHistory>30</maxHistory>
</rollingPolicy> </rollingPolicy>
<encoder> <encoder>
<pattern>${log.pattern}</pattern> <pattern>${log.pattern}</pattern>
</encoder> </encoder>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<!-- 过滤的级别 -->
<level>INFO</level>
<!-- 匹配时的操作:接收(记录) -->
<onMatch>ACCEPT</onMatch>
<!-- 不匹配时的操作:拒绝(不记录) -->
<onMismatch>DENY</onMismatch>
</filter>
</appender> </appender>
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender"> <appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
...@@ -40,9 +32,9 @@ ...@@ -40,9 +32,9 @@
<!-- 循环政策:基于时间创建日志文件 --> <!-- 循环政策:基于时间创建日志文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 日志文件名格式 --> <!-- 日志文件名格式 -->
<fileNamePattern>${log.path}/sys-error.%d{yyyy-MM-dd}.log</fileNamePattern> <fileNamePattern>${log.path}-error.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 --> <!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory> <maxHistory>30</maxHistory>
</rollingPolicy> </rollingPolicy>
<encoder> <encoder>
<pattern>${log.pattern}</pattern> <pattern>${log.pattern}</pattern>
...@@ -57,38 +49,13 @@ ...@@ -57,38 +49,13 @@
</filter> </filter>
</appender> </appender>
<!-- 用户访问日志输出 -->
<appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/sys-user.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 按天回滚 daily -->
<fileNamePattern>${log.path}/sys-user.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>
</encoder>
</appender>
<!-- 系统模块日志级别控制 -->
<logger name="com.ruoyi" level="info"/>
<!-- Spring日志级别控制 -->
<logger name="org.springframework" level="warn"/>
<!-- 控制台输出 -->
<root level="info">
<appender-ref ref="console"/>
</root>
<!--系统操作日志--> <!--系统操作日志-->
<root level="info"> <root level="info">
<appender-ref ref="console"/>
<appender-ref ref="file_info"/> <appender-ref ref="file_info"/>
<appender-ref ref="file_error"/> <appender-ref ref="file_error"/>
</root> </root>
<!-- 系统模块日志级别控制 -->
<logger name="net.cdkj" level="debug"/>
<!--系统用户操作日志-->
<logger name="sys-user" level="info">
<appender-ref ref="sys-user"/>
</logger>
</configuration> </configuration>
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment