Commit 73fb2bbe626672dffc097fca7d1d22c6cdf30621

Authored by yangfei

Merge remote-tracking branch 'origin/master'

Showing 6 changed files

platform-msg-generate/src/main/java/com/lyms/platform/msg/remote/SaveMessageService.java View file @ 73fb2bb
... ... @@ -11,6 +11,7 @@
11 11 import com.lyms.platform.common.utils.StringUtils;
12 12 import com.lyms.platform.msg.constants.ConfigInterface;
13 13 import com.lyms.platform.msg.utils.DateUtils;
  14 +import com.lyms.platform.msg.utils.HttpsClientManager;
14 15 import net.sf.json.JSONArray;
15 16 import net.sf.json.JSONObject;
16 17 import org.apache.commons.httpclient.HttpClient;
17 18  
... ... @@ -26,8 +27,15 @@
26 27 */
27 28 public class SaveMessageService {
28 29  
29   - private static final Logger resultLog = LoggerFactory.getLogger("GEN-RESULT");
  30 + private static final Logger logger = LoggerFactory.getLogger(SaveMessageService.class);
30 31  
  32 + private static HttpsClientManager https =new HttpsClientManager();
  33 + {
  34 + https.setConnTimeout(10000);
  35 + https.setSoTimeout(120000);
  36 + https.setMaxConnectionsPerHost(200);
  37 + https.setMaxTotalConnections(300);
  38 + }
31 39  
32 40  
33 41 /**
34 42  
35 43  
... ... @@ -38,15 +46,20 @@
38 46 public static boolean saveMsgCenter(MessageListRequest list)
39 47 {
40 48 String json = JsonUtil.obj2JsonString(list);
41   - //resultLog.info(json);
42   - String result = HttpClientUtil.doPostSSL(ConfigInterface.MSG_BASE_URL+"/biz-push-web/push",json);
43   - if (StringUtils.isNotEmpty(result))
44   - {
45   - Map<String,String> map = JsonUtil.str2Obj(result, Map.class);
46   - if ("0".equals(String.valueOf(map.get("errorcode"))))
  49 + String result = null;
  50 + try {
  51 + result = https.doPost(ConfigInterface.MSG_BASE_URL + "/biz-push-web/push", json, null);
  52 +
  53 + if (StringUtils.isNotEmpty(result))
47 54 {
48   - return true;
  55 + Map<String,String> map = JsonUtil.str2Obj(result, Map.class);
  56 + if ("0".equals(String.valueOf(map.get("errorcode"))))
  57 + {
  58 + return true;
  59 + }
49 60 }
  61 + } catch (Exception e) {
  62 + logger.error("save msg is error",e);
50 63 }
51 64 return false;
52 65 }
53 66  
54 67  
... ... @@ -66,16 +79,21 @@
66 79 param.put("limit","10");
67 80 param.put("patientId",patientId);
68 81 param.put("tempId",tempId);
69   - String result = HttpClientUtil.doGet(ConfigInterface.MSG_BASE_URL + "/biz-push-web/push", param,"utf-8",null);
70   - JSONObject jsonObject = JsonUtil.getObj(result);
71   - if (jsonObject != null && jsonObject.getJSONObject("pageInfo") != null)
72   - {
73   - JSONObject jsonObject1 = jsonObject.getJSONObject("pageInfo");
74   - int count = jsonObject1.getInt("count");
75   - if (count > 0)
  82 + String result = null;
  83 + try {
  84 + result = https.doGet(ConfigInterface.MSG_BASE_URL + "/biz-push-web/push", param,null);
  85 + JSONObject jsonObject = JsonUtil.getObj(result);
  86 + if (jsonObject != null && jsonObject.getJSONObject("pageInfo") != null)
76 87 {
77   - return true;
  88 + JSONObject jsonObject1 = jsonObject.getJSONObject("pageInfo");
  89 + int count = jsonObject1.getInt("count");
  90 + if (count > 0)
  91 + {
  92 + return true;
  93 + }
78 94 }
  95 + } catch (Exception e) {
  96 + logger.error("get msg is exist error",e);
79 97 }
80 98 return false;
81 99 }
platform-msg-generate/src/main/java/com/lyms/platform/msg/service/impl/MsgGenerateServiceImpl.java View file @ 73fb2bb
... ... @@ -64,7 +64,7 @@
64 64 logger.info("The generatePatAmsMsg configs hospital size is {}",configModels.size());
65 65 if (CollectionUtils.isNotEmpty(configModels))
66 66 {
67   - int batchSize = 5;
  67 + int batchSize = 10;
68 68 int end = 0;
69 69 for (int i = 0; i < configModels.size(); i += batchSize) {
70 70 end = (end + batchSize);
... ... @@ -90,7 +90,7 @@
90 90 logger.info("The generateBabyAmsMsg configs hospital size is {}",configModels.size());
91 91 if (CollectionUtils.isNotEmpty(configModels))
92 92 {
93   - int batchSize = 5;
  93 + int batchSize = 10;
94 94 int end = 0;
95 95 for (int i = 0; i < configModels.size(); i += batchSize) {
96 96 end = (end + batchSize);
... ... @@ -116,7 +116,7 @@
116 116 logger.info("The generateChanAmsMsg configs hospital size is {}",configModels.size());
117 117 if (CollectionUtils.isNotEmpty(configModels))
118 118 {
119   - int batchSize = 5;
  119 + int batchSize = 10;
120 120 int end = 0;
121 121 for (int i = 0; i < configModels.size(); i += batchSize) {
122 122 end = (end + batchSize);
... ... @@ -142,7 +142,7 @@
142 142 logger.info("The generateChanAmsMsg configs hospital size is {}",configModels.size());
143 143 if (CollectionUtils.isNotEmpty(configModels))
144 144 {
145   - int batchSize = 5;
  145 + int batchSize = 10;
146 146 int end = 0;
147 147 for (int i = 0; i < configModels.size(); i += batchSize) {
148 148 end = (end + batchSize);
... ... @@ -162,22 +162,10 @@
162 162 */
163 163 @Override
164 164 public void generaterAllMsg() {
165   - logger.info("generater all msg starting.");
166   - new Thread(new Runnable() {
167   - @Override
168   - public void run() {
169   - generateTemplateMsg(null);
170   - }
171   - }).start();
172   -
173   - new Thread(new Runnable() {
174   - @Override
175   - public void run() {
176   - generateChanAmsMsg(null);
177   - generateBabyAmsMsg(null);
178   - generatePatAmsMsg(null);
179   - }
180   - }).start();
  165 + generateTemplateMsg(null);
  166 + generateChanAmsMsg(null);
  167 + generateBabyAmsMsg(null);
  168 + generatePatAmsMsg(null);
181 169 }
182 170 }
platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/HttpsClientManager.java View file @ 73fb2bb
  1 +package com.lyms.platform.msg.utils;
  2 +
  3 +import com.lyms.platform.common.utils.StringUtils;
  4 +import org.apache.commons.httpclient.HttpClient;
  5 +import org.apache.commons.httpclient.HttpMethod;
  6 +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
  7 +import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
  8 +import org.apache.commons.httpclient.methods.GetMethod;
  9 +import org.apache.commons.httpclient.methods.PostMethod;
  10 +import org.apache.commons.httpclient.methods.RequestEntity;
  11 +import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
  12 +import org.apache.commons.httpclient.protocol.Protocol;
  13 +import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
  14 +import org.slf4j.Logger;
  15 +import org.slf4j.LoggerFactory;
  16 +
  17 +import java.io.InputStream;
  18 +import java.util.Iterator;
  19 +import java.util.Map;
  20 +
  21 +public final class HttpsClientManager
  22 +{
  23 + private static final Logger LOGGER = LoggerFactory.getLogger(HttpsClientManager.class);
  24 + private static HttpClient client = null;
  25 +
  26 + //http.param.maxconnection.perhost=50
  27 + private int maxConnectionsPerHost = 50;
  28 + //http.param.maxTotalConnections=200
  29 + private int maxTotalConnections=200;
  30 + //http.param.connTimeout=2000
  31 + private int connTimeout=2000;
  32 + //http.param.soTimeout=2000
  33 + private int soTimeout=2000;
  34 +
  35 + private void init()
  36 + {
  37 + ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
  38 + Protocol.registerProtocol("https", new Protocol("https", fcty, 443));
  39 +
  40 + MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
  41 +
  42 + HttpConnectionManagerParams params = new HttpConnectionManagerParams();
  43 +
  44 + params.setDefaultMaxConnectionsPerHost(maxConnectionsPerHost);
  45 +
  46 + params.setMaxTotalConnections(maxTotalConnections);
  47 +
  48 + params.setConnectionTimeout(connTimeout);
  49 +
  50 + params.setSoTimeout(soTimeout);
  51 +
  52 + manager.setParams(params);
  53 +
  54 + client = new HttpClient(manager);
  55 + }
  56 +
  57 +
  58 + public HttpClient getClient() {
  59 + if (null == client) {
  60 + synchronized (HttpsClientManager.class) {
  61 + if (null == client) {
  62 + init();
  63 + }
  64 + }
  65 + }
  66 + return client;
  67 + }
  68 +
  69 + public void setMaxConnectionsPerHost(int maxConnectionsPerHost)
  70 + {
  71 + this.maxConnectionsPerHost = maxConnectionsPerHost;
  72 + }
  73 +
  74 + public void setMaxTotalConnections(int maxTotalConnections)
  75 + {
  76 + this.maxTotalConnections = maxTotalConnections;
  77 + }
  78 +
  79 +
  80 + public void setConnTimeout(int connTimeout)
  81 + {
  82 + this.connTimeout = connTimeout;
  83 + }
  84 +
  85 + public void setSoTimeout(int soTimeout)
  86 + {
  87 + this.soTimeout = soTimeout;
  88 + }
  89 +
  90 + public String execute(HttpMethod method)
  91 + throws Exception {
  92 + if (null == method) {
  93 + LOGGER.warn("invalid method,method is null");
  94 + return null;
  95 + }
  96 + String result = null;
  97 + HttpClient hc = getClient();
  98 + try {
  99 + hc.executeMethod(method);
  100 + int status = method.getStatusCode();
  101 + result = method.getResponseBodyAsString();
  102 + if (status == 304) {
  103 + LOGGER.warn("hit cache,result=" + result);
  104 + }
  105 + } finally {
  106 + method.releaseConnection();
  107 + }
  108 + return result;
  109 + }
  110 +
  111 + public String doGet(String url,Map<String,String> params,Map<String,Object> header)
  112 + throws Exception {
  113 +
  114 + StringBuffer sb = new StringBuffer();
  115 + if(params!=null && !params.isEmpty()) {
  116 + Iterator<String> keys = params.keySet().iterator();
  117 + while(keys.hasNext()){
  118 + String key = keys.next();
  119 + String value = params.get(key);
  120 + if (!StringUtils.isNotEmpty(value))
  121 + {
  122 + continue;
  123 + }
  124 + if(sb.length()==0)sb.append("?");
  125 + else sb.append("&");
  126 + sb.append(key+"="+value);
  127 + }
  128 + }
  129 +
  130 + GetMethod getMethod = new GetMethod(url+sb.toString());
  131 + if (null!=header)
  132 + {
  133 + Iterator<Map.Entry<String,Object>> it = header.entrySet().iterator();
  134 + while(it.hasNext()){
  135 + Map.Entry<String,Object> entry =it.next();
  136 + entry.getKey();
  137 + entry.getValue();
  138 + getMethod.addRequestHeader(entry.getKey(), entry.getValue().toString());
  139 + }
  140 + }
  141 + return execute(getMethod);
  142 + }
  143 +
  144 + public String doPost(String url, String postData,Map<String,Object> header)
  145 + throws Exception {
  146 + PostMethod postMethod = new PostMethod(url);
  147 + if (postData != null) {
  148 +
  149 + if (header != null)
  150 + {
  151 + Iterator<Map.Entry<String,Object>> it = header.entrySet().iterator();
  152 + while(it.hasNext()){
  153 + Map.Entry<String,Object> entry =it.next();
  154 + entry.getKey();
  155 + entry.getValue();
  156 + postMethod.addRequestHeader(entry.getKey(), entry.getValue().toString());
  157 + }
  158 + }
  159 + byte[] reqBytes = postData.getBytes();
  160 + RequestEntity requestEntity = new ByteArrayRequestEntity(reqBytes);
  161 + postMethod.setRequestEntity(requestEntity);
  162 + }
  163 + return execute(postMethod);
  164 + }
  165 +}
platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/MySecureProtocolSocketFactory.java View file @ 73fb2bb
  1 +package com.lyms.platform.msg.utils;
  2 +
  3 +
  4 +import org.apache.commons.httpclient.ConnectTimeoutException;
  5 +import org.apache.commons.httpclient.HttpClientError;
  6 +import org.apache.commons.httpclient.params.HttpConnectionParams;
  7 +import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
  8 +import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
  9 +
  10 +import javax.net.ssl.SSLContext;
  11 +import javax.net.ssl.TrustManager;
  12 +import java.io.IOException;
  13 +import java.net.InetAddress;
  14 +import java.net.Socket;
  15 +import java.net.UnknownHostException;
  16 +
  17 +public class MySecureProtocolSocketFactory implements SecureProtocolSocketFactory {
  18 +
  19 + private SSLContext sslContext = null;
  20 +
  21 + /**
  22 + * Constructor for MySecureProtocolSocketFactory.
  23 + */
  24 + public MySecureProtocolSocketFactory() {
  25 + }
  26 +
  27 + /**
  28 + * @return
  29 + */
  30 + private static SSLContext createEasySSLContext() {
  31 + try {
  32 + SSLContext context = SSLContext.getInstance("SSL");
  33 + context.init(null, new TrustManager[] {new MyX509TrustManager()}, null);
  34 + return context;
  35 + } catch (Exception e) {
  36 + throw new HttpClientError(e.toString());
  37 + }
  38 + }
  39 +
  40 + /**
  41 + * @return
  42 + */
  43 + private SSLContext getSSLContext() {
  44 + if (this.sslContext == null) {
  45 + this.sslContext = createEasySSLContext();
  46 + }
  47 + return this.sslContext;
  48 + }
  49 +
  50 + /*
  51 + * (non-Javadoc)
  52 + * @see
  53 + * org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
  54 + * int, java.net.InetAddress, int)
  55 + */
  56 + public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException,
  57 + UnknownHostException {
  58 +
  59 + return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
  60 + }
  61 +
  62 + /*
  63 + * (non-Javadoc)
  64 + * @see
  65 + * org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String,
  66 + * int, java.net.InetAddress, int, org.apache.commons.httpclient.params.HttpConnectionParams)
  67 + */
  68 + public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort,
  69 + final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
  70 + if (params == null) {
  71 + throw new IllegalArgumentException("Parameters may not be null");
  72 + }
  73 + int timeout = params.getConnectionTimeout();
  74 + if (timeout == 0) {
  75 + return createSocket(host, port, localAddress, localPort);
  76 + } else {
  77 + return ControllerThreadSocketFactory.createSocket(this, host, port, localAddress, localPort, timeout);
  78 + }
  79 + }
  80 +
  81 + /*
  82 + * (non-Javadoc)
  83 + * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
  84 + */
  85 + public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
  86 + return getSSLContext().getSocketFactory().createSocket(host, port);
  87 + }
  88 +
  89 + /*
  90 + * (non-Javadoc)
  91 + * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean)
  92 + */
  93 + public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException,
  94 + UnknownHostException {
  95 + return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
  96 + }
  97 +}
platform-msg-generate/src/main/java/com/lyms/platform/msg/utils/MyX509TrustManager.java View file @ 73fb2bb
  1 +package com.lyms.platform.msg.utils;
  2 +
  3 +
  4 +import javax.net.ssl.X509TrustManager;
  5 +import java.security.cert.CertificateException;
  6 +import java.security.cert.X509Certificate;
  7 +
  8 +public class MyX509TrustManager implements X509TrustManager {
  9 +
  10 + /*
  11 + * (non-Javadoc)
  12 + * @see javax.net.ssl.X509TrustManager#checkClientTrusted(java.security.cert.X509Certificate[],
  13 + * java.lang.String)
  14 + */
  15 + public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
  16 +
  17 + }
  18 +
  19 + /*
  20 + * (non-Javadoc)
  21 + * @see javax.net.ssl.X509TrustManager#checkServerTrusted(java.security.cert.X509Certificate[],
  22 + * java.lang.String)
  23 + */
  24 + public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
  25 +
  26 + }
  27 +
  28 + /*
  29 + * (non-Javadoc)
  30 + * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers()
  31 + */
  32 + public X509Certificate[] getAcceptedIssuers() {
  33 + return null;
  34 + }
  35 +
  36 +}
platform-msg-generate/src/main/java/com/lyms/platform/msg/worker/TempleteMsgGenerateWorker.java View file @ 73fb2bb
... ... @@ -91,7 +91,7 @@
91 91 }
92 92 try {
93 93  
94   - int batchSize = 8;
  94 + int batchSize = 10;
95 95 int end = 0;
96 96 for (int i = 0; i < templates.size(); i += batchSize) {
97 97 end = (end + batchSize);