zhangli-s 发表于 2017-2-17 06:56:05

jmx实现监控weblogic中的jms队列信息(一)

  需要加入jar包:weblogic.jar,wlclient.jar,wljmsclient.jar,wls-api.jar,这些jar包在weblogic安装目录下可以找到(bea\weblogic92\server\lib,这是9.2版本),
  下面是简单的代码,通过java代码基本能监控queue中的数据情况

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 MonitorJMS {
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();
}
/*
* Get an array of ServerRuntimeMBeans
*/
public static ObjectName[] getServerRuntimes() throws Exception {
return (ObjectName[]) connection.getAttribute(service,
"ServerRuntimes");
}
public void getJmsQueueInfo() throws Exception {
ObjectName[] serverRT = getServerRuntimes();
ObjectName JMSRT = (ObjectName) connection.getAttribute(serverRT,
"JMSRuntime");
ObjectName[] JMSServers = (ObjectName[]) connection.getAttribute(JMSRT,
"JMSServers");
int JMSServer_Length = (int) JMSServers.length;
for (int x = 0; x < JMSServer_Length; x++) {
//jmsserver名称
String JMSServer_name = (String) connection.getAttribute(JMSServers,
"Name");
ObjectName[] JMSDests = (ObjectName[]) connection.getAttribute(JMSServers,
"Destinations");
int JMSdest_Length = (int) JMSDests.length;
for (int y = 0; y < JMSdest_Length; y++) {
//queue名称
String queue_name = (String) connection.getAttribute(JMSDests,
"Name");
long pendingmcount = (Long) connection.getAttribute(JMSDests,
"MessagesPendingCount");
//当前队列中有多少条记录
long currentcount = (Long) connection.getAttribute(JMSDests,
"MessagesCurrentCount");
System.out.println(y
+ "--: "
+ JMSServer_name
+ ": "
+ queue_name
+ "   : "
+ pendingmcount
+ "   : "
+ currentcount);
}
}
}
public static void main(String[] args) throws Exception {
String hostname = "127.0.0.1";
String portString = "7001";
String username = "weblogic";
String password = "weblogic";
MonitorJMS s = new MonitorJMS();
initConnection(hostname, portString, username, password);
s.getJmsQueueInfo();
connector.close();
}
}
   更多weblogic MBean信息可以查看http://docs.oracle.com/cd/E13222_01/wls/docs92/wlsmbeanref/core/index.html
页: [1]
查看完整版本: jmx实现监控weblogic中的jms队列信息(一)