下面列出了怎么用javax.management.remote.rmi.RMIConnectorServer的API类实例代码及写法,或者点击链接到github查看源代码。
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
System.out.println("Creating connectorServer");
connectorServer = new RMIConnectorServer(url, null, impl, mbs);
System.out.println("Starting connectorServer");
connectorServer.start();
System.out.println("Making client");
RMIConnection cc = impl.newClient(null);
System.out.println("Closing client");
cc.close();
if (connectorServer.isActive()) {
System.out.println("Stopping connectorServer");
connectorServer.stop();
}
if (failure == null)
System.out.println("TEST PASSED, no deadlock");
else
System.out.println("TEST FAILED");
}
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
System.out.println("Creating connectorServer");
connectorServer = new RMIConnectorServer(url, null, impl, mbs);
System.out.println("Starting connectorServer");
connectorServer.start();
System.out.println("Making client");
RMIConnection cc = impl.newClient(null);
System.out.println("Closing client");
cc.close();
if (connectorServer.isActive()) {
System.out.println("Stopping connectorServer");
connectorServer.stop();
}
if (failure == null)
System.out.println("TEST PASSED, no deadlock");
else
System.out.println("TEST FAILED");
}
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
System.out.println("Creating connectorServer");
connectorServer = new RMIConnectorServer(url, null, impl, mbs);
System.out.println("Starting connectorServer");
connectorServer.start();
System.out.println("Making client");
RMIConnection cc = impl.newClient(null);
System.out.println("Closing client");
cc.close();
if (connectorServer.isActive()) {
System.out.println("Stopping connectorServer");
connectorServer.stop();
}
if (failure == null)
System.out.println("TEST PASSED, no deadlock");
else
System.out.println("TEST FAILED");
}
/** optionally starts a normal JMXRMI connector in addition */
public JMXConnectorServer startNormalJmxRmiConnectorIfRequested(Properties properties) {
try {
String rmiPortS = properties.getProperty(RMI_REGISTRY_PORT_PROPERTY);
if (rmiPortS==null || rmiPortS.length()==0)
return null;
int rmiPort = Integer.parseInt(rmiPortS);
LocateRegistry.createRegistry(rmiPort);
MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
String svc =
"service:jmx:rmi:///jndi/rmi://localhost:"+rmiPort+"/jmxrmi";
JMXServiceURL url = new JMXServiceURL(svc);
RMIConnectorServer rmiServer = new RMIConnectorServer(url, null, mbeanServer);
rmiServer.start();
return rmiServer;
} catch (Exception e) {
System.err.println("Unable to start JmxmpAgent: "+e);
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
System.out.println("Creating connectorServer");
connectorServer = new RMIConnectorServer(url, null, impl, mbs);
System.out.println("Starting connectorServer");
connectorServer.start();
System.out.println("Making client");
RMIConnection cc = impl.newClient(null);
System.out.println("Closing client");
cc.close();
if (connectorServer.isActive()) {
System.out.println("Stopping connectorServer");
connectorServer.stop();
}
if (failure == null)
System.out.println("TEST PASSED, no deadlock");
else
System.out.println("TEST FAILED");
}
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
System.out.println("Creating connectorServer");
connectorServer = new RMIConnectorServer(url, null, impl, mbs);
System.out.println("Starting connectorServer");
connectorServer.start();
System.out.println("Making client");
RMIConnection cc = impl.newClient(null);
System.out.println("Closing client");
cc.close();
if (connectorServer.isActive()) {
System.out.println("Stopping connectorServer");
connectorServer.stop();
}
if (failure == null)
System.out.println("TEST PASSED, no deadlock");
else
System.out.println("TEST FAILED");
}
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
System.out.println("Creating connectorServer");
connectorServer = new RMIConnectorServer(url, null, impl, mbs);
System.out.println("Starting connectorServer");
connectorServer.start();
System.out.println("Making client");
RMIConnection cc = impl.newClient(null);
System.out.println("Closing client");
cc.close();
if (connectorServer.isActive()) {
System.out.println("Stopping connectorServer");
connectorServer.stop();
}
if (failure == null)
System.out.println("TEST PASSED, no deadlock");
else
System.out.println("TEST FAILED");
}
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
System.out.println("Creating connectorServer");
connectorServer = new RMIConnectorServer(url, null, impl, mbs);
System.out.println("Starting connectorServer");
connectorServer.start();
System.out.println("Making client");
RMIConnection cc = impl.newClient(null);
System.out.println("Closing client");
cc.close();
if (connectorServer.isActive()) {
System.out.println("Stopping connectorServer");
connectorServer.stop();
}
if (failure == null)
System.out.println("TEST PASSED, no deadlock");
else
System.out.println("TEST FAILED");
}
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
System.out.println("Creating connectorServer");
connectorServer = new RMIConnectorServer(url, null, impl, mbs);
System.out.println("Starting connectorServer");
connectorServer.start();
System.out.println("Making client");
RMIConnection cc = impl.newClient(null);
System.out.println("Closing client");
cc.close();
if (connectorServer.isActive()) {
System.out.println("Stopping connectorServer");
connectorServer.stop();
}
if (failure == null)
System.out.println("TEST PASSED, no deadlock");
else
System.out.println("TEST FAILED");
}
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
System.out.println("Creating connectorServer");
connectorServer = new RMIConnectorServer(url, null, impl, mbs);
System.out.println("Starting connectorServer");
connectorServer.start();
System.out.println("Making client");
RMIConnection cc = impl.newClient(null);
System.out.println("Closing client");
cc.close();
if (connectorServer.isActive()) {
System.out.println("Stopping connectorServer");
connectorServer.stop();
}
if (failure == null)
System.out.println("TEST PASSED, no deadlock");
else
System.out.println("TEST FAILED");
}
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
System.out.println("Creating connectorServer");
connectorServer = new RMIConnectorServer(url, null, impl, mbs);
System.out.println("Starting connectorServer");
connectorServer.start();
System.out.println("Making client");
RMIConnection cc = impl.newClient(null);
System.out.println("Closing client");
cc.close();
if (connectorServer.isActive()) {
System.out.println("Stopping connectorServer");
connectorServer.stop();
}
if (failure == null)
System.out.println("TEST PASSED, no deadlock");
else
System.out.println("TEST FAILED");
}
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
System.out.println("Creating connectorServer");
connectorServer = new RMIConnectorServer(url, null, impl, mbs);
System.out.println("Starting connectorServer");
connectorServer.start();
System.out.println("Making client");
RMIConnection cc = impl.newClient(null);
System.out.println("Closing client");
cc.close();
if (connectorServer.isActive()) {
System.out.println("Stopping connectorServer");
connectorServer.stop();
}
if (failure == null)
System.out.println("TEST PASSED, no deadlock");
else
System.out.println("TEST FAILED");
}
public static void main(String[] args) throws Exception {
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
RMIJRMPServerImplSub impl = new RMIJRMPServerImplSub();
System.out.println("Creating connectorServer");
connectorServer = new RMIConnectorServer(url, null, impl, mbs);
System.out.println("Starting connectorServer");
connectorServer.start();
System.out.println("Making client");
RMIConnection cc = impl.newClient(null);
System.out.println("Closing client");
cc.close();
if (connectorServer.isActive()) {
System.out.println("Stopping connectorServer");
connectorServer.stop();
}
if (failure == null)
System.out.println("TEST PASSED, no deadlock");
else
System.out.println("TEST FAILED");
}
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
Map<String,?> environment,
MBeanServer mbeanServer)
throws IOException {
if (!serviceURL.getProtocol().equals("rmi")) {
throw new MalformedURLException("Protocol not rmi: " +
serviceURL.getProtocol());
}
return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
Map<String,?> environment,
MBeanServer mbeanServer)
throws IOException {
if (!serviceURL.getProtocol().equals("iiop")) {
throw new MalformedURLException("Protocol not iiop: " +
serviceURL.getProtocol());
}
return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
Map<String,?> environment,
MBeanServer mbeanServer)
throws IOException {
if (!serviceURL.getProtocol().equals("iiop")) {
throw new MalformedURLException("Protocol not iiop: " +
serviceURL.getProtocol());
}
return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
private static boolean test(String proto, MBeanServer mbs)
throws Exception {
System.out.println("Testing for proto " + proto);
JMXConnectorServer cs;
JMXServiceURL url = new JMXServiceURL(proto, null, 0);
try {
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null,
mbs);
} catch (MalformedURLException e) {
System.out.println("System does not recognize URL: " + url +
"; ignoring");
return true;
}
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXServiceURL rmiurl = new JMXServiceURL("rmi", null, 0);
JMXConnector client = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = client.getMBeanServerConnection();
ObjectName on = new ObjectName("x:proto=" + proto + ",ok=yes");
mbsc.createMBean(RMIConnectorServer.class.getName(),
on,
mletName,
new Object[] {rmiurl, null},
new String[] {JMXServiceURL.class.getName(),
Map.class.getName()});
System.out.println("Successfully deserialized with " + proto);
mbsc.unregisterMBean(on);
client.close();
cs.stop();
return true;
}
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL url,
Map<String,?> map,
MBeanServer mbeanServer)
throws IOException {
final String protocol = url.getProtocol();
called = true;
System.out.println("JMXConnectorServerProviderImpl called");
if(protocol.equals("rmi"))
return new RMIConnectorServer(url, map, mbeanServer);
if(protocol.equals("throw-provider-exception"))
throw new JMXProviderException("I have been asked to throw");
throw new IllegalArgumentException("UNKNOWN PROTOCOL");
}
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
Map<String,?> environment,
MBeanServer mbeanServer)
throws IOException {
if (!serviceURL.getProtocol().equals("rmi")) {
throw new MalformedURLException("Protocol not rmi: " +
serviceURL.getProtocol());
}
return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
Map<String,?> environment,
MBeanServer mbeanServer)
throws IOException {
if (!serviceURL.getProtocol().equals("iiop")) {
throw new MalformedURLException("Protocol not iiop: " +
serviceURL.getProtocol());
}
return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
private static boolean test(String proto, MBeanServer mbs)
throws Exception {
System.out.println("Testing for proto " + proto);
JMXConnectorServer cs;
JMXServiceURL url = new JMXServiceURL(proto, null, 0);
try {
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null,
mbs);
} catch (MalformedURLException e) {
System.out.println("System does not recognize URL: " + url +
"; ignoring");
return true;
}
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXServiceURL rmiurl = new JMXServiceURL("rmi", null, 0);
JMXConnector client = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = client.getMBeanServerConnection();
ObjectName on = new ObjectName("x:proto=" + proto + ",ok=yes");
mbsc.createMBean(RMIConnectorServer.class.getName(),
on,
mletName,
new Object[] {rmiurl, null},
new String[] {JMXServiceURL.class.getName(),
Map.class.getName()});
System.out.println("Successfully deserialized with " + proto);
mbsc.unregisterMBean(on);
client.close();
cs.stop();
return true;
}
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL url,
Map<String,?> map,
MBeanServer mbeanServer)
throws IOException {
final String protocol = url.getProtocol();
called = true;
System.out.println("JMXConnectorServerProviderImpl called");
if(protocol.equals("rmi"))
return new RMIConnectorServer(url, map, mbeanServer);
if(protocol.equals("throw-provider-exception"))
throw new JMXProviderException("I have been asked to throw");
throw new IllegalArgumentException("UNKNOWN PROTOCOL");
}
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
Map<String,?> environment,
MBeanServer mbeanServer)
throws IOException {
if (!serviceURL.getProtocol().equals("rmi")) {
throw new MalformedURLException("Protocol not rmi: " +
serviceURL.getProtocol());
}
return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
Map<String,?> environment,
MBeanServer mbeanServer)
throws IOException {
if (!serviceURL.getProtocol().equals("iiop")) {
throw new MalformedURLException("Protocol not iiop: " +
serviceURL.getProtocol());
}
return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
private static boolean test(String proto, MBeanServer mbs)
throws Exception {
System.out.println("Testing for proto " + proto);
JMXConnectorServer cs;
JMXServiceURL url = new JMXServiceURL(proto, null, 0);
try {
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null,
mbs);
} catch (MalformedURLException e) {
System.out.println("System does not recognize URL: " + url +
"; ignoring");
return true;
}
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXServiceURL rmiurl = new JMXServiceURL("rmi", null, 0);
JMXConnector client = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = client.getMBeanServerConnection();
ObjectName on = new ObjectName("x:proto=" + proto + ",ok=yes");
mbsc.createMBean(RMIConnectorServer.class.getName(),
on,
mletName,
new Object[] {rmiurl, null},
new String[] {JMXServiceURL.class.getName(),
Map.class.getName()});
System.out.println("Successfully deserialized with " + proto);
mbsc.unregisterMBean(on);
client.close();
cs.stop();
return true;
}
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
Map<String,?> environment,
MBeanServer mbeanServer)
throws IOException {
if (!serviceURL.getProtocol().equals("rmi")) {
throw new MalformedURLException("Protocol not rmi: " +
serviceURL.getProtocol());
}
return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
Map<String,?> environment,
MBeanServer mbeanServer)
throws IOException {
if (!serviceURL.getProtocol().equals("iiop")) {
throw new MalformedURLException("Protocol not iiop: " +
serviceURL.getProtocol());
}
return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
public JmxScraper(String jmxUrl, Optional<String> username, Optional<String> password, boolean ssl, List<String> blacklist, SortedMap<Integer, List<String>> scrapFrequencies, Map<String, String> additionalLabels) {
this.jmxUrl = jmxUrl;
this.blacklist = blacklist.stream().map(Pattern::compile).collect(toList());
this.scrapFrequencies = new TreeMap<>();
this.lastScrapes = new HashMap<>(scrapFrequencies.size());
String[] additionalLabelKeys = additionalLabels.keySet().stream().toArray(String[]::new);
this.additionalLabelValues = additionalLabels.values().stream().toArray(String[]::new);
this.stats = Gauge.build()
.name("cassandra_stats")
.help("node stats")
.labelNames(concat(new String[]{"cluster", "datacenter", "keyspace", "table", "name"}, additionalLabelKeys))
.register();
scrapFrequencies.forEach((k, v) -> {
this.scrapFrequencies.put(k * 1000, v.stream().map(Pattern::compile).collect(toList()));
this.lastScrapes.put(k * 1000, 0L);
});
jmxEnv = new HashMap<>();
username.ifPresent(user -> {
String[] credential = new String[]{user, password.orElse("")};
jmxEnv.put(javax.management.remote.JMXConnector.CREDENTIALS, credential);
});
if (ssl) {
jmxEnv.put(Context.SECURITY_PROTOCOL, "ssl");
SslRMIClientSocketFactory clientSocketFactory = new SslRMIClientSocketFactory();
jmxEnv.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, clientSocketFactory);
jmxEnv.put("com.sun.jndi.rmi.factory.socket", clientSocketFactory);
}
}
public JMXConnectorServer newJMXConnectorServer(JMXServiceURL serviceURL,
Map<String,?> environment,
MBeanServer mbeanServer)
throws IOException {
if (!serviceURL.getProtocol().equals("iiop")) {
throw new MalformedURLException("Protocol not iiop: " +
serviceURL.getProtocol());
}
return new RMIConnectorServer(serviceURL, environment, mbeanServer);
}
private static boolean test(String proto, MBeanServer mbs)
throws Exception {
System.out.println("Testing for proto " + proto);
JMXConnectorServer cs;
JMXServiceURL url = new JMXServiceURL(proto, null, 0);
try {
cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null,
mbs);
} catch (MalformedURLException e) {
System.out.println("System does not recognize URL: " + url +
"; ignoring");
return true;
}
cs.start();
JMXServiceURL addr = cs.getAddress();
JMXServiceURL rmiurl = new JMXServiceURL("rmi", null, 0);
JMXConnector client = JMXConnectorFactory.connect(addr);
MBeanServerConnection mbsc = client.getMBeanServerConnection();
ObjectName on = new ObjectName("x:proto=" + proto + ",ok=yes");
mbsc.createMBean(RMIConnectorServer.class.getName(),
on,
mletName,
new Object[] {rmiurl, null},
new String[] {JMXServiceURL.class.getName(),
Map.class.getName()});
System.out.println("Successfully deserialized with " + proto);
mbsc.unregisterMBean(on);
client.close();
cs.stop();
return true;
}