Commit 492a68f3 authored by yanzg's avatar yanzg

消除成功接收处理

parent 37d70485
......@@ -292,7 +292,8 @@ public class ObjectHelper {
while (tempClass != null) {
fields.addAll(Arrays.asList(tempClass.getDeclaredFields()));
methods.addAll(Arrays.asList(tempClass.getDeclaredMethods()));
tempClass = tempClass.getSuperclass(); //得到父类,然后赋给自己
//得到父类,然后赋给自己
tempClass = tempClass.getSuperclass();
}
// 忽略大小写、忽略下划线 _
......
......@@ -28,6 +28,8 @@ public final class CheckerHelper {
public static final String PARAM_NOT_MIN = "param [%s] must be big %d";
public static final String PARAM_NOT_MAX = "param [%s] must be max %d";
public static final String PARAM_NOT_RANGE = "param [%s] must be range %d and %d";
public static final String DATE_CHECK_TYPE_DAY = "day";
public static final String DATE_CHECK_TYPE_MONTH = "month";
private boolean isValid = true;
private String checkResult;
......@@ -178,10 +180,10 @@ public final class CheckerHelper {
this.setValid(false);
return this;
}
if ("day".equals(queryType)) {
if (DATE_CHECK_TYPE_DAY.equals(queryType)) {
eL = "[0-9]{4}[0-9]{2}[0-9]{2}";
msg = PARAM_NOT_DATE_DAY;
} else if ("month".equals(queryType)) {
} else if (DATE_CHECK_TYPE_MONTH.equals(queryType)) {
eL = "[0-9]{4}[0-9]{2}";
msg = PARAM_NOT_DATE_MONTH;
}
......
......@@ -69,11 +69,17 @@ public class DateAutoHelper {
* 时间格式字符串
*/
private static class DateReplace {
// 正则表达式
/**
* 正则表达式
*/
public String regex;
// 替换表达式
/**
* 替换表达式
*/
public String replace;
// 终止标志位
/**
* 终止标志位
*/
public boolean end;
}
......
......@@ -116,13 +116,16 @@ public class FileHelper {
boolean flag = false;
File file = new File(sPath);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
if (!file.exists()) {
// 不存在返回 false
return flag;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
if (file.isFile()) {
// 为文件时调用删除文件方法
return deleteFile(sPath);
} else { // 为目录时调用删除目录方法
} else {
// 为目录时调用删除目录方法
return deleteDirectory(sPath);
}
}
......
......@@ -83,10 +83,13 @@ public class HttpHelper {
* @return 远程响应结果
*/
public static String post(HttpURLConnection httpConn, String url, String jsonString) throws IOException {
String result = "";// 返回的结果
BufferedReader in = null;// 读取响应输入流
// 返回的结果
String result = "";
// 读取响应输入流
BufferedReader in = null;
PrintWriter out = null;
StringBuffer sb = new StringBuffer();// 处理请求参数
// 处理请求参数
StringBuffer sb = new StringBuffer();
String params = "";
try {
params = jsonString;
......
......@@ -7,7 +7,12 @@ import com.alibaba.fastjson.TypeReference;
* JSON序列化的处理
* Created by yanzu on 2017/5/28.
*/
public class JSONHelper {
public class JsonHelper {
/**
* JSON对象开始标记
*/
public static final String OBJECT_START_FLAG = "{";
/**
* 将JSON字符串转化为JSON对象
......@@ -48,7 +53,7 @@ public class JSONHelper {
return null;
}
json = json.trim();
if (json.startsWith("{")) {
if (json.startsWith(OBJECT_START_FLAG)) {
json = String.format("[%s]", json);
}
return JSON.parseObject(json, type);
......
......@@ -42,7 +42,8 @@ public class RunHelper {
break;
} catch (Exception e) {
if (i < size) {
ThreadHelper.sleep(sleep); // 间隔100ms,防止服务器重启时请求失败
// 间隔100ms,防止服务器重启时请求失败
ThreadHelper.sleep(sleep);
}
ex = e;
}
......
......@@ -139,20 +139,23 @@ public class StringHelper {
boolean foundDigit = false;
// deal with any possible sign up front
final int start = (chars[0] == '-') ? 1 : 0;
if (sz > start + 1 && chars[start] == '0') { // leading 0
// leading 0
if (sz > start + 1 && chars[start] == '0') {
if (
(chars[start + 1] == 'x') ||
(chars[start + 1] == 'X')
) { // leading 0x/0X
int i = start + 2;
if (i == sz) {
return false; // str == "0x"
// str == "0x"
return false;
}
// checking hex (it can't be anything else)
for (; i < chars.length; i++) {
if ((chars[i] < '0' || chars[i] > '9')
boolean inputHex = (chars[i] < '0' || chars[i] > '9')
&& (chars[i] < 'a' || chars[i] > 'f')
&& (chars[i] < 'A' || chars[i] > 'F')) {
&& (chars[i] < 'A' || chars[i] > 'F');
if (inputHex) {
return false;
}
}
......@@ -200,7 +203,8 @@ public class StringHelper {
return false;
}
allowSigns = false;
foundDigit = false; // we need a digit after the E
// we need a digit after the E
foundDigit = false;
} else {
return false;
}
......@@ -223,11 +227,12 @@ public class StringHelper {
// single trailing decimal point after non-exponent is ok
return foundDigit;
}
if (!allowSigns
boolean allowD = !allowSigns
&& (chars[i] == 'd'
|| chars[i] == 'D'
|| chars[i] == 'f'
|| chars[i] == 'F')) {
|| chars[i] == 'F');
if (allowD) {
return foundDigit;
}
if (chars[i] == 'l'
......@@ -467,7 +472,7 @@ public class StringHelper {
* @return 转换成功后的值
*/
public static double toDecimal(Object from) {
Double result = Double.valueOf(0);
double result = 0;
try {
if (!isEmpty(from)) {
result = Double.valueOf(from.toString());
......@@ -515,7 +520,8 @@ public class StringHelper {
byte[] buff;
try {
buff = from.getBytes(charset);
from = new String(buff); // 将字节流转换为字符串
// 将字节流转换为字符串
from = new String(buff);
from = from.replace("\0", "").replace("\n", "");
} catch (UnsupportedEncodingException e) {
ExceptionHelper.handleException(StringHelper.class, e, from);
......
......@@ -19,9 +19,9 @@ public class RunPlanData {
*/
private double executeCount;
/**
* 执行时错误次数
* 执行时错误次数,调试变量
*/
private double executeError; // 调试变量
private double executeError;
/**
* 执行时间
*/
......
......@@ -64,7 +64,7 @@ public final class RunnableListAuto {
// 没有执行过的列表
List<RunnableListAutoItem> initList = new ArrayList<RunnableListAutoItem>();
// 已经执行过的数据缓存,待排序
Map<String, List<RunnableListAutoItem>> executeMap = new HashMap<String, List<RunnableListAutoItem>>();
Map<String, List<RunnableListAutoItem>> executeMap = new HashMap<>(20);
// 已执行过的列表
List<RunnableListAutoItem> executedToList = new ArrayList<RunnableListAutoItem>();
......
......@@ -16,7 +16,9 @@ public class ThreadHelper {
private static Date threadDate = null;
private static RunPlan timeout;
private static RunPlan interval;
// 线程对象
/**
* 线程对象
*/
private static ExecutorService executeService = Executors.newCachedThreadPool();
/**
......
......@@ -31,14 +31,21 @@ public class ThreadNext {
new Thread(new Runnable() {
@Override
public void run() {
runNext(next);
}
}).start();
}
private static void runNext(Next next) {
boolean isExecute = true;
Exception frontEx = null;
while (isExecute) {
try {
isExecute = next.next();
} catch (Exception ex) {
if (frontEx == null || !(frontEx.getClass() == ex.getClass() &&
StringHelper.compare(frontEx.getMessage(), ex.getMessage()))) {
boolean isHistoryEx = frontEx == null || !(frontEx.getClass() == ex.getClass() &&
StringHelper.compare(frontEx.getMessage(), ex.getMessage()));
if (isHistoryEx) {
Log.error(ThreadNext.class, ex);
}
frontEx = ex;
......@@ -55,6 +62,4 @@ public class ThreadNext {
}
}
}
}).start();
}
}
package com.yanzuoguang.util.vo;
import com.yanzuoguang.util.helper.JSONHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import java.io.Serializable;
......@@ -18,6 +18,6 @@ public class BaseVo implements Serializable {
*/
@Override
public String toString() {
return JSONHelper.serialize(this);
return JsonHelper.serialize(this);
}
}
......@@ -40,7 +40,9 @@ public class BaseRequestAspect implements ThreadNext.Next {
*/
protected volatile LinkedBlockingQueue<LogVo> cache = new LinkedBlockingQueue<LogVo>();
// 缓存的每次请求的结果
/**
* 缓存的每次请求的结果
*/
protected MemoryCache<RequestCacheResult> cacheResult = new MemoryCache<>(cacheTime);
public BaseRequestAspect() {
......@@ -67,8 +69,10 @@ public class BaseRequestAspect implements ThreadNext.Next {
protected LogVo initLogInterVo(String url, ProceedingJoinPoint joinPoint, ResponseResult responseResult) {
LogVo logInterVo = new LogVo();
logInterVo.setLogId(StringHelper.getNewID());
logInterVo.setLogSources(applicationName);//平台名
logInterVo.setInterUrl(url);//请求URL
//平台名
logInterVo.setLogSources(applicationName);
//请求URL
logInterVo.setInterUrl(url);
List<Object> para = new ArrayList<>();
for (Object item : joinPoint.getArgs()) {
if (item instanceof HttpServlet || item instanceof HttpServletResponse || item instanceof HttpServletRequest) {
......@@ -80,8 +84,10 @@ public class BaseRequestAspect implements ThreadNext.Next {
if (para.size() == 1) {
paraTo = para.get(0);
}
logInterVo.setContent(JSON.toJSONString(paraTo));//请求参数
logInterVo.setResult(JSON.toJSONString(responseResult));//返回参数
//请求参数
logInterVo.setContent(JSON.toJSONString(paraTo));
//返回参数
logInterVo.setResult(JSON.toJSONString(responseResult));
logInterVo.setStatus(responseResult != null && responseResult.getCode() == ResultConstants.SUCCESS ? 1 : 0);
return logInterVo;
}
......
......@@ -3,7 +3,7 @@ package com.yanzuoguang.cloud.aop;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.JSONHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.LogVo;
......@@ -51,7 +51,8 @@ public class WebAspect extends BaseRequestAspect {
@Around(value = "webAspect()")
public Object requestWebAround(ProceedingJoinPoint joinPoint) throws Throwable {
Log.threadBegin();
ResponseResult responseResult = null; // 用户数据库记录
// 用户数据库记录
ResponseResult responseResult = null;
long start = System.currentTimeMillis();
String name = joinPoint.getSignature().getName();
try {
......@@ -100,17 +101,17 @@ public class WebAspect extends BaseRequestAspect {
}
private Object executeMethod(ProceedingJoinPoint joinPoint, String name) throws Throwable {
if (joinPoint.getArgs().length != 1
boolean dataArgs = joinPoint.getArgs().length != 1
|| joinPoint.getArgs().length == 1 &&
(joinPoint.getArgs()[0] instanceof ServletResponse || joinPoint.getArgs()[0] instanceof ServletRequest)
) {
(joinPoint.getArgs()[0] instanceof ServletResponse || joinPoint.getArgs()[0] instanceof ServletRequest);
if (dataArgs) {
return joinPoint.proceed();
} else {
// 获取请求编号
Object firstArgs = joinPoint.getArgs().length > 0 ? joinPoint.getArgs()[0] : null;
String reqId = ObjectHelper.getString(firstArgs, "reqId");
if (StringHelper.isEmpty(reqId)) {
reqId = StringHelper.md5(JSONHelper.serialize(firstArgs));
reqId = StringHelper.md5(JsonHelper.serialize(firstArgs));
} else {
// 请求编号和公司编号挂钩
reqId = StringHelper.getId(ObjectHelper.getString(firstArgs, "companyId"), reqId);
......
package com.yanzuoguang.code;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.util.helper.ByteHelper;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* 加密算法0版实现
*/
......@@ -120,28 +126,28 @@ public class CodePwdImpl implements CodePwd {
return ret;
}
// public static void main(String[] args) {
// int[] pos2 = {3, 4, 5, 6, 8};
// int startPos = 0;
// int endPos = 1000000;
//
// Date start = new Date();
// CodePwdReplaceImpl item = new CodePwdReplaceImpl();
// Map<String, Integer> map = new HashMap<>();
// for (int i = startPos; i < endPos; i++) {
// String index = String.format("%09d", i);
// String code = item.getCode("", "", index, "18532354168");
// if (map.containsKey(code)) {
// map.put(code, map.get(code) + 1);
// System.err.println("code: " + map.get(code));
// } else {
// map.put(code, 1);
// }
// int rand2 = pos2[new Random().nextInt(pos2.length)];
// String mobile = "1" + rand2 + code;
// System.out.println(mobile);
// }
// Date end = new Date();
// System.out.println("长度: " + (endPos - startPos) + " time:" + (end.getTime() - start.getTime()));
// }
public static void test(String[] args) {
int[] pos2 = {3, 4, 5, 6, 8};
int startPos = 0;
int endPos = 1000000;
Date start = new Date();
CodePwdImpl item = new CodePwdImpl();
Map<String, Integer> map = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
for (int i = startPos; i < endPos; i++) {
String index = String.format("%09d", i);
String code = item.getCode("", "", index, "18532354168");
if (map.containsKey(code)) {
map.put(code, map.get(code) + 1);
System.err.println("code: " + map.get(code));
} else {
map.put(code, 1);
}
int rand2 = pos2[new Random().nextInt(pos2.length)];
String mobile = "1" + rand2 + code;
System.out.println(mobile);
}
Date end = new Date();
System.out.println("长度: " + (endPos - startPos) + " time:" + (end.getTime() - start.getTime()));
}
}
......@@ -116,4 +116,8 @@ public class DaoConst {
* ID字段
*/
public static final String ID_FIELD = "id";
/**
* 集合初始化大小
*/
public static final int COLLECTION_INIT_SIZE = 15;
}
......@@ -195,7 +195,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
// 当主键存在值时,直接通过主键删除
if (!StringHelper.isEmpty(keyString)) {
// 去掉其他非主键的属性
from = new HashMap<String, Object>();
from = new HashMap<String, Object>(DaoConst.COLLECTION_INIT_SIZE);
this.setKeyString(from, keyString);
// 调用删除日志
this.check(DaoConst.OPERATOR_TYPE_REMOVE, keyString, from);
......@@ -232,7 +232,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
// 当主键存在时,只通过主键加载
if (!StringHelper.isEmpty(key)) {
from = new HashMap<String, Object>();
from = new HashMap<String, Object>(DaoConst.COLLECTION_INIT_SIZE);
this.setKeyString(from, key);
}
......@@ -278,7 +278,7 @@ public abstract class BaseDaoImpl extends BaseDaoSql implements BaseDao {
from = model;
this.setKeyString(model, md5);
} else if (!StringHelper.isEmpty(this.table.getTable().getMD5KeyName())) {
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> map = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
map.put(this.table.getTable().getMD5KeyName(), md5);
from = this.load(map, cls);
} else {
......
......@@ -410,11 +410,7 @@ public abstract class BaseDaoSql {
* @return SQL条件
*/
protected String getQueryPara(List<Object> paras, SqlData sqlData, Object model) {
// 定义可替换片段
// String fromNext = "{FIELD_Front}";
// String fromPrev = "{FIELD}";
// String wherePrev = "{INNER}";
String[] lastCode = new String[]{"{WHERE}", "{GROUP}", "{HAVING}", "{ORDER}", "{LIMIT}"};
// 将SQL语句进行代码片段追加
......@@ -425,9 +421,25 @@ public abstract class BaseDaoSql {
}
}
// 代码片段处理
sql = handleCodeMap(sql, sqlData, model);
// 通过正则表达式处理参数 @name 并替换SQL语句成 ?
sql = handlePara(sql, paras, model);
return sql;
}
/**
* 代码片段处理
*
* @return
*/
private String handleCodeMap(String sql, SqlData sqlData, Object model) {
// 处理字段以及代码片段
// 代码片段缓存
Map<String, List<String>> codeMap = new HashMap<>();
Map<String, List<String>> codeMap = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
// 循环处理字段
for (SqlDataField field : sqlData.getSqlDataFields()) {
// 获取值
......@@ -470,15 +482,24 @@ public abstract class BaseDaoSql {
// 寻找到的代码片段 不包含分括号
while (m.find()) {
String name = m.group();
// m.group(1);
List<String> codes = codeMap.containsKey(name) ? codeMap.get(name) : new ArrayList<String>();
String code = String.join("", codes);
sql = sql.replace(name, code);
}
}
// 通过正则表达式处理参数 @name 并替换SQL语句成 ?
{
return sql;
}
/**
* 通过正则表达式处理参数 @name 并替换SQL语句成 ?
*
* @param sql 需要处理的SQL语句
* @param paras 需要添加的参数
* @param model 需要获取的参数实体
* @return 处理后的SQL语句
*/
private String handlePara(String sql, List<Object> paras, Object model) {
String regex = "@([a-zA-Z0-9_]+)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(sql);
......@@ -491,7 +512,8 @@ public abstract class BaseDaoSql {
// 根据输入字段从参数中取值
Object val = ObjectHelper.get(model, field);
// 判断是否为数组
if (val != null && (val instanceof List || val.getClass().isArray())) {
boolean isArray = val != null && (val instanceof List || val.getClass().isArray());
if (isArray) {
sql = getListSql(paras, sql, name, val);
} else {
// 当参数不为数组时,则直接增加
......@@ -500,8 +522,6 @@ public abstract class BaseDaoSql {
paras.add(val);
}
}
}
return sql;
}
......@@ -647,7 +667,7 @@ public abstract class BaseDaoSql {
}
// 将对象转换为 Map 对象
Map to = new HashMap<String, Object>();
Map to = new HashMap<String, Object>(DaoConst.COLLECTION_INIT_SIZE);
if (from instanceof Map) {
to = (Map) from;
} else {
......
package com.yanzuoguang.dao.impl;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper;
......@@ -120,9 +121,9 @@ public class TableSqlCache {
*/
public List<SqlData> addGroupList(String sqlName, TableFieldString tableWhereField, TableFieldString addField,
String sql, String... field) {
Map<String, Boolean> sqlFieldHas = new HashMap<String, Boolean>();
Map<String, Boolean> sqlFieldWhere = new HashMap<String, Boolean>();
Map<String, Boolean> sqlFieldAdd = new HashMap<String, Boolean>();
Map<String, Boolean> sqlFieldHas = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
Map<String, Boolean> sqlFieldWhere = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
Map<String, Boolean> sqlFieldAdd = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
SqlData sqlInit = new SqlData(sqlName + "_GroupInit", "", field);
SqlData sqlAdd = new SqlData(sqlName + "_GroupAdd", "", field);
......@@ -166,7 +167,7 @@ public class TableSqlCache {
String addModel = "UPDATE {table} AS a INNER JOIN ( {SelectSQL} ) AS b ON a.{Key} = b.{Key} SET {addConst} ";
// 定义需要处理的SQL对象
Map<String, StringBuilder> map = new HashMap<String, StringBuilder>();
Map<String, StringBuilder> map = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
addString(map, "{SQL}", sql);
addString(map, "{table}", this.table.getName());
......
......@@ -76,9 +76,9 @@ public class AllBeanRowMapper<T> implements RowMapper<T> {
*/
protected void initialize(Class<T> mappedClass) {
this.mappedClass = mappedClass;
this.mappedFields = new HashMap<>();
this.mappedPropertys = new HashMap<>();
this.mappedIsFields = new HashMap<>();
this.mappedFields = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
this.mappedPropertys = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
this.mappedIsFields = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
if (ObjectHelper.isSub(MapRow.class, mappedClass) || ObjectHelper.isSub(Map.class, mappedClass)) {
isMapping = true;
......
package com.yanzuoguang.mq.dao;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
......@@ -19,7 +20,9 @@ public class BeanDao {
private static final String QUEUE = "queue";
private static final String EXCHANGE = "exchange";
// 上下文
/**
* 上下文
*/
@Autowired
private ApplicationContext context;
......@@ -83,7 +86,7 @@ public class BeanDao {
bean = new Queue(queueName, true, false, false);
amqpAdmin.declareQueue(bean);
} else {
Map<String, Object> params = new HashMap<>();
Map<String, Object> params = new HashMap<>(DaoConst.COLLECTION_INIT_SIZE);
if (deadTime > 0) {
params.put("x-message-ttl", deadTime);
}
......
......@@ -39,7 +39,7 @@ public class MessageDaoImpl extends BaseDaoImpl implements MessageDao {
*/
@Override
public int updateBatch(String batchId, int size) {
Map<String, Object> map = new HashMap<>();
Map<String, Object> map = new HashMap<>(1);
map.put("batchId", batchId);
SqlData sql = this.getSql(UPDATE_BATCH_SQL).copy();
......
......@@ -19,12 +19,16 @@ import java.util.List;
@Component
public class QueueServiceImpl implements QueueService {
// 队列名称
/**
* 队列名称
*/
@Autowired
private QueueDao queueDao;
@Autowired
private BeanDao beanDao;
// 是否已经初始化
/**
* 是否已经初始化
*/
private boolean init;
/**
......
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