`

监控tomcat 启动和停止向管理员发送通知

阅读更多
当服务上的web容器由于故障停止工作了,怎样通知管理员呢?
我们可以实现ServletContextListener 接口的方法。

1代码:
package com.boce.server.listener;

import java.util.Enumeration;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* web 服务启动和关闭时监听
* @author gjp
*
*/
public class ServerStopListener implements ServletContextListener{

@Override
public void contextDestroyed(ServletContextEvent event) {
System.out.println("发送邮件通知管理员====================================stop.............");

Enumeration<String> e = event.getServletContext().getAttributeNames();
while(e.hasMoreElements()){
System.out.println("name:"+e.nextElement());
}
System.err.println(event.getServletContext());

}

@Override
public void contextInitialized(ServletContextEvent event) {
System.out.println("############################start#######################");
System.out.println("start:"+event.getServletContext());
System.out.println("source:"+event.getSource());
}

}


2配置:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<display-name>Archetype Created Web Application</display-name>


<listener>
<listener-class>com.boce.server.listener.ServerStopListener</listener-class>
</listener>

</web-app>



测试日志:






  • 大小: 42.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics