|
Java学习者论坛
大家好!
我有個想法,就是想写一個JMX程序动态修改Tomcat的应用监听端口;这样动态修改端口是否可行?
我的代码实现后却老是不生效,請各位知道的帮忙看一下是哪里的问題.
代 码:
import javax.management.remote.JMXServiceURL;
import javax.management.remote.JMXConnectorFactory;
import javax.management.modelmbean.RequiredModelMBean;
import javax.management.remote.JMXConnector;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.ObjectInstance;
import javax.management.Attribute;
import org.apache.catalina.ServerFactory;
import java.util.Set;
import java.util.Iterator;
public class TCManager{
// Connect to URL remote MBServer
public MBeanServerConnection initialRemoteSrv(String url){
JMXServiceURL sreUrl = null;
JMXConnector conn = null;
MBeanServerConnection msConn = null;
try{
sreUrl = new JMXServiceURL(url);
conn = JMXConnectorFactory.connect(sreUrl);
msConn = conn.getMBeanServerConnection();
}catch(Exception e){
e.printStackTrace();
}
return msConn;
}
// Alter MBean Attribute
public void chgAttr(String strObj, MBeanServerConnection msConn, String attr, int value){
ObjectName obj = null;
Attribute attribute = null;
Set set = null;
Iterator itr = null;
String tmp = null;
try{
obj = new ObjectName(strObj);
set = msConn.queryMBeans(null, null);
itr = set.iterator();
while(itr.hasNext()){
ObjectInstance instance = (ObjectInstance)itr.next();
ObjectName objName = instance.getObjectName();
tmp = objName.getKeyProperty("port");
if(tmp != null){
System.out.println("Object: ["objName"] Key Value:"+tmp);
}
}
System.out.println("Initial Port Value: "+msConn.getAttribute(obj, attr));
attribute = new Attribute(attr, new Integer(value));
msConn.setAttribute(obj, attribute);
System.out.println("After alter Value: "+msConn.getAttribute(obj, attr));
}catch(Exception e){
e.printStackTrace();
}
}
// Alter MBean Attribute
public void chgAttr(String strObj, MBeanServerConnection msConn, String attr, String value){
ObjectName obj = null;
Attribute attribute = null;
Set set = null;
Iterator itr = null;
String tmp = null;
try{
obj = new ObjectName(strObj);
set = msConn.queryMBeans(null, null);
itr = set.iterator();
while(itr.hasNext()){
ObjectInstance instance = (ObjectInstance)itr.next();
ObjectName objName = instance.getObjectName();
tmp = objName.getKeyProperty("port");
if(tmp != null){
System.out.println("Object: ["objName"] Key Value:"+tmp);
}
}
System.out.println("Initial Port Value: "+msConn.getAttribute(obj, attr));
attribute = new Attribute(attr, new String(value));
msConn.setAttribute(obj, attribute);
System.out.println("After alter Value: "+msConn.getAttribute(obj, attr));
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
String url = "service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi";
String attr = "port";
String obj = "Standalone:type=Connector,port=8080";
int value = 7001;
MBeanServerConnection msConn = null;
TCManager tcm = new TCManager();
msConn = tcm.initialRemoteSrv(url);
tcm.chgAttr(obj, msConn, attr, value);
}
}
名户用 于 2009-6-4 xia午10:33 修改过
欢迎来到Javaxue习者论坛,转载請注明地址:http://www.javaxxz.com. |
|