springjmxclient.java

来自「jmx在spring中的实现」· Java 代码 · 共 46 行

JAVA
46
字号
/*
======================================================================

  SpringJmxClient.java

  Created by Claude Duguay
  Copyright (c) 2005

======================================================================
*/

package com.claudeduguay.jmx.demo.client;

import java.util.*;

import javax.management.*;
import javax.management.remote.*;

public class SpringJmxClient implements NotificationListener
{
	public void handleNotification(Notification notification, Object handback) 
	{
		System.out.println("Notification: " + notification.getMessage());
	}
	
	public static void main(String[] args)
		throws Exception
	{
		SpringJmxClient listener = new SpringJmxClient();
		
		String address = "service:jmx:rmi:///jndi/rmi://localhost:8999/jmxrmi";
		JMXServiceURL serviceURL = new JMXServiceURL(address);
		Map<String,Object> environment = null;
		JMXConnector connector = JMXConnectorFactory.connect(serviceURL, environment);
		MBeanServerConnection mBeanConnection = connector.getMBeanServerConnection();
		
		ObjectName exampleServiceName = ObjectName.getInstance("Services:name=ExampleService");
		mBeanConnection.addNotificationListener(exampleServiceName, listener, null, null);
		
		mBeanConnection.invoke(exampleServiceName, "startService", null, null);
		mBeanConnection.setAttribute(exampleServiceName, new Attribute("propertyValue", "new value"));
		System.out.println(mBeanConnection.getAttribute(exampleServiceName, "propertyValue"));
		mBeanConnection.invoke(exampleServiceName, "stopService", null, null);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?