计算机二级:使用Tomcat的jmx服务计算机二级考试
文章作者 100test 发表时间 2009:04:30 08:55:13
来源 100Test.Com百考试题网
2009年下半年全国计算机等级考试你准备好了没?考计算机等级考试的朋友,2009年下半年全国计算机等级考试时间是2009年9月19日至23日。更多优质资料尽在百考试题论坛 百考试题在线题库
Tomcat 5.5.20可以使用JMX服务进行管理操作。下面介绍如何查看Tomcat提供哪些JMX服务,并如何使用这些JMX服务。
1. 使用JDK1.5自带的JConsole程序查看Tomcat的JMX服务
要让JConsole能查看到Tomcat的JMX服务,需要Tomcat启动一个管理口。由于tomcat5.5.20缺省启动文件不提供JMX服务接口 加入下面红色内容到catalina.bat:
set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.port=1090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file="Ê.TALINA_BASE%conflogging.properties" 启动Tomcat5.5.20
再运行jdk1.5的jconsole程序
d:jdk1.5binjconsole nnnnn(nnnn 是tomcat的进程号 用Task Manager查)
2. 调用Tomcat的JMX服务,如停止、启动web应用
写一个JavaBean,用来调用Tomcat的JMX服务,关键方法如下:
public static boolean callWebModuleMBeanMethod(String appName,String methodName) throws Exception{
MBeanServer mBeanServer = null.
if (MBeanServerFactory.findMBeanServer(null).size() >. 0) {
mBeanServer = (MBeanServer) MBeanServerFactory.findMBeanServer(
null).get(0).
} else {
throw new Exception("cann t find catalina MBeanServer").
}
Set names = null.
try {
names = mBeanServer.queryNames(new ObjectName(
"*:j2eeType=WebModule,name=//localhost/" appName ",*"), null).
} catch (Exception e) {
throw new Exception("cann t find " appName " web moudule mbean! can t undeploy web app.n" e.getMessage()).
}
if(names==null || names.size()==0) {
log.debug("can t find " appName " web moudule mbean!").
return false.
}
ObjectName oname =null.
Iterator it = names.iterator().
if (it.hasNext()) {
oname=(ObjectName) it.next().
}
if(oname==null)
return false.
try {
mBeanServer.invoke(oname,methodName,null,null).
return true.
} catch (Exception e) {
throw new Exception("can t " methodName " " appName " web application!n" e.getMessage()).
}
}
public static void main(String[] args){
callWebModuleMBeanMethod("app1","stop"). //停止web应用app1
callWebModuleMBeanMethod("app1","start"). //启动web应用app1
}