设为首页 收藏本站
查看: 634|回复: 0

[经验分享] 通过weblogic 9 mbean监控其应用状态

[复制链接]

尚未签到

发表于 2017-2-17 08:03:21 | 显示全部楼层 |阅读模式
  通过weblogic开放的jmx mbean可以对部署其上的应用\jms\datasource。。。很多内容进行查看和管理,下面这个demo只是借鉴了一下weblogic提供的例子作一个简单的测试。如果想通过这个实现监控,可以为这段代码加上定时设置(通过TimerTask或者quartz)。


下面这段代码主要查看应用的状态以及jms相关情况(需要依赖weblogic.jar)。

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
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 PrintServerState {
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;
// Initializing the object name for DomainRuntimeServiceMBean
// so it can be used throughout the class.
static {
try {
service = new ObjectName(
"com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
} catch (MalformedObjectNameException e) {
throw new AssertionError(e.getMessage());
}
}
/*
* Initialize connection to the Domain Runtime MBean Server
*/
public static void initConnection(String hostname, String portString, String username, String password)
throws IOException, MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.domainruntime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver);
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");
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
}
/*
* Print an array of ServerRuntimeMBeans.
* This MBean is the root of the runtime MBean hierarchy, and
* each server in the domain hosts its own instance.
*/
public static ObjectName[] getServerRuntimes() throws Exception {
return (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
}
/*
* Iterate through ServerRuntimeMBeans and get the name and state
*/
public void printNameAndState() throws Exception {
ObjectName[] serverRT = getServerRuntimes();
System.out.println("got domain runtimes");
int length = (int) serverRT.length;
for (int i = 0; i < length; i++) {
String name = (String) connection.getAttribute(serverRT, "Name");
String state = (String) connection.getAttribute(serverRT, "Type");
System.out.println("Server name: " + name + ".   Server state: " + state);
}
}
public void printAppNameAndState() throws Exception {
ObjectName[] serverRT = getServerRuntimes();
for (int k = 0; k < serverRT.length; k++) {
ObjectName[] appRT = (ObjectName[]) connection.getAttribute(serverRT[k], "ApplicationRuntimes");
int length = appRT.length;
for (int i = 0; i < length; i++) {
String appName = (String) connection.getAttribute(appRT, "Name");
ObjectName[] compRT = (ObjectName[]) connection.getAttribute(appRT, "ComponentRuntimes");
for (int j = 0; j < compRT.length; j++) {
int appState = ((Integer) connection.getAttribute(compRT[j], "DeploymentState")).intValue();
String type = (String) connection.getAttribute(compRT[j], "Type");
System.out.println(k + "|" + j + "|Server name: " + appName + ".    Server type: " + type
+ "   Server state: " + appState);
}
}
}
}
public void printJMS() throws Exception {
ObjectName[] serverRT = getServerRuntimes();
ObjectName JMSRT = (ObjectName) connection.getAttribute(serverRT[0], "JMSRuntime");
ObjectName[] JMSServers = (ObjectName[]) connection.getAttribute(JMSRT, "JMSServers");
int JMSServer_Length = (int) JMSServers.length;
for (int x = 0; x < JMSServer_Length; x++) {
String JMSServer_name = (String) connection.getAttribute(JMSServers[x], "Name");
ObjectName[] JMSDests = (ObjectName[]) connection.getAttribute(JMSServers[x], "Destinations");
int JMSdest_Length = (int) JMSDests.length;
for (int y = 0; y < JMSdest_Length; y++) {
String queue_name = (String) connection.getAttribute(JMSDests[y], "Name");
long pendingmcount = (Long) connection.getAttribute(JMSDests[y], "MessagesPendingCount");
long currentcount = (Long) connection.getAttribute(JMSDests[y], "MessagesCurrentCount");
System.out.println(y + "|jms server name: " + JMSServer_name + "  jms name: " + queue_name
+ "   pending: " + pendingmcount + "   current: " + currentcount);
} //for y
} //for  x
}
public static void main(String[] args) throws Exception {
String hostname = "127.0.0.1";
String portString = "8001";
String username = "weblogic";
String password = "weblogic";
PrintServerState s = new PrintServerState();
initConnection(hostname, portString, username, password);
s.printNameAndState();
s.printAppNameAndState();
s.printJMS();
connector.close();
}
}
   更详细的内容,可以查看weblogic相关文档http://download.oracle.com/docs/cd/E13222_01/wls/docs90/wlsmbeanref/core/index.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-343149-1-1.html 上篇帖子: Spring+Hibernate+Struts程序移植Weblogic问题集 下篇帖子: JVM技术:如何中断weblogic中stuck thread
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表