use of javax.management.MBeanServerConnection in project opennms by OpenNMS.
the class JmxCommand method execute.
protected void execute() throws CmdLineException, CmdRunException {
try (JMXConnector connector = getJmxConnector()) {
MBeanServerConnection mbeanServerConnection = connector.getMBeanServerConnection();
execute(mbeanServerConnection);
} catch (MBeanServerQueryException | JMException | IOException e) {
throw new CmdRunException(e);
}
}
use of javax.management.MBeanServerConnection in project opennms by OpenNMS.
the class DefaultJmxCollector method collect.
@Override
public void collect(JmxCollectorConfig config, MBeanServer mBeanServer, JmxSampleProcessor sampleProcessor) throws JmxServerConnectionException {
Map<String, String> mergedStringMap = new HashMap<>(config.getServiceProperties());
if (mBeanServer != null) {
mergedStringMap.putAll(mBeanServer.getParameterMap());
}
JmxConnectionManager connectionManager = new DefaultConnectionManager(config.getRetries());
try (JmxServerConnectionWrapper connectionWrapper = connectionManager.connect(config.getConnectionName(), InetAddressUtils.addr(config.getAgentAddress()), mergedStringMap, null)) {
Objects.requireNonNull(connectionWrapper, "connectionWrapper should never be null");
Objects.requireNonNull(connectionWrapper.getMBeanServerConnection(), "connectionWrapper.getMBeanServerConnection() should never be null");
final MBeanServerConnection concreteConnection = connectionWrapper.getMBeanServerConnection();
collect(concreteConnection, config.getJmxCollection(), sampleProcessor);
}
}
use of javax.management.MBeanServerConnection 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.MBeanServerConnection 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.MBeanServerConnection in project wildfly by wildfly.
the class MBeanTCCLTestCase method testTCCLInMBeanInvocation.
/**
* Tests the MBean was deployed successfully and can be invoked. The fact that the MBean deployed successfully is a sign that the TCCL access from within the MBean code, worked fine
*
* @throws Exception
*/
@Test
public void testTCCLInMBeanInvocation() throws Exception {
final MBeanServerConnection mBeanServerConnection = this.getMBeanServerConnection();
final ObjectName mbeanObjectName = new ObjectName("wildfly:name=tccl-test-mbean");
final int num1 = 3;
final int num2 = 4;
// invoke the operation on MBean
final Integer sum = (Integer) mBeanServerConnection.invoke(mbeanObjectName, "add", new Object[] { num1, num2 }, new String[] { Integer.TYPE.getName(), Integer.TYPE.getName() });
Assert.assertEquals("Unexpected return value from MBean: " + mbeanObjectName, num1 + num2, (int) sum);
}
Aggregations