use of javax.management.remote.JMXConnector in project opennms by OpenNMS.
the class AbstractSystemReportPlugin method getConnection.
private MBeanServerConnection getConnection() {
final List<Integer> ports = new ArrayList<Integer>();
Integer p = Integer.getInteger("com.sun.management.jmxremote.port");
if (p != null)
ports.add(p);
ports.add(18980);
ports.add(1099);
for (final Integer port : ports) {
LOG.trace("Trying JMX at localhost:{}/jmxrmi", port);
try {
JMXServiceURL url = new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi", port));
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
return jmxc.getMBeanServerConnection();
} catch (final Exception e) {
LOG.debug("Unable to get JMX connection to OpenNMS on port {}.", port, e);
}
}
return null;
}
use of javax.management.remote.JMXConnector in project Activiti by Activiti.
the class DeploymentsJMXClientTest method testDeploymentsJmxClient.
@SuppressWarnings("unchecked")
@Test
public void testDeploymentsJmxClient() throws IOException, InterruptedException, MalformedObjectNameException, AttributeNotFoundException, MBeanException, ReflectionException, InstanceNotFoundException, IntrospectionException {
String hostName = Utils.getHostName();
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://" + hostName + ":10111/jndi/rmi://" + hostName + ":1099/jmxrmi/activiti");
ProcessEngineConfiguration processEngineConfig = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
ProcessEngine processEngine = processEngineConfig.buildProcessEngine();
// wait for jmx server to come up
Thread.sleep(500);
JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
ObjectName deploymentsBeanName = new ObjectName("org.activiti.jmx.Mbeans:type=Deployments");
Thread.sleep(500);
// no process deployed yet
List<List<String>> deployments = (List<List<String>>) mbsc.getAttribute(deploymentsBeanName, "Deployments");
assertEquals(0, deployments.size());
// deploy process remotely
URL fileName = Thread.currentThread().getContextClassLoader().getResource("org/activiti/management/jmx/trivialProcess.bpmn");
mbsc.invoke(deploymentsBeanName, "deployProcessDefinition", new String[] { "trivialProcess.bpmn", fileName.getFile() }, new String[] { String.class.getName(), String.class.getName() });
// one process is there now, test remote deployments
deployments = (List<List<String>>) mbsc.getAttribute(deploymentsBeanName, "Deployments");
assertNotNull(deployments);
assertEquals(1, deployments.size());
assertEquals(3, deployments.get(0).size());
String firstDeploymentId = deployments.get(0).get(0);
// test remote process definition
List<List<String>> pdList = (List<List<String>>) mbsc.getAttribute(deploymentsBeanName, "ProcessDefinitions");
assertNotNull(pdList);
assertEquals(1, pdList.size());
assertEquals(5, pdList.get(0).size());
assertNotNull(pdList.get(0).get(0));
assertEquals("My process", pdList.get(0).get(1));
// version
assertEquals("1", pdList.get(0).get(2));
// not suspended
assertEquals("false", pdList.get(0).get(3));
assertEquals("This process to test JMX", pdList.get(0).get(4));
// redeploy the same process
mbsc.invoke(deploymentsBeanName, "deployProcessDefinition", new String[] { "trivialProcess.bpmn", fileName.getFile() }, new String[] { String.class.getName(), String.class.getName() });
// now there should be two deployments
deployments = (List<List<String>>) mbsc.getAttribute(deploymentsBeanName, "Deployments");
assertNotNull(deployments);
assertEquals(2, deployments.size());
assertEquals(3, deployments.get(0).size());
assertEquals(3, deployments.get(1).size());
// there should be two process definitions, one with version equals to two
pdList = (List<List<String>>) mbsc.getAttribute(deploymentsBeanName, "ProcessDefinitions");
assertNotNull(pdList);
assertEquals(2, pdList.size());
assertEquals(5, pdList.get(0).size());
assertEquals(5, pdList.get(1).size());
// check there is one with version= = 1 and another one with version == 2, other attributed are the same
String pidV2 = null;
String pidV1 = null;
if (pdList.get(0).get(2).equals("1") && pdList.get(1).get(2).equals("2")) {
pidV2 = pdList.get(1).get(0);
pidV1 = pdList.get(0).get(0);
} else if (pdList.get(1).get(2).equals("1") && pdList.get(0).get(2).equals("2")) {
pidV2 = pdList.get(0).get(0);
pidV1 = pdList.get(1).get(0);
} else
fail("there should one process definition with version == 1 and another one with version == 2. It is not the case");
assertNotNull(pdList.get(0).get(0));
assertNotNull(pdList.get(1).get(0));
assertEquals("My process", pdList.get(0).get(1));
assertEquals("My process", pdList.get(1).get(1));
// not suspended
assertEquals("false", pdList.get(0).get(3));
// not suspended
assertEquals("false", pdList.get(1).get(3));
assertEquals("This process to test JMX", pdList.get(0).get(4));
assertEquals("This process to test JMX", pdList.get(1).get(4));
//suspend the one with version == 2
mbsc.invoke(deploymentsBeanName, "suspendProcessDefinitionById", new String[] { pidV2 }, new String[] { String.class.getName() });
RepositoryService repositoryService = processEngine.getRepositoryService();
// test if it is realy suspended and not the other one
assertTrue(repositoryService.getProcessDefinition(pidV2).isSuspended());
assertFalse(repositoryService.getProcessDefinition(pidV1).isSuspended());
// test if it is reported as suspended and not the other one
List<String> pd = (List<String>) mbsc.invoke(deploymentsBeanName, "getProcessDefinitionById", new String[] { pidV2 }, new String[] { String.class.getName() });
assertNotNull(pd);
assertEquals(5, pd.size());
assertEquals("true", pd.get(3));
pd = (List<String>) mbsc.invoke(deploymentsBeanName, "getProcessDefinitionById", new String[] { pidV1 }, new String[] { String.class.getName() });
assertNotNull(pd);
assertEquals(5, pd.size());
assertEquals("false", pd.get(3));
// now reactivate the same suspended process
mbsc.invoke(deploymentsBeanName, "activatedProcessDefinitionById", new String[] { pidV2 }, new String[] { String.class.getName() });
// test if both processes are active again
assertFalse(repositoryService.getProcessDefinition(pidV2).isSuspended());
assertFalse(repositoryService.getProcessDefinition(pidV1).isSuspended());
// test if they are properly reported as activated
pd = (List<String>) mbsc.invoke(deploymentsBeanName, "getProcessDefinitionById", new String[] { pidV2 }, new String[] { String.class.getName() });
assertNotNull(pd);
assertEquals(5, pd.size());
assertEquals("false", pd.get(3));
pd = (List<String>) mbsc.invoke(deploymentsBeanName, "getProcessDefinitionById", new String[] { pidV1 }, new String[] { String.class.getName() });
assertNotNull(pd);
assertEquals(5, pd.size());
assertEquals("false", pd.get(3));
// now undeploy the one with version == 1
mbsc.invoke(deploymentsBeanName, "deleteDeployment", new String[] { firstDeploymentId }, new String[] { String.class.getName() });
// now there should be only one deployment and only one process definition with version 2, first check it with API
assertEquals(1, repositoryService.createDeploymentQuery().count());
assertTrue(!repositoryService.createDeploymentQuery().singleResult().getId().equals(firstDeploymentId));
// check if it is also affected in returned results.
deployments = (List<List<String>>) mbsc.getAttribute(deploymentsBeanName, "Deployments");
assertNotNull(deployments);
assertEquals(1, deployments.size());
assertEquals(3, deployments.get(0).size());
assertTrue(!deployments.get(0).get(0).equals(firstDeploymentId));
}
use of javax.management.remote.JMXConnector in project ACS by ACS-Community.
the class GCJMXClient method connect.
public void connect() throws Exception {
// get the connector address
String connectorAddress = _vm.getAgentProperties().getProperty(LOCAL_CONNECTOR_ADDRESS);
// no connector address, so we start the JMX agent
if (connectorAddress == null) {
String agent = _vm.getSystemProperties().getProperty("java.home") + File.separator + "lib" + File.separator + "management-agent.jar";
_vm.loadAgent(agent);
// agent is started, get the connector address
connectorAddress = _vm.getAgentProperties().getProperty(LOCAL_CONNECTOR_ADDRESS);
if (connectorAddress == null) {
System.err.println("Cannot connect :(");
System.exit(1);
}
}
JMXServiceURL remoteURL = new JMXServiceURL(connectorAddress);
JMXConnector connector = null;
try {
connector = JMXConnectorFactory.connect(remoteURL);
} catch (IOException e) {
System.err.println("Can't connect to the remote URL");
System.exit(-1);
}
try {
_remote = connector.getMBeanServerConnection();
} catch (IOException e) {
System.err.println("Can't get a connection to the remote MBeanServer");
System.exit(-1);
}
}
use of javax.management.remote.JMXConnector in project ats-framework by Axway.
the class SystemOperations method getJvmMbeans.
/**
* @param host the address of the host machine
* @param jmxPort the jmx port
*
* @return all MBeans with their attributes and type
* @throws SystemOperationException
*/
@PublicAtsApi
public String getJvmMbeans(String host, String jmxPort) {
JMXConnector jmxCon = null;
try {
// Connect to JMXConnector
JMXServiceURL serviceUrl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + host + ":" + jmxPort + "/jmxrmi");
jmxCon = JMXConnectorFactory.newJMXConnector(serviceUrl, null);
jmxCon.connect();
// Access the MBean
MBeanServerConnection con = jmxCon.getMBeanServerConnection();
Set<ObjectName> queryResults = con.queryNames(null, null);
StringBuilder results = new StringBuilder();
for (ObjectName theName : queryResults) {
results.append("\n---");
results.append("\nMBean name: " + theName.getCanonicalName());
MBeanAttributeInfo[] attributes = con.getMBeanInfo(theName).getAttributes();
for (MBeanAttributeInfo attribute : attributes) {
if (attribute.getType() != null) {
if (!"javax.management.openmbean.CompositeData".equals(attribute.getType())) {
if ("java.lang.Long".equals(attribute.getType()) || "java.lang.Integer".equals(attribute.getType()) || "int".equals(attribute.getType()) || "long".equals(attribute.getType()))
results.append("\r " + attribute.getName() + " | " + attribute.getType());
} else {
results.append("\r " + attribute.getName() + " | " + attribute.getType());
CompositeData comdata = (CompositeData) con.getAttribute(theName, attribute.getName());
if (comdata != null) {
for (String key : comdata.getCompositeType().keySet()) {
Object value = comdata.get(key);
if (value instanceof Integer || value instanceof Double || value instanceof Long)
results.append("\r " + key + " | " + value.getClass());
}
}
}
}
}
}
return results.toString();
} catch (Exception e) {
throw new SystemOperationException("MBeans with their attributes cannot be get.", e);
} finally {
if (jmxCon != null)
try {
jmxCon.close();
} catch (IOException e) {
log.error("JMX connection was not closed!");
}
}
}
use of javax.management.remote.JMXConnector in project ignite by apache.
the class GridServletLoaderTest method testLoader.
/**
* @throws Exception If failed.
*/
@SuppressWarnings({ "unchecked" })
public void testLoader() throws Exception {
JMXConnector jmx = null;
try {
while (true) {
try {
jmx = getJMXConnector("localhost", Integer.valueOf(GridTestProperties.getProperty("tomcat.jmx.rmi.connector.port")));
if (jmx != null)
break;
} catch (IOException e) {
log().warning("Failed to connect to server (will try again).", e);
}
Thread.sleep(WAIT_DELAY);
}
assert jmx != null;
String query = "*:*";
ObjectName queryName = new ObjectName(query);
boolean found = false;
ObjectName kernal = null;
int i = 0;
while (found == false) {
info("Attempt to find GridKernal MBean [num=" + i + ']');
Set<ObjectName> names = jmx.getMBeanServerConnection().queryNames(queryName, null);
if (names.isEmpty() == false) {
for (ObjectName objectName : names) {
info("Found MBean for node: " + objectName);
String kernalName = objectName.getKeyProperty("name");
if ("GridKernal".equals(kernalName)) {
kernal = objectName;
found = true;
}
}
}
if (kernal == null) {
System.out.println("Node GridKernal MBean was not found.");
Thread.sleep(WAIT_DELAY);
}
i++;
}
UUID nodeId = (UUID) jmx.getMBeanServerConnection().getAttribute(kernal, "LocalNodeId");
assert nodeId != null : "Failed to get Grid nodeId.";
info("Found grid node with id: " + nodeId);
} finally {
if (jmx != null) {
try {
jmx.close();
info("JMX connection closed.");
} catch (IOException e) {
System.out.println("Failed to close JMX connection (will ignore): " + e.getMessage());
}
}
}
}
Aggregations