split monitor to server and client mode #98
parent
ced79630d3
commit
30af23d003
|
@ -26,7 +26,20 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||||
*/
|
*/
|
||||||
public class SpiderMonitor {
|
public class SpiderMonitor {
|
||||||
|
|
||||||
public static final int RMI_PORT = 14721;
|
private enum Type {
|
||||||
|
Server, Client, Local;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final int DEFAULT_SERVER_PORT = 14721;
|
||||||
|
|
||||||
|
private static final String DEFAULT_SERVER_HOST = "localhost";
|
||||||
|
|
||||||
|
private int serverPort;
|
||||||
|
|
||||||
|
private String serverHost;
|
||||||
|
|
||||||
|
private Type type = Type.Local;
|
||||||
|
|
||||||
private List<SpiderStatusMBean> spiderStatuses = new ArrayList<SpiderStatusMBean>();
|
private List<SpiderStatusMBean> spiderStatuses = new ArrayList<SpiderStatusMBean>();
|
||||||
|
|
||||||
public List<SpiderStatusMBean> getSpiders() {
|
public List<SpiderStatusMBean> getSpiders() {
|
||||||
|
@ -37,6 +50,11 @@ public class SpiderMonitor {
|
||||||
return spiderStatuses.get(0);
|
return spiderStatuses.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register spider for monitor.
|
||||||
|
* @param spiders
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public SpiderMonitor register(Spider... spiders) {
|
public SpiderMonitor register(Spider... spiders) {
|
||||||
for (Spider spider : spiders) {
|
for (Spider spider : spiders) {
|
||||||
MonitorSpiderListener monitorSpiderListener = new MonitorSpiderListener();
|
MonitorSpiderListener monitorSpiderListener = new MonitorSpiderListener();
|
||||||
|
@ -93,17 +111,59 @@ public class SpiderMonitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public SpiderMonitor jndiStart(int port) throws IOException, JMException {
|
/**
|
||||||
|
* Start monitor as server mode.
|
||||||
|
* @param port
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
* @throws JMException
|
||||||
|
*/
|
||||||
|
public SpiderMonitor server(int port) throws IOException, JMException {
|
||||||
Registry registry = LocateRegistry.createRegistry(port);
|
Registry registry = LocateRegistry.createRegistry(port);
|
||||||
|
serverPort = port;
|
||||||
|
serverHost = "localhost";
|
||||||
|
type = Type.Server;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpiderMonitor jndiStart() throws IOException, JMException {
|
/**
|
||||||
return jndiStart(RMI_PORT);
|
* Start monitor as server mode.
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
* @throws JMException
|
||||||
|
*/
|
||||||
|
public SpiderMonitor server() throws IOException, JMException {
|
||||||
|
return server(DEFAULT_SERVER_PORT);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start monitor as client mode.
|
||||||
|
* @param serverHost
|
||||||
|
* @param serverPort
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
* @throws JMException
|
||||||
|
*/
|
||||||
|
public SpiderMonitor client(String serverHost, int serverPort) throws IOException, JMException {
|
||||||
|
type = Type.Client;
|
||||||
|
this.serverHost = serverHost;
|
||||||
|
this.serverPort = serverPort;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start monitor as client mode.
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
* @throws JMException
|
||||||
|
*/
|
||||||
|
public SpiderMonitor client() throws IOException, JMException {
|
||||||
|
return client(DEFAULT_SERVER_HOST, DEFAULT_SERVER_PORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpiderMonitor jmxStart() throws IOException, JMException {
|
public SpiderMonitor jmxStart() throws IOException, JMException {
|
||||||
return jmxStart("localhost", RMI_PORT);
|
return jmxStart("localhost", DEFAULT_SERVER_PORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpiderMonitor jmxStart(String jndiServer, int rmiPort) throws IOException, JMException {
|
public SpiderMonitor jmxStart(String jndiServer, int rmiPort) throws IOException, JMException {
|
||||||
|
@ -114,12 +174,13 @@ public class SpiderMonitor {
|
||||||
|
|
||||||
ObjectName objName;
|
ObjectName objName;
|
||||||
|
|
||||||
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + jndiServer + ":" + rmiPort + "/" + jmxServerName);
|
if (type != Type.Local) {
|
||||||
System.out.println("JMXServiceURL: " + url.toString());
|
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + jndiServer + ":" + rmiPort + "/" + jmxServerName);
|
||||||
System.out.println("Please replace localhost of your ip if you want to connect it in remote server.");
|
System.out.println("JMXServiceURL: " + url.toString());
|
||||||
JMXConnectorServer jmxConnServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, localServer);
|
System.out.println("Please replace localhost of your ip if you want to connect it in remote server.");
|
||||||
jmxConnServer.start();
|
JMXConnectorServer jmxConnServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, localServer);
|
||||||
|
jmxConnServer.start();
|
||||||
|
}
|
||||||
|
|
||||||
for (SpiderStatusMBean spiderStatus : spiderStatuses) {
|
for (SpiderStatusMBean spiderStatus : spiderStatuses) {
|
||||||
objName = new ObjectName(jmxServerName + ":name=" + spiderStatus.getName());
|
objName = new ObjectName(jmxServerName + ":name=" + spiderStatus.getName());
|
||||||
|
@ -129,9 +190,7 @@ public class SpiderMonitor {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws JMException,
|
public static void main(String[] args) throws Exception {
|
||||||
NullPointerException,
|
|
||||||
IOException {
|
|
||||||
|
|
||||||
Spider oschinaSpider = Spider.create(new OschinaBlogPageProcessor())
|
Spider oschinaSpider = Spider.create(new OschinaBlogPageProcessor())
|
||||||
.addUrl("http://my.oschina.net/flashsword/blog").thread(2);
|
.addUrl("http://my.oschina.net/flashsword/blog").thread(2);
|
||||||
|
@ -140,7 +199,9 @@ public class SpiderMonitor {
|
||||||
|
|
||||||
SpiderMonitor spiderMonitor = new SpiderMonitor();
|
SpiderMonitor spiderMonitor = new SpiderMonitor();
|
||||||
spiderMonitor.register(oschinaSpider, githubSpider);
|
spiderMonitor.register(oschinaSpider, githubSpider);
|
||||||
spiderMonitor.jndiStart().jmxStart();
|
spiderMonitor.jmxStart();
|
||||||
|
oschinaSpider.start();
|
||||||
|
githubSpider.start();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue