如何获取tomcat中有多少个servlet在内存中

hopegood 2009-08-06 12:07:10
我想写一个监控程序,用于监控在tomcat内存中,有多少个sevlet被装入到内存中。
...全文
297 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
supermax 2009-08-09
  • 打赏
  • 举报
回复
顶一下
islandrabbit 2009-08-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hopegood 的回复:]
对于自己的写的可以使用ServletCounter类,但是,我们知道在tomcat启动的时候,它本身也有一些servlet被加载到内存中,这些servlet我们可以监控吗
[/Quote]

现在想想,jsp 最终也会变成 servlet, 这些怎么监控?
  • 打赏
  • 举报
回复
使用JMX应该可以做到吧。
hopegood 2009-08-07
  • 打赏
  • 举报
回复
对于自己的写的可以使用ServletCounter类,但是,我们知道在tomcat启动的时候,它本身也有一些servlet被加载到内存中,这些servlet我们可以监控吗
rumlee 2009-08-06
  • 打赏
  • 举报
回复
好像做不到吧
islandrabbit 2009-08-06
  • 打赏
  • 举报
回复
想了想还是有可能的,大家看可不可以这样:

首先写个 ServletCounter 类:

public class ServletCounter {

private static Object lock = new Object();
private static ServletCounter instance = null;

private int counter;

private ServletCounter() {
counter = 0;
}

public static ServletCounter getInstance() {
if (instance==null) {
synchronized(lock) {
if (instance == null) {
instance = new ServletCounter();
}
}
}
return instance;
}

public synchronized int getCounter() {
return counter;
}

public synchronized int setCounter(int c) {
counter = c;
return counter;
}

public synchronized int incCounter() {
return (++counter);
}

public synchronized int decCounter() {
return (--counter);
}
}


然后在每个Servelet的init()方法中加入: ServletCounter.getInstance().incCounter();
再在每个Servelet的destroy()方法中加入:ServletCounter.getInstance().decCounter();
不就行了吗。
islandrabbit 2009-08-06
  • 打赏
  • 举报
回复
mark
lovecj6185 2009-08-06
  • 打赏
  • 举报
回复
偶觉得你可以得到web.xml的内容
看到里面servlet的配置
但是有的servlet配了,不一定被启动了阿

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧