use of javax.management.remote.JMXServiceURL in project jersey by jersey.
the class Main method main.
public static void main(String[] args) throws Exception {
// e.g. "service:jmx:rmi:///jndi/rmi://sysifos.cz.oracle.com:11112/jmxrmi"
final String jmxUrl = args[0];
// e.g. "org.glassfish.jersey.test.performance.interceptor.dynamic:type=DynamicallyBoundInterceptorResource,name=gets"
final String mBeanName = args[1];
// e.g. "OneMinuteRate"
final String mBeanAttrName = args[2];
// e.g. 50
final int sampleCount = Integer.parseInt(args[3]);
// e.g. "phishing.properties"
final String propertiesFile = args[4];
System.out.printf("JMX URL = %s\nMBean = %s\nattribute = %s\nsamples = %d\nfilename = %s\n" + "Going to connect...\n", jmxUrl, mBeanName, mBeanAttrName, sampleCount, propertiesFile);
final JMXServiceURL url = new JMXServiceURL(jmxUrl);
final JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
final MBeanServerConnection mBeanServer = jmxc.getMBeanServerConnection();
final ObjectName mBeanObjectName = new ObjectName(mBeanName);
System.out.println("Connected...");
double totalSum = 0;
int samplesTaken = 0;
for (int i = 0; i < sampleCount; i++) {
Thread.sleep(5000);
Double sample = (Double) mBeanServer.getAttribute(mBeanObjectName, mBeanAttrName);
System.out.printf("OMR[%d]=%f\n", i, sample);
totalSum += sample;
samplesTaken++;
}
jmxc.close();
final double result = totalSum / samplesTaken;
writeResult(result, propertiesFile);
System.out.printf("\nAverage=%f\n", result);
}
use of javax.management.remote.JMXServiceURL in project hadoop by apache.
the class JMXGet method init.
/**
* @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";
}
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);
}
}
use of javax.management.remote.JMXServiceURL 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.JMXServiceURL in project neo4j by neo4j.
the class HotspotManagementSupport method createServer.
private JMXConnectorServer createServer(int port, boolean useSSL, Log log) {
MBeanServer server = getMBeanServer();
final JMXServiceURL url;
try {
url = new JMXServiceURL("rmi", null, port);
} catch (MalformedURLException e) {
log.warn("Failed to start JMX Server", e);
return null;
}
Map<String, Object> env = new HashMap<>();
if (useSSL) {
env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, new SslRMIClientSocketFactory());
env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, new SslRMIServerSocketFactory());
}
try {
return JMXConnectorServerFactory.newJMXConnectorServer(url, env, server);
} catch (IOException e) {
log.warn("Failed to start JMX Server", e);
return null;
}
}
use of javax.management.remote.JMXServiceURL 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;
}
Aggregations