Created
November 29, 2018 09:13
-
-
Save 0532/d6dd6f03e1116f0dc40ad4228d70a1b8 to your computer and use it in GitHub Desktop.
钉钉群预警
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.doraemoney.wk.quotecheckplatform.service.support; | |
import com.alibaba.fastjson.JSONObject; | |
import org.asynchttpclient.AsyncHttpClient; | |
import org.asynchttpclient.DefaultAsyncHttpClient; | |
import org.asynchttpclient.DefaultAsyncHttpClientConfig; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.stereotype.Service; | |
@Service | |
@ConfigurationProperties(prefix = "cutomer") | |
public class AsyncAlertService { | |
private Logger logger = LoggerFactory.getLogger("alertLogger"); | |
@Value("${spring.profiles}") | |
private String activeProfile; | |
private AsyncHttpClient asyncHttpClient; | |
private String asyncDingdingUrl; | |
//https://oapi.dingtalk.com/robot/send?access_token=69be9c302d5f9885f07accc23f466805ce16a2ceb194a85803f7a859e18694fd | |
//https://oapi.dingtalk.com/robot/send?access_token=1a826120faf0ebfc6fe2033abc842848adfa01c5573a22462a3c2e78e4f1176e | |
public AsyncAlertService() { | |
DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder(); | |
builder.setConnectTimeout(3000) | |
.setReadTimeout(6000) | |
.setRequestTimeout(6000) | |
.setMaxConnections(100) | |
.setMaxConnectionsPerHost(100); | |
asyncHttpClient = new DefaultAsyncHttpClient(builder.build()); | |
} | |
public void sendMarkdownMsg(String title, String text) { | |
JSONObject markdownValue = new JSONObject(); | |
markdownValue.put("title", title); | |
markdownValue.put("text", String.format("#### 【activeProfiles=%s】 \n %s", activeProfile, text)); | |
JSONObject bodyObj = new JSONObject(); | |
bodyObj.put("msgtype", "markdown"); | |
bodyObj.put("markdown", markdownValue); | |
send(bodyObj.toJSONString()); | |
} | |
public void send(String msg) { | |
logger.info("url={}, msg={}", asyncDingdingUrl, msg); | |
try { | |
asyncHttpClient.preparePost(asyncDingdingUrl).setFollowRedirect(true) | |
.addHeader("Content-Type", "application/json;charset=utf-8") | |
.setBody(msg) | |
.execute(); | |
} catch (Exception e) { | |
// logger.error("send ding msg error", e); | |
} | |
} | |
public String getAsyncDingdingUrl() { | |
return asyncDingdingUrl; | |
} | |
public void setAsyncDingdingUrl(String asyncDingdingUrl) { | |
this.asyncDingdingUrl = asyncDingdingUrl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment