优化galaxyMessageFormat中的json解析速度,使用fastjson替换JSONObject

This commit is contained in:
wangxin
2019-04-29 17:39:55 +08:00
parent d4ee7c7a42
commit fa9edae028
2 changed files with 19 additions and 5 deletions

View File

@@ -727,6 +727,10 @@
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.57</version>
</dependency>
</dependencies>
</project>

View File

@@ -23,6 +23,7 @@ import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.commons.collections.CollectionUtils;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
@@ -37,6 +38,7 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSON;
import com.nis.exceptions.MaatConvertException;
import com.nis.util.Constants;
import com.nis.util.Encodes;
@@ -236,6 +238,7 @@ public class HttpClientUtil {
* @return 查询结果数据json
*/
public static String getMsg(String destUrl, Map<String, Object> params, HttpServletRequest req) throws IOException {
long start=System.currentTimeMillis(), end=System.currentTimeMillis();
// RequestContext requestContext = new RequestContext(req);
// CloseableHttpResponse response = null;
@@ -283,6 +286,7 @@ public class HttpClientUtil {
logger.error("获取消息失败,相应内容如下: " + result);
throw new MaatConvertException(status+"");
}
} catch (Exception e) {
e.printStackTrace();
logger.error("获取消息失败,相应内容如下: " + result);
@@ -292,6 +296,8 @@ public class HttpClientUtil {
if (response != null) {
response.close();
}
end=System.currentTimeMillis();
logger.warn("getMsg cost:"+(end-start)+"ms");
}
return result;
}
@@ -302,12 +308,14 @@ public class HttpClientUtil {
* @return
*/
public static String galaxyMessageFormat(String recv){
JSONObject jobj = JSONObject.fromObject(recv);
long start=System.currentTimeMillis(),end=System.currentTimeMillis();
//JSONObject jobj = JSONObject.fromObject(recv);
com.alibaba.fastjson.JSONObject jobj = JSON.parseObject(recv);
Map<String, Object> parse = (Map<String, Object>)jobj;
Map map = (Map) parse.get("data");
List reslist=new ArrayList();
List<Map<String,Object>> list= (List)map.get("list");
if(list!=null&&list.size()>0){
if(CollectionUtils.isNotEmpty(list)){
for (Map<String,Object> m : list) {
Map recvMap = new HashMap();
Iterator<Entry<String, Object>> iterator = m.entrySet().iterator();
@@ -316,12 +324,12 @@ public class HttpClientUtil {
String key = next.getKey().toString();
Object value = next.getValue();
//处理字段为“null”情况
if(value!=null&&"null".equals(value)){
if("null".equals(value)){
value="";
}
//处理时间字段为“0”情况
if("foundTime".equals(key)||"recvTime".equals(key)){
if(null!=value&&"0".equals(value)){
if("0".equals(value)){
value="";
}
}
@@ -337,6 +345,8 @@ public class HttpClientUtil {
parse.put("data", map);
recv = parse.toString();
}
end=System.currentTimeMillis();
logger.info("galaxyMessageFormat cost:"+(end-start));
return recv;
}