|
|
@ -2,6 +2,7 @@ package com.epmet.commons.tools.utils; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.epmet.commons.tools.dto.form.DingTalkTextMsg; |
|
|
|
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
|
|
import com.google.common.collect.Lists; |
|
|
|
import org.apache.commons.codec.binary.Base64; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
@ -12,7 +13,12 @@ import org.springframework.stereotype.Component; |
|
|
|
import javax.annotation.PreDestroy; |
|
|
|
import javax.crypto.Mac; |
|
|
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
import java.io.BufferedReader; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStreamReader; |
|
|
|
import java.io.PrintWriter; |
|
|
|
import java.net.URL; |
|
|
|
import java.net.URLConnection; |
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.util.concurrent.ArrayBlockingQueue; |
|
|
|
|
|
|
@ -48,7 +54,7 @@ public class DingdingMsgSender { |
|
|
|
//阻塞取元素
|
|
|
|
msg = msgQueue.take(); |
|
|
|
if (msg != null) { |
|
|
|
sendPostByJSON(msg); |
|
|
|
sendMsg(msg); |
|
|
|
} else { |
|
|
|
Thread.sleep(1000); |
|
|
|
} |
|
|
@ -81,7 +87,7 @@ public class DingdingMsgSender { |
|
|
|
DingTalkTextMsg param = new DingTalkTextMsg(); |
|
|
|
param.setContent("待发送消息队列已满,当前队列个数" + msgQueue.size() + "\n" + "最新消息内容:" + JSON.toJSONString(messageParam)); |
|
|
|
param.setWebHook(messageParam.getWebHook()); |
|
|
|
sendPostByJSON(param); |
|
|
|
sendMsg(param); |
|
|
|
} |
|
|
|
return flag; |
|
|
|
} |
|
|
@ -94,7 +100,7 @@ public class DingdingMsgSender { |
|
|
|
* @throws IOException |
|
|
|
*/ |
|
|
|
public Result<String> sendMsgSync(DingTalkTextMsg messageParam) { |
|
|
|
return sendPostByJSON(messageParam); |
|
|
|
return sendMsg(messageParam); |
|
|
|
} |
|
|
|
|
|
|
|
private Thread getThread() { |
|
|
@ -121,7 +127,7 @@ public class DingdingMsgSender { |
|
|
|
* @param param 请求参数,JSON格式 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private Result<String> sendPostByJSON(DingTalkTextMsg param) { |
|
|
|
private Result<String> sendMsg(DingTalkTextMsg param) { |
|
|
|
if (StringUtils.isBlank(param.getWebHook())) { |
|
|
|
param.setWebHook(webHook); |
|
|
|
} |
|
|
@ -139,13 +145,70 @@ public class DingdingMsgSender { |
|
|
|
String url = param.getWebHook(); |
|
|
|
url = url.concat("×tamp=" + timestamp + "&sign=" + sign); |
|
|
|
String jsonStrParam = param.getMsgContent(); |
|
|
|
result = HttpClientManager.getInstance().sendPostByJSON(url, jsonStrParam); |
|
|
|
result = this.sendPostByJSON(url, jsonStrParam); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.warn("sendPostByJSON error", e); |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 发送POST 请求 |
|
|
|
* |
|
|
|
* @param url 发送请求的 URL |
|
|
|
* @param param 请求参数,JSON格式 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public Result<String> sendPostByJSON(String url, String param) throws IOException { |
|
|
|
if (StringUtils.isEmpty(url) || StringUtils.isEmpty(param)) { |
|
|
|
throw new IllegalArgumentException("参数不能为空"); |
|
|
|
} |
|
|
|
PrintWriter out = null; |
|
|
|
BufferedReader in = null; |
|
|
|
String result = ""; |
|
|
|
try { |
|
|
|
URL realUrl = new URL(url); |
|
|
|
// 打开和URL之间的连接
|
|
|
|
URLConnection conn = realUrl.openConnection(); |
|
|
|
// 设置通用的请求属性
|
|
|
|
conn.setRequestProperty("accept", "*/*"); |
|
|
|
conn.setRequestProperty("Content-Type", "application/json"); |
|
|
|
conn.setRequestProperty("connection", "Keep-Alive"); |
|
|
|
conn.setRequestProperty("user-agent", |
|
|
|
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
|
|
|
// 发送POST请求必须设置如下两行
|
|
|
|
conn.setDoOutput(true); |
|
|
|
conn.setDoInput(true); |
|
|
|
// 获取URLConnection对象对应的输出流
|
|
|
|
out = new PrintWriter(conn.getOutputStream()); |
|
|
|
// 发送请求参数
|
|
|
|
out.print(param); |
|
|
|
// flush输出流的缓冲
|
|
|
|
out.flush(); |
|
|
|
// 定义BufferedReader输入流来读取URL的响应
|
|
|
|
in = new BufferedReader( |
|
|
|
new InputStreamReader(conn.getInputStream())); |
|
|
|
String line; |
|
|
|
while ((line = in.readLine()) != null) { |
|
|
|
result += line; |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
logger.warn("sendPostByJSON error", e); |
|
|
|
return new Result<String>().error(EpmetErrorCode.SERVER_ERROR.getCode(), e.getMessage()); |
|
|
|
} finally { |
|
|
|
try { |
|
|
|
if (out != null) { |
|
|
|
out.close(); |
|
|
|
} |
|
|
|
if (in != null) { |
|
|
|
in.close(); |
|
|
|
} |
|
|
|
} catch (IOException ex) { |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return new Result<String>().ok(result); |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
for (int i = 0; i < 50; i++) { |
|
|
|