com.bea:name=name,type=type[,typeofparentmbean=nameofparentmbean][,typeofparentmbean1=nameofparentmbean1]...
import java.io.ioexception;
import java.net.malformedurlexception;
import java.util.hashtable;
import java.util.map;
import javax.management.attribute;
import javax.management.mbeanserverconnection;
import javax.management.objectname;
import javax.management.remote.jmxconnector;
import javax.management.remote.jmxconnectorfactory;
import javax.management.remote.jmxserviceurl;
import javax.naming.context;
public class jmxsample {
// jmxsample class definition - do not copy this line
private static string username = "weblogic";
private static string password = "weblogic";
private static string protocol = "t3";
private static string hostname = "localhost";
private static int port = 7001;
private static string jndi = "/jndi/";
private static string runtime_uri = "weblogic.management.mbeanservers.runtime";
private static string edit_uri = "weblogic.management.mbeanservers.edit";
private static string runtime_service = "com.bea:name=runtimeservice,type=weblogic.management.mbeanservers.runtime.runtimeservicembean";
private static string edit_service = "com.bea:name=editservice,type=weblogic.management.mbeanservers.edit.editservicembean";
private mbeanserverconnection runtimeserviceconnection = null;
private mbeanserverconnection editserviceconnection = null;
public static void main(string[] args) {
jmxsample jmx = new jmxsample();
jmx.runtests();
}
//连到特定server的特定mbean server上。
//wls9支持jdk 1.5,所以我这里用上了tiger的新特性,泛型
public mbeanserverconnection getconnection(string uri) throws ioexception,
malformedurlexception {
//描述mbean server的地址
jmxserviceurl serviceurl = new jmxserviceurl(protocol, hostname, port, jndi + uri);
hashtable h = new hashtable();
h.put(context.security_principal, username);
h.put(context.security_credentials, password);
h.put(jmxconnectorfactory.protocol_provider_packages, "weblogic.management.remote");
//构造jmxconnector对象。
jmxconnector connector = jmxconnectorfactory.connect(serviceurl, (map)h);
//连接到mbean server
mbeanserverconnection connection = connector.getmbeanserverconnection();
return connection;
}
//包括两类操作,前面一部分是读取domain配置,后面一部分是修改domain配置。
public void runtests() {
try {
runtimeserviceconnection = getconnection(runtime_uri);
editserviceconnection = getconnection(edit_uri);
objectname runtimeon = new objectname(runtime_service);
objectname editon = new objectname(edit_service);
// 获得 server
objectname server = (objectname) runtimeserviceconnection
.getattribute(runtimeon, "serverconfiguration");
// 获得并显示当前 server 名
system.out.println("server name "
+ runtimeserviceconnection.getattribute(runtimeon,
"servername"));
// 获得并显示 domain 名
objectname domain = (objectname) runtimeserviceconnection
.getattribute(runtimeon, "domainconfiguration");
system.out.println("domain name "
+ runtimeserviceconnection.getattribute(domain, "name"));
// since we have the server already we will just reuse it to
// 获得并显示当前监听端口
system.out.println("listen port "
+ runtimeserviceconnection.getattribute(server,
"listenport").tostring());
// 获得并显示ssl端口
objectname ssl = (objectname) runtimeserviceconnection
.getattribute(server, "ssl");
system.out.println("ssl listen port "
+ runtimeserviceconnection.getattribute(ssl, "listenport")
.tostring());
// 获得并显示生产模式
system.out.println("production mode enabled "
+ runtimeserviceconnection.getattribute(domain,
"productionmodeenabled").tostring());
//获得并显示当前部署的所有应用
objectname[] apps = (objectname[]) runtimeserviceconnection
.getattribute(domain, "appdeployments");
for (objectname app : apps) {
system.out.println("app deployment : "
+ runtimeserviceconnection.getattribute(app, "name")
.tostring());
}
objectname mgron = (objectname) editserviceconnection.getattribute(
editon, "configurationmanager");
//对edit mbean的事务控制包括startedit,save,activate等。这些方法都必须通过类反射来执行。
//第一个参数表示如果调用startedit时候等待获得锁的时间。第二个参数表示如果2分钟之内没完成所有操作,则自动失去锁。
object[] params = new object[] { new integer(60000),
new integer(120000) };
string[] paramtypes = new string[] { "java.lang.integer",
"java.lang.integer" };
//start edit将返回一个domain mbean的句柄,可认为是整个配置树的根。
objectname domainmgr = (objectname) editserviceconnection.invoke(mgron,
"startedit", params, paramtypes);
if (domainmgr == null) {
// 无法在规定时间内获得锁
throw new exception("somebody else is editing already");
}
//对domain mbean的notes属性做一个修改
attribute notes = new attribute("notes", "blah blah blah");
editserviceconnection.setattribute(domainmgr, notes);
//列出未保存的修改
object[] unsavedlist = (object[])editserviceconnection.getattribute(mgron, "changes");
for(object o:unsavedlist)
system.out.println("unsaved change: " + o.tostring());
//如果不保存,则可执行"undo"操作
editserviceconnection.invoke(mgron, "save", null, null);
//列出未激活的修改
object[] unactivatedlist = (object[])editserviceconnection.getattribute(mgron, "changes");
for(object o:unactivatedlist)
system.out.println("unactivated change: " + o.tostring());
//激活这个修改
params = new object[]{new long(120000)};
paramtypes = new string[]{"java.lang.long"};
objectname taskon =
(objectname) editserviceconnection.invoke(mgron, "activate", params, paramtypes);
//列出已经激活的修改
object[] activatedlist=(object[])editserviceconnection.getattribute(taskon, "changes");
for(object o:activatedlist)
system.out.println("activated change: " + o.tostring());
//最近激活的任务。wls默认保存最近10笔激活的任务历史
for(object o:activatedlist)
system.out.println("activated change: " + o.tostring());
objectname[] completedobjects=(objectname[])editserviceconnection.getattribute(mgron,
"completedactivationtasks");
for(objectname on:completedobjects){
system.out.println("user who started activation: " +editserviceconnection.getattribute(on, "user"));
system.out.println("task state:" +editserviceconnection.getattribute(on, "state"));
system.out.println("start time:" +editserviceconnection.getattribute(on, "starttime"));
object[] completedlist=(object[])editserviceconnection.getattribute(on, "changes");
for(object o:completedlist)
system.out.println("changes activated: " + o.tostring());
}
//清除已经完成的激活的任务
editserviceconnection.invoke(mgron, "purgecompletedactivationtasks", null, null);
// 由于edit属性是异步的,在此我们等待操作完成。
params = new object[]{new long(120000)};
paramtypes = new string[]{"java.lang.long"};
editserviceconnection.invoke(taskon, "waitfortaskcompletion", params, paramtypes);
} catch (throwable t) {
t.printstacktrace();
}
}
}
import java.io.ioexception;
import java.net.malformedurlexception;
import java.util.iterator;
import java.util.set;
import java.util.properties;
import javax.management.j2ee.management;
import javax.management.j2ee.managementhome;
import javax.management.attributenotfoundexception;
import javax.management.instancenotfoundexception;
import javax.management.objectname;
import javax.management.queryexp;
import javax.naming.context;
import javax.naming.initialcontext;
import javax.naming.namingexception;
import javax.ejb.createexception;
public class getjmonames {
static string url = "t3://localhost:8001";
static string user = "weblogic";
static string password = "weblogic";
public static void main(string[] args) {
try {
getalljmonames();
} catch (exception e) {
system.out.println(e);
}
}
public static context getinitialcontext() throws namingexception {
properties p = new properties();
p.put(context.initial_context_factory,
"weblogic.jndi.wlinitialcontextfactory");
p.put(context.provider_url, url);
if (user != null) {
p.put(context.security_principal, user);
if (password == null)
password = "";
p.put(context.security_credentials, password);
}
return new initialcontext(p);
}
//通过jndi获得javax.management.j2ee.managementhome接口,并构造mejb的远程接口实例。
public static management getmejbremote() throws ioexception,
malformedurlexception, namingexception, createexception {
context context = getinitialcontext();
managementhome home = (managementhome) context.lookup("ejb.mgmt.mejb");
management bean = home.create();
return bean;
}
public static void getalljmonames() {
try {
management rhome = getmejbremote();
string string = "";
objectname name = new objectname(string);
queryexp query = null;
set allnames = rhome.querynames(name, query);
iterator nameiterator = allnames.iterator();
while (nameiterator.hasnext()) {
objectname on = (objectname) nameiterator.next();
system.out.println(on.getcanonicalname() + "\n");
}
} catch (exception ex) {
ex.printstacktrace();
}
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器