本文提供了一种监控Jboss性能信息的方式——Jmx,通过这种方式监控的前提是要先配置好jboss的Jmx功能,具体的配置方法,前面已经详细记述过。
当配置好Jmx后,我们通过jconsole可以看到Jboss的相关信息:
根据相关属性,我们可以使用Java代码对jboss进行监控,具体的实现代码如下:
package iwwenbo;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
/**
* 通过jmx方式采集jboss信息
*
* @author https://www.4spaces.org
*
*/
public class JbossJmxCollector {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
Date ct = cal.getTime();
public void collector() {
String jmxUrl = "service:jmx:rmi:///jndi/rmi://192.168.1.105:1099/jmxrmi";
JMXConnector connector = null;
MBeanServerConnection mbsc = null;
String operatingSystemName = null;// 系统名称
try {
JMXServiceURL serviceURL = new JMXServiceURL(jmxUrl);
Map<String, String[]> map = new HashMap<String, String[]>();
String[] credentials = new String[] { "admin", "jboss123" };
map.put("jmx.remote.credentials", credentials);
connector = JMXConnectorFactory.connect(serviceURL, map);
mbsc = connector.getMBeanServerConnection();
ObjectName opObjName = new ObjectName("java.lang:type=OperatingSystem");
operatingSystemName = mbsc.getAttribute(opObjName, "Name").toString() + " "
+ mbsc.getAttribute(opObjName, "Version").toString();
System.out.println(operatingSystemName);// 打印出系统信息
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
JbossJmxCollector collect = new JbossJmxCollector();
collect.collector();
}
}
上面的代码只是简单的对系统信息进行输出,更多的属性信息可以在jconsole中进行查看。