use of javax.management.remote.JMXConnectorServer in project neo4j by neo4j.
the class HotspotManagementSupport method getJMXServiceURL.
@Override
protected JMXServiceURL getJMXServiceURL(KernelData kernel) {
JMXServiceURL url = null;
Log log = kernel.graphDatabase().getDependencyResolver().resolveDependency(LogService.class).getInternalLog(HotspotManagementSupport.class);
try {
Class<?> cal = Class.forName("sun.management.ConnectorAddressLink");
try {
Method importRemoteFrom = cal.getMethod("importRemoteFrom", int.class);
@SuppressWarnings("unchecked") Map<String, String> remote = (Map<String, String>) importRemoteFrom.invoke(null, 0);
url = getUrlFrom(remote);
} catch (NoSuchMethodException ex) {
// handled by null check
}
if (url == null) {
Method importFrom = cal.getMethod("importFrom", int.class);
url = getUrlFrom((String) importFrom.invoke(null, 0));
}
} catch (InvocationTargetException e) {
log.warn("Failed to load local JMX connection URL.", e.getTargetException());
} catch (LinkageError | Exception e) {
log.warn("Failed to load local JMX connection URL.", e);
}
// No previous connection server -- create one!
if (url == null) {
int port = 0;
try {
port = kernel.getConfig().get(setting("jmx.port", INTEGER, "0"));
} catch (NumberFormatException ok) {
// handled by 0-check
}
if (port > 0) {
boolean useSSL = kernel.getConfig().get(setting("jmx.use_ssl", BOOLEAN, "false"));
log.debug("Creating new MBean server on port %s%s", port, useSSL ? " using ssl" : "");
JMXConnectorServer server = createServer(port, useSSL, log);
if (server != null) {
try {
server.start();
} catch (IOException e) {
log.warn("Failed to start MBean server", e);
server = null;
}
if (server != null) {
try {
server.getMBeanServer().registerMBean(server, KernelProxy.createObjectName(kernel.instanceId(), "JMX Server"));
} catch (Exception e) {
log.warn("Failed to register MBean server as JMX bean", e);
}
url = server.getAddress();
}
}
}
}
return url;
}
use of javax.management.remote.JMXConnectorServer in project spring-framework by spring-projects.
the class MBeanClientInterceptorTests method testTestLazyConnectionToRemote.
@Test
public void testTestLazyConnectionToRemote() throws Exception {
assumeTrue(runTests);
Assume.group(TestGroup.JMXMP);
final int port = SocketUtils.findAvailableTcpPort();
JMXServiceURL url = new JMXServiceURL("service:jmx:jmxmp://localhost:" + port);
JMXConnectorServer connector = JMXConnectorServerFactory.newJMXConnectorServer(url, null, getServer());
MBeanProxyFactoryBean factory = new MBeanProxyFactoryBean();
factory.setServiceUrl(url.toString());
factory.setProxyInterface(IJmxTestBean.class);
factory.setObjectName(OBJECT_NAME);
factory.setConnectOnStartup(false);
factory.setRefreshOnConnectFailure(true);
// should skip connection to the server
factory.afterPropertiesSet();
IJmxTestBean bean = (IJmxTestBean) factory.getObject();
// now start the connector
try {
connector.start();
} catch (BindException ex) {
System.out.println("Skipping remainder of JMX LazyConnectionToRemote test because binding to local port [" + port + "] failed: " + ex.getMessage());
return;
}
// should now be able to access data via the lazy proxy
try {
assertEquals("Rob Harrop", bean.getName());
assertEquals(100, bean.getAge());
} finally {
connector.stop();
}
try {
bean.getName();
} catch (JmxException ex) {
// expected
}
connector = JMXConnectorServerFactory.newJMXConnectorServer(url, null, getServer());
connector.start();
// should now be able to access data via the lazy proxy
try {
assertEquals("Rob Harrop", bean.getName());
assertEquals(100, bean.getAge());
} finally {
connector.stop();
}
}
use of javax.management.remote.JMXConnectorServer in project spring-framework by spring-projects.
the class MBeanServerConnectionFactoryBeanTests method testTestValidConnection.
@Test
public void testTestValidConnection() throws Exception {
Assume.group(TestGroup.JMXMP);
JMXConnectorServer connectorServer = getConnectorServer();
connectorServer.start();
try {
MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
bean.setServiceUrl(serviceUrl);
bean.afterPropertiesSet();
try {
MBeanServerConnection connection = bean.getObject();
assertNotNull("Connection should not be null", connection);
// perform simple MBean count test
assertEquals("MBean count should be the same", getServer().getMBeanCount(), connection.getMBeanCount());
} finally {
bean.destroy();
}
} finally {
connectorServer.stop();
}
}
use of javax.management.remote.JMXConnectorServer in project spring-framework by spring-projects.
the class MBeanServerConnectionFactoryBeanTests method testTestWithLazyConnection.
@Test
public void testTestWithLazyConnection() throws Exception {
Assume.group(TestGroup.JMXMP);
MBeanServerConnectionFactoryBean bean = new MBeanServerConnectionFactoryBean();
bean.setServiceUrl(serviceUrl);
bean.setConnectOnStartup(false);
bean.afterPropertiesSet();
MBeanServerConnection connection = bean.getObject();
assertTrue(AopUtils.isAopProxy(connection));
JMXConnectorServer connector = null;
try {
connector = getConnectorServer();
connector.start();
assertEquals("Incorrect MBean count", getServer().getMBeanCount(), connection.getMBeanCount());
} finally {
bean.destroy();
if (connector != null) {
connector.stop();
}
}
}
use of javax.management.remote.JMXConnectorServer in project jdk8u_jdk by JetBrains.
the class JMXServerErrorTest method test.
public void test(String url) throws Exception {
final JMXServiceURL jurl = new JMXServiceURL(url);
final ObjectName kname = new ObjectName(":type=Kaeffer");
final MBeanServer mbs = MBeanServerFactory.newMBeanServer();
final String that = "that";
mbs.registerMBean(new Kaeffer(that), kname);
final MBeanServer kbs = new MBeanServerKaeffer(mbs);
final JMXConnectorServer cs;
try {
cs = JMXConnectorServerFactory.newJMXConnectorServer(jurl, null, kbs);
} catch (MalformedURLException m) {
if ("jmxmp".equals(jurl.getProtocol()) || "iiop".equals(jurl.getProtocol())) {
// OK, we may not have this in the classpath...
System.out.println("WARNING: Skipping protocol: " + jurl);
return;
}
throw m;
}
final ObjectName cname = new ObjectName(":type=JMXConnectorServer");
mbs.registerMBean(cs, cname);
cs.start();
JMXConnector c = null;
try {
c = JMXConnectorFactory.connect(cs.getAddress(), null);
final MBeanServerConnection mbsc = c.getMBeanServerConnection();
final KaefferMBean kaeffer = (KaefferMBean) MBeanServerInvocationHandler.newProxyInstance(mbsc, kname, KaefferMBean.class, false);
final String that1 = kaeffer.getThis();
if (!that.equals(that1))
throw new Exception("Unexpected string returned by " + kname + ": " + that1);
try {
kaeffer.setThis("but not that");
throw new Exception("Expected JMXServerErrorException" + " not thrown" + " for setAttribute \"This\" ");
} catch (JMXServerErrorException jsee) {
if (!(jsee.getCause() instanceof KaefferError)) {
final Exception e = new Exception("Expected JMXServerErrorException" + " is not an instance of " + KaefferError.class.getName() + ": " + jsee.getCause());
e.initCause(jsee);
throw e;
}
System.out.println("Got expected error: " + jsee);
}
try {
kaeffer.doThis("but not that");
throw new Exception("Expected JMXServerErrorException" + " not thrown" + " for invoke \"doThis\" ");
} catch (JMXServerErrorException jsee) {
if (!(jsee.getCause() instanceof KaefferError)) {
final Exception e = new Exception("Expected JMXServerErrorException" + " is not an instance of " + KaefferError.class.getName() + ": " + jsee.getCause());
e.initCause(jsee);
throw e;
}
System.out.println("Got expected error: " + jsee);
}
} finally {
if (c != null)
try {
c.close();
} catch (Exception x) {
System.err.println("Failed to close client: " + x);
throw x;
}
try {
cs.stop();
} catch (Exception x) {
System.err.println("Failed to stop server: " + x);
throw x;
}
}
}
Aggregations