tomcat用线程监控另一个服务
程序员文章站
2022-06-04 18:22:35
...
- 1 起一个监控服务,写一个listner,服务启动即开启监控线程
public class CheckServerListner implements ServletContextListener {
private CheckServerThread myThread;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
if (myThread != null && myThread.isInterrupted()) {
myThread.interrupt();
}
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
String str = null;
if (str == null && myThread == null) {
myThread = new CheckServerThread();
myThread.start(); // servlet 上下文初始化时启动 socket
System.out.println("监控IPTVservice服务线程启动");
}
}
}
- 2 写一个监控线程,没五分钟监控一次
public class CheckServerThread extends Thread {
@Override
public void run() {
while(true){
try {
sleep(5*60*1000);
//1、 调用被监控服务,
// 判断 正常响应则没事了
//2、异常则调用邮箱服务,告警
//String result = Util.httpGet("http://192.168.1.118:8081/IPTVService/checkServer.do");
String result = Util.httpGet("http://xxx.xxx.xxx.xxx:8082/IPTVService/checkServer.do");
System.out.println(result);
if(!"OK".equals(result)){
Util.httpGet("http://xx.xx.xx.xx:8888/web/iptv/guangdong/[email protected]&checkCode=sichuanggaojing");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+com.shareinfo.common.init.InitData.getServerIP(request)+":"+request.getServerPort()+path+"/";
String user = request.getParameter("user");
String password = request.getParameter("password");
final String email = request.getParameter("email");
final String checkCode = request.getParameter("checkCode");
new Thread(new Runnable(){
public void run(){
try{
com.shareinfo.common.util.mail.MailUtil.sendMessage("smtp.qiye.163.com", "[email protected]", "password", email, "验证码", "你本次登录的验证码是:"+checkCode+",5min内有效!", "text/html;charset=utf-8");
}catch(Exception e){
e.printStackTrace();
}
}
}).start();
out.print("success");
%>
上一篇: 算法基础 递归---上台阶
下一篇: Git基本使用指南