📄 userclient.java
字号:
package book.jmx.examples;
import javax.management.*;
import java.util.List;
public class UserClient {
public void run() {
// Find an agent from this JVM. Null argument will return
// a list of all MBeanServer instances.
List srvrList = MBeanServerFactory.findMBeanServer(null);
MBeanServer server =
(MBeanServer)srvrList.iterator().next();
try {
// register the MBean
ObjectName name = new ObjectName("example:name=user");
server.registerMBean(new DynamicUser(), name);
// Invoke the printInfo operation on an
// uninitialized MBean instance.
Object result = server.invoke(
name, // MBean name
"printInfo", // operation name
null, // no parameters
null // void signature
);
System.out.println("Non-initialized User object:");
System.out.println(result);
// Create the list of management attribute value pairs
AttributeList list = new AttributeList();
list.add(new Attribute("Name", "John"));
list.add(new Attribute("Address", "Strawberry Street"));
list.add(new Attribute("PhoneNumbers", new String[]
{
"555-1232",
null,
null
}
));
// Init the MBean instance by calling setAttributes()
server.setAttributes(name, list);
// Invoke the printInfo to retrieve the updated state
result = server.invoke(
name, // MBean name
"printInfo", // operation name
null, // no parameters
null // void signature
);
System.out.println("Initialized User object:");
System.out.println(result);
}
catch (MalformedObjectNameException e) {
e.printStackTrace();
}
catch (InstanceNotFoundException e) {
e.printStackTrace();
}
catch (MBeanException e) {
e.getTargetException().printStackTrace();
}
catch (ReflectionException e) {
e.printStackTrace();
}
catch (InstanceAlreadyExistsException e) {
e.printStackTrace();
}
catch (NotCompliantMBeanException e) {
e.printStackTrace();
}
catch (RuntimeOperationsException e) {
e.getTargetException().printStackTrace();
}
}
/**
* Main method for the client. This will instantiate an agent
* and register the User MBean to it.
*/
public static void main(String[] args) {
MBeanServer server =
MBeanServerFactory.createMBeanServer();
new UserClient().run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -