import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
public class AAA {
public static void main(String[] args) throws Exception {
String hostName = "aaa.google.com";
String port = "13221";
String jmxConnURL = "service:jmx:rmi:///jndi/rmi://" + hostName + ":" + port + "/jmxrmi";
JMXServiceURL address = new JMXServiceURL(jmxConnURL);
JMXConnector connector = DataJMXConnector.connectWithTimeout(address, 1000, TimeUnit.SECONDS);
if (connector == null) {
throw new IOException("connect failed to url : " + jmxConnURL);
}
connector.connect(null);
MBeanServerConnection mbsc = connector.getMBeanServerConnection();
System.out.println(mbsc.getMBeanCount());
}
}
class DataJMXConnector {
private static final ThreadFactory daemonThreadFactory = new DaemonThreadFactory();
public static JMXConnector connectWithTimeout(final JMXServiceURL url, long timeout, TimeUnit unit) throws IOException {
final BlockingQueue<Object> mailbox = new ArrayBlockingQueue<Object>(1);
ExecutorService executor = Executors.newSingleThreadExecutor(daemonThreadFactory);
System.out.println("0");
executor.submit(new Runnable() {
public void run() {
try {
System.out.println("xx");
JMXConnector connector = JMXConnectorFactory.connect(url);
System.out.println("1");
if (!mailbox.offer(connector)) {
System.out.println("2");
connector.close();
}
} catch (Throwable t) {
System.out.println("3");
mailbox.offer(t);
t.printStackTrace();
}
}
});
Object result;
try {
result = mailbox.poll(timeout, unit);
if (result == null) {
if (!mailbox.offer("")) {
result = mailbox.take();
}
}
} catch (InterruptedException e) {
throw initCause(new InterruptedIOException(e.getMessage()), e);
} finally {
executor.shutdown();
}
if (result == null) {
throw new SocketTimeoutException("Connect timed out: " + url);
} else if (result instanceof JMXConnector) {
return (JMXConnector) result;
} else {
return null;
}
}
private static <T extends Throwable> T initCause(T wrapper, Throwable wrapped) {
wrapper.initCause(wrapped);
return wrapper;
}
private static class DaemonThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
Thread t = Executors.defaultThreadFactory().newThread(r);
t.setDaemon(true);
return t;
}
}
}
'java core' 카테고리의 다른 글
/tmp/hsperfdata_계정(hsperfdata_www)/asdfjasdfd 파일 (0) | 2010.02.22 |
---|---|
Java Dns Cache관련 (0) | 2010.01.11 |
Studing JVM this week (0) | 2009.12.11 |
JDK bug (conncurrent lock??) (0) | 2009.12.11 |
Java Garbage Collection, Monitoring and Tuning (0) | 2009.12.09 |