本文整理汇总了Java中javax.management.remote.JMXConnector.getMBeanServerConnection方法的典型用法代码示例。如果您正苦于以下问题:Java JMXConnector.getMBeanServerConnection方法的具体用法?Java JMXConnector.getMBeanServerConnection怎么用?Java JMXConnector.getMBeanServerConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.management.remote.JMXConnector
的用法示例。
在下文中一共展示了JMXConnector.getMBeanServerConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: connect
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
private static void connect(String pid, String address) throws Exception {
if (address == null) {
throw new RuntimeException("Local connector address for " +
pid + " is null");
}
System.out.println("Connect to process " + pid + " via: " + address);
JMXServiceURL url = new JMXServiceURL(address);
JMXConnector c = JMXConnectorFactory.connect(url);
MBeanServerConnection server = c.getMBeanServerConnection();
System.out.println("Connected.");
RuntimeMXBean rt = newPlatformMXBeanProxy(server,
RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
System.out.println(rt.getName());
// close the connection
c.close();
}
示例2: JMXExecutor
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
/**
* Instantiates a new JMXExecutor targeting the VM indicated by the given host/port combination or a full JMX
* Service URL
*
* @param target a host/port combination on the format "host:port" or a full JMX Service URL of the target VM
*/
public JMXExecutor(String target) {
String urlStr;
if (target.matches("^\\w[\\w\\-]*(\\.[\\w\\-]+)*:\\d+$")) {
/* Matches "hostname:port" */
urlStr = String.format("service:jmx:rmi:///jndi/rmi://%s/jmxrmi", target);
} else if (target.startsWith("service:")) {
urlStr = target;
} else {
throw new IllegalArgumentException("Could not recognize target string: " + target);
}
try {
JMXServiceURL url = new JMXServiceURL(urlStr);
JMXConnector c = JMXConnectorFactory.connect(url, new HashMap<>());
mbs = c.getMBeanServerConnection();
} catch (IOException e) {
throw new CommandExecutorException("Could not initiate connection to target: " + target, e);
}
}
示例3: MBeanWrapper
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
MBeanWrapper( int jvmPort ) throws MonitorConfigurationException {
this.jvmPort = jvmPort;
try {
JMXConnector connector = JMXConnectorFactory.newJMXConnector(new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:"
+ this.jvmPort
+ "/jmxrmi"),
null);
connector.connect();
this.connection = connector.getMBeanServerConnection();
} catch (Exception e) {
final String msg = "Error initializing the JMV monitor. Unable to connect to JVM at port "
+ this.jvmPort;
log.error(msg, e);
throw new MonitorConfigurationException(msg, e);
}
}
示例4: tryConnect
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
private static void tryConnect(int port, boolean shouldSucceed) throws Exception {
String jmxUrlStr =
String.format(
"service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi",
port);
JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
HashMap<String, ?> env = new HashMap<>();
boolean succeeded;
try {
JMXConnector c = JMXConnectorFactory.connect(url, env);
c.getMBeanServerConnection();
succeeded = true;
} catch(Exception ex) {
succeeded = false;
}
if (succeeded && !shouldSucceed) {
throw new Exception("Could connect to agent, but should not have been possible");
}
if (!succeeded && shouldSucceed) {
throw new Exception("Could not connect to agent");
}
}
示例5: dotest
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
private static void dotest(JMXServiceURL url, MBeanServer mbs)
throws Exception {
JMXConnectorServer server = null;
JMXConnector client = null;
server = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
server.start();
JMXServiceURL outputAddr = server.getAddress();
System.out.println("Server started ["+ outputAddr+ "]");
client = JMXConnectorFactory.newJMXConnector(outputAddr, null);
client.connect();
System.out.println("Client connected");
MBeanServerConnection connection
= client.getMBeanServerConnection();
System.out.println(connection.getDefaultDomain());
}
示例6: getMBeanServerConnection
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
/**
* Get an MBeanServerConnection object. Return null if there is any failure.
*/
public static MBeanServerConnection getMBeanServerConnection(String host, String jmxPort) {
MBeanServerConnection mbs = null;
try {
Map<String, String[]> env = new HashMap<>();
JMXServiceURL address = new JMXServiceURL(
"service:jmx:rmi://" + host + "/jndi/rmi://" + host + ":" + jmxPort + "/jmxrmi");
JMXConnector connector = JMXConnectorFactory.connect(address, env);
mbs = connector.getMBeanServerConnection();
} catch (Exception e) {
LOG.error("Failed to connect to MBeanServer {}:{}", HostName, jmxPort, e);
}
return mbs;
}
示例7: testWithException
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
private static void testWithException(boolean send)
throws Exception {
ClassLoader zoobyCL = new ZoobyClassLoader();
Class<?> zoobyClass = Class.forName("Zooby", false, zoobyCL);
Object zooby = zoobyClass.newInstance();
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///");
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, pmbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
Object rzooby;
if (send) {
System.out.println("Sending object...");
mbsc.setAttribute(getSetName, new Attribute("It", zooby));
rzooby = getSetInstance.getIt();
} else {
System.out.println("Receiving object...");
getSetInstance.setIt(zooby);
rzooby = mbsc.getAttribute(getSetName, "It");
}
if (!rzooby.getClass().getName().equals("Zooby")) {
throw new Exception("FAILED: remote object is not a Zooby");
}
if (rzooby.getClass().getClassLoader() ==
zooby.getClass().getClassLoader()) {
throw new Exception("FAILED: same class loader: " +
zooby.getClass().getClassLoader());
}
cc.close();
cs.stop();
}
示例8: getMemberId
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
protected static String getMemberId(final String jmxManagerHost, final int jmxManagerPort,
final String memberName) throws Exception {
JMXConnector connector = null;
try {
connector = JMXConnectorFactory.connect(new JMXServiceURL(String.format(
"service:jmx:rmi://%1$s/jndi/rmi://%1$s:%2$d/jmxrmi", jmxManagerHost, jmxManagerPort)));
MBeanServerConnection connection = connector.getMBeanServerConnection();
ObjectName objectNamePattern = ObjectName.getInstance("GemFire:type=Member,*");
QueryExp query = Query.eq(Query.attr("Name"), Query.value(memberName));
Set<ObjectName> objectNames = connection.queryNames(objectNamePattern, query);
assertNotNull(objectNames);
assertFalse(objectNames.isEmpty());
assertEquals(1, objectNames.size());
// final ObjectName objectName = ObjectName.getInstance("GemFire:type=Member,Name=" +
// memberName);
ObjectName objectName = objectNames.iterator().next();
// System.err.printf("ObjectName for Member with Name (%1$s) is %2$s%n", memberName,
// objectName);
return ObjectUtils.toString(connection.getAttribute(objectName, "Id"));
} finally {
IOUtils.close(connector);
}
}
示例9: testStart
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
@Test
public void testStart() throws Exception {
JMXConnector connector = JMXConnectorFactory.connect(
JMXListener.buildJMXServiceURL(connectorPort,connectorPort));
MBeanServerConnection mb = connector.getMBeanServerConnection();
String domain = mb.getDefaultDomain();
Assert.assertTrue("default domain is not correct",
!domain.isEmpty());
connector.close();
}
示例10: test
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
private static void test() throws Exception {
// Create client
//
JMXConnector connector = JMXConnectorFactory.connect(url);
MBeanServerConnection client = connector.getMBeanServerConnection();
// Add listener at the client side
//
client.addNotificationListener(mbean, listener, null, null);
// Cleanup
//
receivedNotifs = 0;
// Ask to send notifs
//
Object[] params = new Object[] {new Integer(nb)};
String[] signatures = new String[] {"java.lang.Integer"};
client.invoke(mbean, "sendNotifications", params, signatures);
// Waiting...
//
synchronized (lock) {
for (int i = 0; i < 10; i++) {
if (receivedNotifs < nb) {
lock.wait(1000);
}
}
}
// Waiting again to ensure no more notifs
//
Thread.sleep(3000);
synchronized (lock) {
if (receivedNotifs != nb) {
throw new Exception("The client expected to receive " +
nb + " notifs, but got " + receivedNotifs);
}
}
// Remove listener
//
client.removeNotificationListener(mbean, listener);
connector.close();
}
示例11: test
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
private static void test() {
try {
JMXServiceURL u = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer server;
JMXServiceURL addr;
JMXConnector client;
MBeanServerConnection mserver;
final ObjectName delegateName =
new ObjectName("JMImplementation:type=MBeanServerDelegate");
final NotificationListener dummyListener =
new NotificationListener() {
public void handleNotification(Notification n,
Object o) {
// do nothing
return;
}
};
server = JMXConnectorServerFactory.newJMXConnectorServer(u,
null,
mbs);
server.start();
addr = server.getAddress();
client = JMXConnectorFactory.newJMXConnector(addr, null);
client.connect(null);
mserver = client.getMBeanServerConnection();
String s1 = "1";
String s2 = "2";
String s3 = "3";
mserver.addNotificationListener(delegateName,
dummyListener, null, s1);
mserver.addNotificationListener(delegateName,
dummyListener, null, s2);
mserver.addNotificationListener(delegateName,
dummyListener, null, s3);
mserver.removeNotificationListener(delegateName,
dummyListener, null, s3);
mserver.removeNotificationListener(delegateName,
dummyListener, null, s2);
mserver.removeNotificationListener(delegateName,
dummyListener, null, s1);
client.close();
server.stop();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
System.exit(1);
}
}
示例12: main
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
System.out.println("---RMIConnectorNullSubjectConnTest starting...");
JMXConnectorServer connectorServer = null;
JMXConnector connectorClient = null;
try {
MBeanServer mserver = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL serverURL = new JMXServiceURL("rmi", "localhost", 0);
connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(serverURL, null, mserver);
connectorServer.start();
JMXServiceURL serverAddr = connectorServer.getAddress();
connectorClient = JMXConnectorFactory.connect(serverAddr, null);
connectorClient.connect();
Field nullSubjectConnField = RMIConnector.class.getDeclaredField("nullSubjectConnRef");
nullSubjectConnField.setAccessible(true);
WeakReference<MBeanServerConnection> weak =
(WeakReference<MBeanServerConnection>)nullSubjectConnField.get(connectorClient);
if (weak != null && weak.get() != null) {
throw new RuntimeException("nullSubjectConnRef must be null at initial time.");
}
MBeanServerConnection conn1 = connectorClient.getMBeanServerConnection(null);
MBeanServerConnection conn2 = connectorClient.getMBeanServerConnection(null);
if (conn1 == null) {
throw new RuntimeException("A connection with null subject should not be null.");
} else if (conn1 != conn2) {
throw new RuntimeException("The 2 connections with null subject are not equal.");
}
conn1 = null;
conn2 = null;
int i = 1;
do {
System.gc();
Thread.sleep(100);
weak = (WeakReference<MBeanServerConnection>)nullSubjectConnField.get(connectorClient);
} while ((weak != null && weak.get() != null) && i++ < 60);
System.out.println("---GC times: " + i);
if (weak != null && weak.get() != null) {
throw new RuntimeException("Failed to clean RMIConnector's nullSubjectConn");
} else {
System.out.println("---RMIConnectorNullSubjectConnTest: PASSED!");
}
} finally {
try {
connectorClient.close();
connectorServer.stop();
} catch (Exception e) {
}
}
}
示例13: run
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
public void run(Map<String, Object> args) {
System.out.println("MXBeanInteropTest1::run: Start") ;
int errorCount = 0 ;
try {
// JMX MbeanServer used inside single VM as if remote.
// MBeanServer mbs = MBeanServerFactory.newMBeanServer();
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
// Print out registered java.lang.management MXBeans found
// in the remote jvm.
printMBeans(mbsc) ;
// For each possible kind of JDK 5 defined MXBean, we retrieve its
// MBeanInfo and print it and we call all getters and print
// their output.
errorCount += doClassLoadingMXBeanTest(mbsc) ;
errorCount += doMemoryMXBeanTest(mbsc) ;
errorCount += doThreadMXBeanTest(mbsc) ;
errorCount += doRuntimeMXBeanTest(mbsc) ;
errorCount += doOperatingSystemMXBeanTest(mbsc) ;
errorCount += doCompilationMXBeanTest(mbsc) ;
errorCount += doGarbageCollectorMXBeanTest(mbsc) ;
errorCount += doMemoryManagerMXBeanTest(mbsc) ;
errorCount += doMemoryPoolMXBeanTest(mbsc) ;
// Terminate the JMX Client
cc.close();
} catch(Exception e) {
Utils.printThrowable(e, true) ;
throw new RuntimeException(e);
}
if ( errorCount == 0 ) {
System.out.println("MXBeanInteropTest1::run: Done without any error") ;
} else {
System.out.println("MXBeanInteropTest1::run: Done with "
+ errorCount
+ " error(s)") ;
throw new RuntimeException("errorCount = " + errorCount);
}
}
示例14: testInterface
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
private static <T> void testInterface(Class<T> c, boolean nullTest)
throws Exception {
System.out.println("Testing " + c.getName() +
(nullTest ? " for null values" : "") + "...");
MBeanServer mbs = MBeanServerFactory.newMBeanServer();
JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXConnector cc = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = cc.getMBeanServerConnection();
NamedMXBeans namedMXBeans = new NamedMXBeans(mbsc);
InvocationHandler ih =
nullTest ? new MXBeanNullImplInvocationHandler(c, namedMXBeans) :
new MXBeanImplInvocationHandler(c, namedMXBeans);
T impl = c.cast(Proxy.newProxyInstance(c.getClassLoader(),
new Class[] {c},
ih));
ObjectName on = new ObjectName("test:type=" + c.getName());
mbs.registerMBean(impl, on);
System.out.println("Register any MXBeans...");
Field[] fields = c.getFields();
for (Field field : fields) {
String n = field.getName();
if (n.endsWith("ObjectName")) {
String objectNameString = (String) field.get(null);
String base = n.substring(0, n.length() - 10);
Field f = c.getField(base);
Object mxbean = f.get(null);
ObjectName objectName =
ObjectName.getInstance(objectNameString);
mbs.registerMBean(mxbean, objectName);
namedMXBeans.put(objectName, mxbean);
}
}
try {
testInterface(c, mbsc, on, namedMXBeans, nullTest);
} finally {
try {
cc.close();
} finally {
cs.stop();
}
}
}
示例15: init
import javax.management.remote.JMXConnector; //导入方法依赖的package包/类
/**
* @throws Exception
* initializes MBeanServer
*/
public void init() throws Exception {
err("init: server=" + server + ";port=" + port + ";service=" + service
+ ";localVMUrl=" + localVMUrl);
String url_string = null;
// build connection url
if (localVMUrl != null) {
// use
// jstat -snap <vmpid> | grep sun.management.JMXConnectorServer.address
// to get url
url_string = localVMUrl;
err("url string for local pid = " + localVMUrl + " = " + url_string);
} else if (!port.isEmpty() && !server.isEmpty()) {
// using server and port
url_string = "service:jmx:rmi:///jndi/rmi://" + server + ":" + port
+ "/jmxrmi";
} // else url stays null
// Create an RMI connector client and
// connect it to the RMI connector server
if (url_string == null) { // assume local vm (for example for Testing)
mbsc = ManagementFactory.getPlatformMBeanServer();
} else {
JMXServiceURL url = new JMXServiceURL(url_string);
err("Create RMI connector and connect to the RMI connector server" + url);
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
// Get an MBeanServerConnection
//
err("\nGet an MBeanServerConnection");
mbsc = jmxc.getMBeanServerConnection();
}
// Get domains from MBeanServer
//
err("\nDomains:");
String domains[] = mbsc.getDomains();
Arrays.sort(domains);
for (String domain : domains) {
err("\tDomain = " + domain);
}
// Get MBeanServer's default domain
//
err("\nMBeanServer default domain = " + mbsc.getDefaultDomain());
// Get MBean count
//
err("\nMBean count = " + mbsc.getMBeanCount());
// Query MBean names for specific domain "hadoop" and service
ObjectName query = new ObjectName("Hadoop:service=" + service + ",*");
hadoopObjectNames = new ArrayList<ObjectName>(5);
err("\nQuery MBeanServer MBeans:");
Set<ObjectName> names = new TreeSet<ObjectName>(mbsc
.queryNames(query, null));
for (ObjectName name : names) {
hadoopObjectNames.add(name);
err("Hadoop service: " + name);
}
}