java接口报警_服务器接口状态监控,异常报警到。(示例代码...
java接⼝报警_服务器接⼝状态监控,异常报警到电⼦邮箱。
(⽰例代码)
使⽤HTTP URL,发送请求并接收服务器接⼝响应消息,如5分钟内发⽣5次异常,则把异常信息发送到指定电⼦邮箱中;指定时间内异常未达到5次则清零。
1,Http Post
package m;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.HttpURLConnection;
import java.SocketException;
import java.URL;
import java.URLEncoder;
SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import MailSenderInfo.MyAuthenticator;
public class test_postout {
static String sessionId = "";
static int num;
static int j;
// 默认的url
String surl = "IP:Port/ibis/faceService/compareFaces";
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("请输⼊服务器接⼝URL:");
String surl = sc.nextLine();
System.out.println("请输⼊图⽚img1所在⽂件夹的路径:");
四大名绣是指String folderPath1 = sc.nextLine();
folderPath1 = place("\\", "/");
System.out.println("请输⼊图⽚img1的名称:");
String imageName1 = sc.nextLine();
System.out.println("请输⼊图⽚img2所在⽂件夹的路径:");
String folderPath2 = sc.nextLine();
folderPath2 = place("\\", "/");
邮箱发
System.out.println("请输⼊图⽚img2的名称:");
String imageName2 = sc.nextLine();
// 执⾏图⽚上传接收⽅法
postOut(surl, folderPath1, imageName1, folderPath2, imageName2);
sc.close();
}
李英爱吧public static void postOut(String surl, String folderPath1, String imageName1, String folderPath2, String imageName2) {
test_parm mp = new test_parm();
// 获取⽂件夹图⽚调⽤接⼝次数
int num = 0;
// 接⼝调⽤不成功SocketException次数,达到门限就告警
int i = 0;
// 5分钟计时器,j>300秒时,如果异常计数器未达到门限值则,计数器清零
int j = 0;
// 脸脸⽐对返回code不是0,的门限告警值
int k = 0;
// 调⽤接⼝未能返回200 OK消息
int l = 0;
// EX的门限告警
int m = 0;
do {
// 开始⽐对时间点t1
long t1 = System.currentTimeMillis();
// ⽐对次数num++;
num = mp.getNum() + 1;
mp.setNum(num);
两小儿辩日译文// http属性设置
try {
URL url = new URL(surl);
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(false);
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("contentType", "utf-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
Date date = new Date();
SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSS"); DataOutputStream out = new OutputStream());
String imgpath1 = "";
String imgpath2 = "";
imgpath1 = folderPath1 + "/" + imageName1;
imgpath2 = folderPath2 + "/" + imageName2;
// 把jpg图⽚转换为⼆进制流
System.out.println("imgpath1:" + imgpath1);
System.out.println("imgpath2:" + imgpath2);
// 构造表单消息体
//System.out.println("构造消息体前的时间:" + System.currentTimeMillis()
//+ "***************************************************");
// 图⽚base64编码
String img1 = null;
String img2 = null;
try {
//System.out.println("编码前" + System.currentTimeMillis());
img1 = de(PicBASE64(folderPath1 + "/" + imageName1), "UTF-8"); img2 = de(PicBASE64(folderPath2 + "/" + imageName2), "UTF-8"); //System.out.println("编码后" + System.currentTimeMillis());
} catch (Exception e) {
throw new RuntimeException(e);
}
String data = ("{" + "\"" + "methodName" + "\"" + ":" + "\"" + "compareFaces" + "\"" + "," + "\""
+ "channel" + "\"" + ":" + "\"" + "0400" + "\"" + "," + "\"" + "img1" + "\"" + ":" + "\"" + img1
+ "\"" + "," + "\"" + "img2" + "\"" + ":" + "\"" + img2 + "\"" + "}");
out.writeBytes("requestData" + "=" + data);
out.flush();
//System.out.println("HTTP发送时间" + System.currentTimeMillis());
//long t2 = System.currentTimeMillis();
out.close();
// 接收返回值
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
//System.out.println("HTTP开始接收时间" + System.currentTimeMillis());
InputStream is = InputStream();
// HTTP返回值
mp.setCode(new ResponseCode()).toString());
// HTTP返回消息
mp.ResponseMessage());
//System.out.println("HTTP 返回code值为:" + mp.getCode());
//System.out.println("HTTP 返回message值为:" + mp.getMessage());
if (!(mp.getCode().equals("200")) || !(mp.getMessage().equals("OK"))) {
l = mp.getL() + 1;
mp.setL(l);
// l++;
if (mp.getL() > 4) {
mp.setFlagl(1);
}
}
System.out.println("接收字节数:" +is.available());
// 接收消息内容
while ((len = is.read(buf)) != -1) {
System.out.println("Img1" + "\t" + imageName1 + "\t" + "Img2" + "\t" + imageName2 + "\t" + "⽐对次数:" + "\t" + num );
// 返回结果写⼊txt⽂件
baos.write(buf, 0, len);
//System.out.println("HTTP接收完成时间" + System.currentTimeMillis());
//System.out.println("HTTP发送接收⽤时" + (System.currentTimeMillis() - t2) + "毫秒");
FileOutputStream fos = new FileOutputStream(folderPath1 + "/", true);关于高尚的名言
wow烹饪攻略String str2;
//替换str2 ="第" + num + "次⽐对:" + "\t" + "Img1" + "\t" + imageName1 + "\t" + "Img2" + "\t" + imageName2 + "\t" + formater.format(date)
//替换+ "\t" + String("UTF-8").replace("\n", "") + "\n";
str2 ="第" + num + "次⽐对:" + "\t" + "Img1" + "\t" + imageName1 + "\t" + "Img2" + "\t" + imageName2 + "\t" + formater.format(date)
+ "\t" + String("UTF-8") + "\n";
byte[] word = Bytes();
fos.write(word, 0, word.length);
fos.flush();
fos.close();
}
baos.flush();
//System.out.println("预期的HTTP 200OK 实际返回的内容:" + String("UTF-8"));
// 初始化数组的值
String[] a1 = new String[11];
a1[0] = "a0";
a1[1] = "a1";
a1[2] = "a2";
a1[3] = "a3";
a1[4] = "a4";
a1[5] = "a5";
a1[6] = "a6";
a1[7] = "a7";
a1[8] = "a8";
a1[9] = "a9";
a1[10] = "a10";
// 截取{"code":0,"message":"......","data":{"sim":0.9815}}
mp.setRs(new String("UTF-8")));
String a[] = mp.getRs().split("\"");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。