困扰
开发的服务端java程序运行崩溃后,由于没有监控,总是不能及时发现!!!
解决方法
通过手机端实现服务器应用运行状态的实时监控和异常报警,记录以备后用
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
public void getStatus() {
try {
newState();
} catch (Exception e) {
// pushToken需要在比特信(bit.dayutec.cn)生成获取
String pushToken = "************";
String url = "https://push.dayutec.cn/bit/send?pushToken="+pushToken;
String title = "登录异常通知";
String content = e.toString();
sendMsg(url, title, content);
}
}
public static void sendMsg(String url, String title, String content) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("title",title);
jsonObject.put("content",content);
StringEntity msgEntity = new StringEntity(jsonObject.toString(), "UTF-8");
String content = null;
HttpPost httpPost = new HttpPost(url);
msgEntity.setContentEncoding("UTF-8");
msgEntity.setContentType("application/json");
httpPost.setEntity(msgEntity);
try (
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = httpClient.execute(httpPost);
) {
if (response.getStatusLine().getStatusCode() == 200) {
content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("请求发送成功!发送内容为:" + content);
} else {
System.out.println("请求发送失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}