在我的前一篇文章中,我们讨论了如何管理9.0之前的WLS。在9.0中发生了许多变化,重要的变化包括:采用了基于JMX 1.2的JSR 160的Management API,以及包含新功能的MbeanServer。JSR 160定义了JMX的远程API,使客户机无需在类路径中使用任何的专有类就可以连接到远程的JMX MbeanServer。既然已经有了远程连接到JMX Agent的标准方式,BEA就不鼓励使用其远程扩展MbeanHome和RemoteMBeanServer了。BEA也否决了(当查找一个Mbean时)MbeanHome通常返回的类型安全存根,现在推荐使用直接的JMX或WLST。在9.0中,config.xml是基于模式的,这使用户可以使用诸如XMLSpy之类的工具脱机编辑配置文件。在9.0以前的版本中,只有一个MbeanServer,用来驻留所有的Mbean(Configuration、Runtime以及Custom)。在9.0中,WLS有四个Mbean服务器,根据它们的功能,每个都提供了到不同MBean的访问。Edit MBean Server提供到可编辑的Configuration Mbean的访问,Domain Runtime MBean Server提供到域中所有Runtime Mbean和只读Configuration MBean的联邦访问,Runtime MBean Server提供只到Runtime或只读Configuration Mbean的访问,Compatibility MBeanServer提供到所有不鼓励使用的MBean的访问。
下面我们对9.0以前的WLS版本和9.0版本做个一对一的比较。
| WLS Management(6.0到8.1) |
WLS Management(9.0及以后) |
| 专有的远程API,用于访问MbeanServer。 |
采用基于标准的JSR 160。不鼓励使用 MbeanHome和RemoteMBeanServer。 |
| 一个MbeanServer,驻留所有的Mbean。 |
4个Mbean服务器(MBS),根据功能驻留不同的Mbean。 |
| 不基于模式的config.xml。 |
基于模式的config.xml。 |
| 在MBean层次结构中存在许多故障链接。 |
所有的MBean都完全链接,使层次结构可以越过。 |
| MbeanHome是创建和删除所有Mbean的Factory。 |
每个父Mbean都作为创建和删除其子Mbean的Factory。 |
| 配置更改机制是原子的。 |
对配置更改引入了事务语义。 |
| 对配置更改没有控制,而且通常无法预料。 |
对配置进行更改时需要锁定,更多控制(可以撤销更改、终止编辑等等)。 |
| AdminMBeanHome提供域的联邦视图。 |
DomainRuntime MBeanServer提供域的联邦视图。 |
| 通过weblogic.Admin进行命令行管理。 |
否决weblogic.Admin,引入了WLST。 |
| WebLogic域名在MBean的ObjectName中,例如: mydomain:Name=myserver,Type=Server。 |
更改为com.bea,例如: com.bea:Name=myserver,Type=Server。 |
| config.xml表示所有的WLS配置。 |
{domain_dir}/config目录表示WLS配置,即,配置分布于多个xml文件中(JMS、JDBC和WLDF配置在不同的文件中)。 |
| 无。 |
获得根MBean的服务MBean。 |
下面是在WLS 9.0中获得ServerMBean的代码:
// All JMX imports that you need
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
// Assuming you know these
String protocol = "t3"; // can be t3, http, iiop
String hostname = "localhost";
int port = 7001;
String uri = "/jndi/" +"weblogic.management.mbeanservers.runtime";
// Create a JMX Service URL
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, uri);
// Setup connection properties
Hashtable h = new Hashtable();
h.put(Context.SECURITY_PRINCIPAL, user);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote"); // this one points to a BEA class
// Create a connection to the RuntimeMBeanServer
JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h);
// Get the RuntimeMBeanServer
MBeanServerConnection connection = connector.getMBeanServerConnection();
// Get the Runtime Service MBean
ObjectName service = new ObjectName("weblogic:Name=RuntimeService");
// Get the Domain MBean
ObjectName domain = (ObjectName) connection.getAttribute(service,"DomainConfiguration");
// Get the ServerMBean
ObjectName server = (ObjectName) connection.getAttribute(domain,"ServerConfiguration");
String name = (String) connection.getAttribute(server, "Name");
System.out.println("Server's name "+name);
或者可以编写一个简单的WLST脚本来实现,代码会少很多,但是肯定也会需要在类路径中使用Weblogic类。下面是可能的脚本:
# assuming you know the username, password and url
connect(username, password, url)
svr = cmo.lookupServer('myserver')
print svr.getName()
本文概述了9.0中的Management API的变化。更多信息请参考BEA电子文档。
原文出处:http://dev2dev.bea.com/blog/sghattu/archive/2005/09/wls_90_manageme_1.html