|
@ -1,6 +1,6 @@
|
|
|
package com.yihu.wlyy.statistics.task;
|
|
|
|
|
|
import com.yihu.wlyy.statistics.util.http.HttpResponse;
|
|
|
import com.yihu.wlyy.statistics.util.HttpClientUtil;
|
|
|
import net.sf.json.JSONArray;
|
|
|
import net.sf.json.JSONObject;
|
|
|
import org.apache.http.NameValuePair;
|
|
@ -94,12 +94,13 @@ public class PushMsgTask {
|
|
|
while (true) {
|
|
|
// 如果queue为空,则当前线程会堵塞,直到有新数据加入
|
|
|
JSONObject json = queue.take();
|
|
|
System.out.println("发送前:"+json);
|
|
|
// 推送平台消息
|
|
|
String receiver = json.has("receiver") ? json.getString("receiver") : "";
|
|
|
String type = json.has("type") ? json.getString("type") : "";
|
|
|
String title = json.has("title") ? json.getString("title") : "";
|
|
|
String msg = json.has("msg") ? json.getString("msg") : "";
|
|
|
String data = json.has("data") ? json.getString("data") : "";
|
|
|
String receiver = json.containsKey("receiver") ? json.getString("receiver") : "";
|
|
|
String type = json.containsKey("type") ? json.getString("type") : "";
|
|
|
String title = json.containsKey("title") ? json.getString("title") : "";
|
|
|
String msg = json.containsKey("msg") ? json.getString("msg") : "";
|
|
|
String data = json.containsKey("data") ? json.getString("data") : "";
|
|
|
boolean res = pushMessage(receiver, type, title, msg, data);
|
|
|
if (res) {
|
|
|
logger.info("消息推送成功!");
|
|
@ -130,62 +131,11 @@ public class PushMsgTask {
|
|
|
params.add(new BasicNameValuePair("contentType", msgType));
|
|
|
params.add(new BasicNameValuePair("title", title));
|
|
|
params.add(new BasicNameValuePair("summary", data));
|
|
|
HttpResponse response = post(url, params);
|
|
|
if (response != null && response.getStatusCode() == 200) {
|
|
|
JSONObject json = JSONObject.fromObject(response.getBody());
|
|
|
if (!"200".equals(json.optString("status"))) {
|
|
|
throw new Exception(json.optString("msg"));
|
|
|
}
|
|
|
} else {
|
|
|
throw new Exception("接口调用错误!"+response.getBody());
|
|
|
}
|
|
|
String response = HttpClientUtil.post(url, params, "UTF-8");
|
|
|
return true;
|
|
|
} catch (Exception e) {
|
|
|
logger.error("push message error:", e);
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
private static HttpResponse post(String url, List<NameValuePair> params) {
|
|
|
HttpResponse re = new HttpResponse();
|
|
|
CloseableHttpResponse response = null;
|
|
|
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
|
|
|
|
//设置请求信息
|
|
|
try {
|
|
|
RequestConfig requestConfig = RequestConfig.custom().
|
|
|
setAuthenticationEnabled(true).build();
|
|
|
HttpPost request = new HttpPost(url );
|
|
|
request.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
|
|
|
request.setConfig(requestConfig);
|
|
|
|
|
|
response = httpclient.execute(request);
|
|
|
|
|
|
re.setStatusCode(response.getStatusLine().getStatusCode());
|
|
|
re.setBody(EntityUtils.toString(response.getEntity(), "UTF-8"));
|
|
|
} catch (Exception e) {
|
|
|
re.setStatusCode(201);
|
|
|
re.setBody(e.getMessage());
|
|
|
} finally {
|
|
|
close(httpclient, response);
|
|
|
}
|
|
|
return re;
|
|
|
}
|
|
|
|
|
|
private static void close(CloseableHttpClient httpClient, CloseableHttpResponse response) {
|
|
|
try {
|
|
|
if (httpClient != null) {
|
|
|
httpClient.close();
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
System.out.print("关闭httpClient失败");
|
|
|
}
|
|
|
try {
|
|
|
if (response != null) {
|
|
|
response.close();
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
System.out.print("关闭response失败");
|
|
|
}
|
|
|
}
|
|
|
}
|