jmc监控工具

java7以上的版本,jdk的bin目录下自带jmc工具,可以用来监控java应用的性能,貌似比jvisualvm全面一些

用法是先在java服务启动的地方加入jmxServer代码,绑定端口,建立起jmx服务


jmxServer = new JmxServer(config.getProperty(RmiServer.RMI_SERVER_HOST), 10713);
		jmxServer.start();
		SessionFactory sf = context.getBean(SessionFactory.class);
		jmxServer.addProvider(new EhcacheMonitor(sf, "gameserver", false, true));

public class JmxServer {


	private static final Logger logger = LoggerFactory.getLogger(JmxServer.class);
	private MBeanServer mbServer;
	private String hostName;
	private int port;
	private JMXConnectorServer connectorServer;


	public JmxServer(String hostName, int port) {
		this.hostName = hostName;
		this.port = port;
		this.mbServer = ManagementFactory.getPlatformMBeanServer();
	}


	public void start() {
		System.setProperty("java.rmi.server.hostname", hostName);


		Properties properties = new Properties();
		properties.put(ConnectorBootstrap.PropertyNames.USE_AUTHENTICATION, "true");
		properties.put(ConnectorBootstrap.PropertyNames.USE_SSL, "false");
		connectorServer = ConnectorBootstrap.initialize(String.valueOf(port), properties);


		logger.info("Start the JMX server on {}:{}", hostName, port);
	}


	public void stop() {
		try {
			if (this.connectorServer != null) {
				this.connectorServer.stop();
			}
		} catch (Exception e) {
			logger.error("", e);
		}
	}


	public void addProvider(MBeanProvider provider) throws Exception {
		provider.register(this.mbServer);
	}
}


服务建立起来以后,需要在java的\jre7\lib\management的目录下加入jmxremote.password,jmxremote.access身份验证文件,win操作系统下需要修改这两个文件的访问权限,修改方法,选择文件右键,安全,权限

jmxremote.password这个里面可以设置用户的访问名称,和密码,在最后面加入

# monitorRole  QED
# controlRole   R&D
monitorRole 123456
controlRole 123456



jmxremote.access这个里面可以设置用户的访问权限,在最后面加入

# o The "monitorRole" role has readonly access.  
# o The "controlRole" role has readwrite access and can create the standard
#   Timer and Monitor MBeans defined by the JMX API.
monitorRole   readonly
controlRole   readwrite \
              create javax.management.monitor.*,javax.management.timer.*,com.sun.management.*,com.oracle.jrockit.*  \
              unregister


这两个文件改好放对位置后,java服务启动时加入如下参数,

-XX:MaxDirectMemorySize=1024m -XX:+UnlockCommercialFeatures -XX:+FlightRecorder -XX:FlightRecorderOptions=defaultrecording=true

这样,jmx服务就算搭建完成了,后面的就是使用jmc客户端去连接这个服务,监控性能,连接的时候身份验证就是之前那个jmxremote.password文件里配的用户名和密码

连上去之后可以看到JMC工具的强大,特别是飞行记录仪,蛮好用的




你可能感兴趣的:(JAVA编程基础)