From ced79630d324e199abb9c3becf93aab3defa307e Mon Sep 17 00:00:00 2001 From: "yihua.huang" Date: Fri, 25 Apr 2014 11:10:35 +0800 Subject: [PATCH] specify jndi and jmx #98 --- .../webmagic/monitor/SpiderMonitor.java | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/webmagic-core/src/main/java/us/codecraft/webmagic/monitor/SpiderMonitor.java b/webmagic-core/src/main/java/us/codecraft/webmagic/monitor/SpiderMonitor.java index 2c11d71..d16aa5f 100644 --- a/webmagic-core/src/main/java/us/codecraft/webmagic/monitor/SpiderMonitor.java +++ b/webmagic-core/src/main/java/us/codecraft/webmagic/monitor/SpiderMonitor.java @@ -5,11 +5,14 @@ import us.codecraft.webmagic.Spider; import us.codecraft.webmagic.processor.example.GithubRepoPageProcessor; import us.codecraft.webmagic.processor.example.OschinaBlogPageProcessor; -import javax.management.*; +import javax.management.JMException; +import javax.management.MBeanServer; +import javax.management.ObjectName; import javax.management.remote.JMXConnectorServer; import javax.management.remote.JMXConnectorServerFactory; import javax.management.remote.JMXServiceURL; import java.io.IOException; +import java.lang.management.ManagementFactory; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; import java.util.ArrayList; @@ -23,6 +26,7 @@ import java.util.concurrent.atomic.AtomicInteger; */ public class SpiderMonitor { + public static final int RMI_PORT = 14721; private List spiderStatuses = new ArrayList(); public List getSpiders() { @@ -52,7 +56,7 @@ public class SpiderMonitor { return new SpiderStatus(spider, monitorSpiderListener); } - public static SpiderMonitor create(){ + public static SpiderMonitor create() { return new SpiderMonitor(); } @@ -89,31 +93,41 @@ public class SpiderMonitor { } - public void jmxStart() throws IOException, JMException { - jmxStart(14721); + public SpiderMonitor jndiStart(int port) throws IOException, JMException { + Registry registry = LocateRegistry.createRegistry(port); + return this; } - public void jmxStart(int rmiPort) throws IOException, JMException { - String jmxServerName = "WebMagic"; - // jdkfolder/bin/rmiregistry.exe 9999 - Registry registry = LocateRegistry.createRegistry(rmiPort); + public SpiderMonitor jndiStart() throws IOException, JMException { + return jndiStart(RMI_PORT); + } - MBeanServer mbs = MBeanServerFactory.createMBeanServer(jmxServerName); - //MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + public SpiderMonitor jmxStart() throws IOException, JMException { + return jmxStart("localhost", RMI_PORT); + } + + public SpiderMonitor jmxStart(String jndiServer, int rmiPort) throws IOException, JMException { + String jmxServerName = "WebMagic"; + + // start JNDI + MBeanServer localServer = ManagementFactory.getPlatformMBeanServer(); ObjectName objName; - JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + rmiPort + "/" + jmxServerName); + JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + jndiServer + ":" + rmiPort + "/" + jmxServerName); System.out.println("JMXServiceURL: " + url.toString()); - JMXConnectorServer jmxConnServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); + System.out.println("Please replace localhost of your ip if you want to connect it in remote server."); + JMXConnectorServer jmxConnServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, localServer); jmxConnServer.start(); + for (SpiderStatusMBean spiderStatus : spiderStatuses) { objName = new ObjectName(jmxServerName + ":name=" + spiderStatus.getName()); - mbs.registerMBean(spiderStatus, objName); + localServer.registerMBean(spiderStatus, objName); } - } + return this; + } public static void main(String[] args) throws JMException, NullPointerException, @@ -126,7 +140,7 @@ public class SpiderMonitor { SpiderMonitor spiderMonitor = new SpiderMonitor(); spiderMonitor.register(oschinaSpider, githubSpider); - spiderMonitor.jmxStart(); + spiderMonitor.jndiStart().jmxStart(); }