当前页面: 开发资料首页 → J2EE 专题 → WebLogic Server 9.0 应用的配置管理接口
摘要: WLS 9可以说是自WLS 6.0以来最重大的一次改革,不仅增加了许多功能,而且对许多旧有的子系统重新进行了设计。为了集成第三方管理系统,WLS提供了与JMX完全兼容的标准接口来执行所有的管理操作。
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();
}
}
}