注册
DEM短信接口实现
技术分享/ 文章详情 /

DEM短信接口实现

Sunflowers 2023/07/06 1469 3 0

DM 企业管理器的英文全称 DM Enterprise Manager,基于web界面 的一个管理器,包括监控、告警、安装部署等。
DEM提供了短信告警推送接口,需要借助WEB-INF/lib/demsdk.jar,实现com.dameng.dem.server.util.IPoneNotyfy接口,根据实际告警需求编辑java程序打包放入到WEB-INF/lib目录下,重启DEM web容器,在DEM系统配置中开启短信通知并填写jar包完整名称即可。
目前新版本建议使用com.dameng.dem.server.util.INotify
短信推送实现:

  1. 前期准备
    请求方式:http
    请求地址:http://xxxxx
    提交方式:POST
    请求参数:
  • mobile,String
  • content,String 短信内容
    请求数据格式:application/x-www-form-urlencoded
    调用返回格式:JSON or XML(返回内容包括code,data,msg)
  1. 具体实现
  • 短信内容编辑
//告警时间:gmtHappen
//告警资源:resType + resName
//告警内容:[level] message
//告警内容建议进行一下截断避免过长导致短信无法发送成功,截取长度根据具体情况设置
notifyContent.append("\u544a\u8b66\u65f6\u95f4\uFF1A").append(notify.gmtHappen); notifyContent.append(LINE_SEPARATOR);
notifyContent.append("\u544a\u8b66\u8d44\u6e90\uFF1A").append(notify.resType).append(notify.resName); notifyContent.append(LINE_SEPARATOR); notifyContent.append("\u544A\u8B66\u5185\u5BB9\uFF1A\u005B").append(notify.level).append("\u005D").append(notify.message.substring(0,50)); 

  • 确认推送的目标URL&实现信息推送
HttpClient client = new HttpClient(); 
PostMethod method = new PostMethod(NOTIFY_URL);
client.getParams().setContentCharset(CHAR_SET);
method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset="+CHAR_SET);
NameValuePair[] data = {
new NameValuePair("mobile",cellPhone),
new NameValuePair("content",content)};
method.setRequestBody(data);
client.executeMethod(method);
  • 获取返回值
返回值类型:分为JSON 和 XML类型
- 对于JSON类型
通过字符获取再转换为JSON获取每个key对应的value
String SubmitResult =method.getResponseBodyAsString();
JSONObject jsonObject =  new JSONObject(SubmitResult);
code = jsonObject.getString("code");
rdata=jsonObject.getString("data");
msg = jsonObject.getString("msg");
- 对于XML类型
Document doc = DocumentHelper.parseText(SubmitResult);
Element root = doc.getRootElement();
//返回参数需要修改
String code = root.elementText("code");
String msg = root.elementText("msg");
String smsid = root.elementText("smsid");
  • 代码编写后导出选择jar file,放入apache安装目录webapps/dem/WEB_INF/lib下
  • Jar放在指定目录下后,重启apache,开启自定义短信推送
    image.png
  • 修改告警勾选自定义通知,告警发生后会发送告警信息至手机
    image.png
  1. 完整代码
package com.my.notify;
import com.dameng.common.util.*;
import com.dameng.dem.server.util.INotify;
import com.dameng.dem.server.util.Notify;
import com.dameng.dem.server.util.NotifyUser;
import com.google.gwt.thirdparty.json.JSONException;
import com.google.gwt.thirdparty.json.JSONObject;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;

public class OaNotify
    implements INotify
{

    public OaNotify()
    {
    }
	private static final String CHAR_SET = "utf-8";
	//http地址
    private static final String NOTIFY_URL = StringUtil.trimToEmpty(System.getProperty("phone_url", "http://xxx.xxx.xxx.xxx:xxxx/shortMessage/send"));
    public static final String LINE_SEPARATOR = System.getProperty("line.separator");
    public boolean send(NotifyUser user, Notify notify)
    {
    	StringBuilder notifyContent = new StringBuilder(256);
    	StringBuilder userContent = new StringBuilder(256);
    	String content;
    	String cellPhone; 	
    	userContent.append(user.phone);
    	//告警时间:gmtHappen
    	//告警资源:resType + resName
    	//告警内容:[level] message
    	notifyContent.append("\u544a\u8b66\u65f6\u95f4\uFF1A").append(notify.gmtHappen);
    	notifyContent.append(LINE_SEPARATOR);
    	notifyContent.append("\u544a\u8b66\u8d44\u6e90\uFF1A").append(notify.resType).append(notify.resName);
    	notifyContent.append(LINE_SEPARATOR);
    	notifyContent.append("\u544A\u8B66\u5185\u5BB9\uFF1A\u005B").append(notify.level).append("\u005D").append(notify.message.substring(0,50));    	
        content = StringUtil.trimToEmpty(notifyContent.toString());
        if(StringUtil.isEmpty(content))
        	//消息不能为空
            throw new RuntimeException("\u6D88\u606F\u4E0D\u80FD\u4E3A\u7A7A");
        cellPhone = StringUtil.trimToEmpty(userContent.toString());
        if(StringUtil.isEmpty(cellPhone))
        	//号码不能为空
            throw new RuntimeException("\u53F7\u7801\u4E0D\u80FD\u4E3A\u7A7A");
        content = content.replace("<br/>", "\u003b");
        try
        {
            post(cellPhone,content);
        }
        catch(Exception e)
        {
            throw new RuntimeException(e);
        }
        return true;
    }
	public void post(String cellPhone,String content) {
		String code;
		String rdata;
		String msg;
		HttpClient client = new HttpClient(); 
		PostMethod method = new PostMethod(NOTIFY_URL);
		client.getParams().setContentCharset(CHAR_SET);
		method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset="+CHAR_SET);
		NameValuePair[] data = {
				new NameValuePair("mobile",cellPhone),
				new NameValuePair("content",content)};
		method.setRequestBody(data);
		try {
			client.executeMethod(method);
			
			String SubmitResult =method.getResponseBodyAsString();
//			System.out.println(SubmitResult);
//返回xml格式的
//			Document doc = DocumentHelper.parseText(SubmitResult);
//			Element root = doc.getRootElement();
//			//返回参数需要修改
//			String code = root.elementText("code");
//			String rdata=root.elementText("data");
//			String msg = root.elementText("msg");
//返回JSON格式的
			JSONObject jsonObject =  new JSONObject(SubmitResult);
			code = jsonObject.getString("code");
			rdata=jsonObject.getString("data");
			msg = jsonObject.getString("msg");
			 if("0".equals(code)){
				 //发送成功
					System.out.println(msg);
			}else
			 {
				//发送失败
				System.out.println(msg);
			}
		} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
//		} catch (DocumentException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
		}
	}

}
评论
后发表回复

作者

文章

阅读量

获赞

扫一扫
联系客服