use of javax.management.remote.JMXConnector in project opennms by OpenNMS.
the class DefaultJmxConnector method createConnection.
public JmxServerConnectionWrapper createConnection(JmxConnectionConfig config) throws JmxServerConnectionException {
try {
// If we're trying to create a connection to a localhost address...
if (config.isLocalConnection()) {
// this JVM's MBeanServer directly.
return new PlatformMBeanServerConnector().createConnection();
}
// Create URL
final String urlString = config.getUrl();
final JMXServiceURL url = new JMXServiceURL(urlString);
LOG.debug("JMX: {} - {}", config.getFactory(), url);
// Apply password strategy
final Map<String, Object> env = new HashMap<>();
config.getPasswordStategy().apply(env, config);
// Create connection and connect
final JMXConnector connector = JMXConnectorFactory.newJMXConnector(url, env);
try {
connector.connect(env);
} catch (SecurityException x) {
throw new JmxServerConnectionException("Security exception: bad credentials", x);
}
// Wrap Connection
MBeanServerConnection connection = connector.getMBeanServerConnection();
JmxServerConnectionWrapper connectionWrapper = new DefaultConnectionWrapper(connector, connection);
return connectionWrapper;
} catch (MalformedURLException e) {
throw new JmxServerConnectionException(e);
} catch (IOException e) {
throw new JmxServerConnectionException(e);
}
}
use of javax.management.remote.JMXConnector in project opennms by OpenNMS.
the class JmxRemoteAdminIT method canConnect.
@Test
public void canConnect() throws Exception {
final InetSocketAddress addr = m_testEnvironment.getServiceAddress(ContainerAlias.OPENNMS, 18980);
final String hostString = "localhost".equals(addr.getHostString()) ? "127.0.0.1" : addr.getHostString();
final int port = addr.getPort();
final RMISocketFactory socketFactory = new JMXTestClientSocketFactory();
System.setProperty("sun.rmi.transport.tcp.responseTimeout", "5000");
final Callable<Integer> getRmiConnection = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
LOG.debug("getRmiConnection({}:{})", hostString, port);
try {
final Registry registry = LocateRegistry.getRegistry(hostString, port, socketFactory);
final String[] bound = registry.list();
LOG.debug("bound={}", Arrays.asList(bound));
if (bound.length > 0) {
return bound.length;
}
} catch (final Exception e) {
}
return null;
}
};
await().atMost(5, MINUTES).pollInterval(10, SECONDS).until(getRmiConnection, greaterThanOrEqualTo(1));
final Callable<Integer> getJmxConnection = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
LOG.debug("getJmxConnection({}:{})", hostString, port);
try {
RMISocketFactory.setSocketFactory(socketFactory);
final Map<String, Object> env = new HashMap<>();
final String[] credentials = { "admin", "admin" };
env.put(JMXConnector.CREDENTIALS, credentials);
env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, socketFactory);
final String urlString = String.format("service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi", hostString, port);
LOG.debug("getJmxConnection(): connecting to {}", urlString);
final JMXServiceURL url = new JMXServiceURL(urlString);
final JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
final MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
LOG.debug("mbeanCount={}", mbsc.getMBeanCount());
if (mbsc.getMBeanCount() > 0) {
return mbsc.getMBeanCount();
}
} catch (final Exception e) {
}
return null;
}
};
await().atMost(5, MINUTES).pollInterval(10, SECONDS).until(getJmxConnection, greaterThanOrEqualTo(1));
}
use of javax.management.remote.JMXConnector in project wildfly by wildfly.
the class SarResourceInjectionTestCase method testMBean.
@Test
public void testMBean() throws Exception {
final JMXConnector connector = JMXConnectorFactory.connect(managementClient.getRemoteJMXURL(), DefaultConfiguration.credentials());
try {
final MBeanServerConnection mbeanServer = connector.getMBeanServerConnection();
final ObjectName objectName = new ObjectName("jboss:name=X");
Assert.assertTrue((Boolean) mbeanServer.invoke(objectName, "resourcesInjected", null, null));
} finally {
IoUtils.safeClose(connector);
}
}
use of javax.management.remote.JMXConnector in project wildfly by wildfly.
the class SarValueFactoryInjectionTestCase method testMBean.
@Test
public void testMBean() throws Exception {
final JMXConnector connector = JMXConnectorFactory.connect(managementClient.getRemoteJMXURL());
try {
final MBeanServerConnection mbeanServer = connector.getMBeanServerConnection();
final ObjectName objectName = new ObjectName("jboss:name=TargetBean");
Assert.assertEquals("Injection using value-factory without method arguments failed", 2, ((Integer) mbeanServer.getAttribute(objectName, "SourceCount")).intValue());
Assert.assertEquals("Injection using value-factory with method argument failed", 4, ((Integer) mbeanServer.getAttribute(objectName, "CountWithArgument")).intValue());
} finally {
IoUtils.safeClose(connector);
}
}
use of javax.management.remote.JMXConnector in project wildfly by wildfly.
the class SarTestCase method testMBean.
@Test
public void testMBean() throws Exception {
final JMXConnector connector = JMXConnectorFactory.connect(managementClient.getRemoteJMXURL());
try {
MBeanServerConnection mbeanServer = connector.getMBeanServerConnection();
ObjectName objectName = new ObjectName("jboss:name=test,type=config");
mbeanServer.getAttribute(objectName, "IntervalSeconds");
mbeanServer.setAttribute(objectName, new Attribute("IntervalSeconds", 2));
} finally {
IoUtils.safeClose(connector);
}
}
Aggregations