📄 exportmanager.java
字号:
package book.jmx.examples;
import java.util.*;
import javax.naming.*;
import javax.management.*;
public class ExportManager {
final static String SERVER_DELEGATE =
"JMImplementation:type=MBeanServerDelegate";
// maps connector object name to export properties
private HashMap connectorMap = new HashMap();
// stores the naming context
private Context ctx = null;
// reference to the mbean server with connectors
private MBeanServer server = null;
// default constructor
public ExportManager() { }
public void start(String agentID) throws NamingException {
ctx = new InitialContext();
ObjectName serverDelegate = null;
try {
server = (MBeanServer)MBeanServerFactory.
findMBeanServer(agentID).get(0);
// listen for MBean registration notifications
serverDelegate = new ObjectName(SERVER_DELEGATE);
server.addNotificationListener(
serverDelegate,
new RegistrationListener(),
null,
null
);
// query for existing connector servers
ObjectName connectorQuery = new ObjectName("Connector:*");
Set connectors = server.queryNames(connectorQuery, null);
Iterator it = connectors.iterator();
while (it.hasNext()) {
ObjectName name = null;
try {
name = (ObjectName)it.next();
// try to retrieve export properties
connectorMap.put(name,
server.getAttribute(name, "ExportProperties"));
}
catch (JMException e) {
System.out.println(
name + " does not have ExportProperties attribute."
);
}
}
}
catch (JMException e) {
e.printStackTrace();
}
}
public void export(String exportName) throws NamingException {
MBeanHandleImpl handle = new MBeanHandleImpl();
Iterator it = connectorMap.values().iterator();
// add the known export properties to the handle
while (it.hasNext())
handle.addProperties((Properties)it.next());
// bind to naming service
ctx.rebind(exportName, handle);
}
// This notification listener updates the export properties based
// on connector server registration and unregistration events
class RegistrationListener implements NotificationListener {
public void handleNotification(Notification n, Object hb) {
if (!(n instanceof MBeanServerNotification))
return;
MBeanServerNotification notif = (MBeanServerNotification)n;
ObjectName name = notif.getMBeanName();
String notifType = notif.getType();
String domain = name.getDomain();
if (!domain.equalsIgnoreCase("Connector"))
return;
if (notifType.equals(
MBeanServerNotification.REGISTRATION_NOTIFICATION)) {
try {
connectorMap.put(name,
server.getAttribute(name, "ExportProperties"));
}
catch (JMException e) {
System.out.println(
name + " does not have ExportProperties attribute."
);
}
}
else if (notifType.equals(
MBeanServerNotification.UNREGISTRATION_NOTIFICATION)) {
connectorMap.remove(name);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -