diff --git a/webmagic-core/src/main/java/us/codecraft/webmagic/utils/UrlUtils.java b/webmagic-core/src/main/java/us/codecraft/webmagic/utils/UrlUtils.java index 9f66f73e..ed7ae8c1 100644 --- a/webmagic-core/src/main/java/us/codecraft/webmagic/utils/UrlUtils.java +++ b/webmagic-core/src/main/java/us/codecraft/webmagic/utils/UrlUtils.java @@ -83,6 +83,15 @@ public class UrlUtils { return domain; } + public static String removePort(String domain) { + int portIndex = domain.indexOf(":"); + if (portIndex != -1) { + return domain.substring(0, portIndex); + }else { + return domain; + } + } + /** * allow blank space in quote */ diff --git a/webmagic-extension/src/main/java/us/codecraft/webmagic/monitor/SpiderMonitor.java b/webmagic-extension/src/main/java/us/codecraft/webmagic/monitor/SpiderMonitor.java index 55472f12..cae0d1da 100644 --- a/webmagic-extension/src/main/java/us/codecraft/webmagic/monitor/SpiderMonitor.java +++ b/webmagic-extension/src/main/java/us/codecraft/webmagic/monitor/SpiderMonitor.java @@ -6,6 +6,7 @@ import us.codecraft.webmagic.Request; import us.codecraft.webmagic.Spider; import us.codecraft.webmagic.SpiderListener; import us.codecraft.webmagic.utils.Experimental; +import us.codecraft.webmagic.utils.UrlUtils; import javax.management.*; import java.lang.management.ManagementFactory; @@ -103,7 +104,8 @@ public class SpiderMonitor { } protected void registerMBean(SpiderStatusMXBean spiderStatus) throws MalformedObjectNameException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException { - ObjectName objName = new ObjectName(jmxServerName + ":name=" + spiderStatus.getName()); +// ObjectName objName = new ObjectName(jmxServerName + ":name=" + spiderStatus.getName()); + ObjectName objName = new ObjectName(jmxServerName + ":name=" + UrlUtils.removePort(spiderStatus.getName())); mbeanServer.registerMBean(spiderStatus, objName); } diff --git a/webmagic-extension/src/test/java/us/codecraft/webmagic/monitor/SeedUrlWithPortTest.java b/webmagic-extension/src/test/java/us/codecraft/webmagic/monitor/SeedUrlWithPortTest.java new file mode 100644 index 00000000..2ba5f247 --- /dev/null +++ b/webmagic-extension/src/test/java/us/codecraft/webmagic/monitor/SeedUrlWithPortTest.java @@ -0,0 +1,35 @@ +package us.codecraft.webmagic.monitor; + +import org.junit.Test; +import us.codecraft.webmagic.Page; +import us.codecraft.webmagic.Site; +import us.codecraft.webmagic.Spider; +import us.codecraft.webmagic.processor.PageProcessor; + +import javax.management.JMException; + +/** + * @author jerry_shenchao@163.com + */ +public class SeedUrlWithPortTest { + + @Test + public void testSeedUrlWithPort() throws JMException { + Spider spider = Spider.create(new TempProcessor()).addUrl("http://www.hndpf.org:8889/"); + SpiderMonitor.instance().register(spider); + spider.run(); + } +} + +class TempProcessor implements PageProcessor { + + @Override + public void process(Page page) { + + } + + @Override + public Site getSite() { + return Site.me(); + } +}