update spring4 configuration
parent
7fd27d9b22
commit
12a6390cbd
@ -1,19 +0,0 @@
|
|||||||
package us.codecraft.webmagic.worker;
|
|
||||||
|
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|
||||||
import us.codecraft.express.WebServer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author code4crafter@gmail.com
|
|
||||||
*/
|
|
||||||
public class Bootstrap {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(
|
|
||||||
new String[]{"classpath*:/spring/applicationContext-*.xml"}
|
|
||||||
);
|
|
||||||
WebServer webServer = classPathXmlApplicationContext.getBean(WebServer.class);
|
|
||||||
webServer.port(11111).start();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,64 +0,0 @@
|
|||||||
package us.codecraft.webmagic.worker;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import us.codecraft.express.WebServer;
|
|
||||||
import us.codecraft.express.controller.AjaxController;
|
|
||||||
import us.codecraft.express.controller.ParamMap;
|
|
||||||
import us.codecraft.express.controller.ResultMap;
|
|
||||||
import us.codecraft.webmagic.Spider;
|
|
||||||
import us.codecraft.webmagic.scripts.Language;
|
|
||||||
import us.codecraft.webmagic.scripts.ScriptProcessor;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author code4crafter@gmail.com
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class SpiderManager implements InitializingBean {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private WebServer webServer;
|
|
||||||
|
|
||||||
private Map<String, Spider> spiderMap = new ConcurrentHashMap<String, Spider>();
|
|
||||||
|
|
||||||
public Spider newSpider(ParamMap params) {
|
|
||||||
Spider spider = Spider
|
|
||||||
.create(new ScriptProcessor(Language.JavaScript, params.get("script"), params.getInt("thread")))
|
|
||||||
.thread(params.getInt("thread")).addUrl(params.get("url"));
|
|
||||||
spider.start();
|
|
||||||
return spider;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet() throws Exception {
|
|
||||||
AjaxController newController = new AjaxController() {
|
|
||||||
@Override
|
|
||||||
public Object ajax(ParamMap params) {
|
|
||||||
try {
|
|
||||||
Spider spider = newSpider(params);
|
|
||||||
spiderMap.put(params.get("uuid"), spider);
|
|
||||||
return ResultMap.create().put("code", 200).put("msg", "success");
|
|
||||||
} catch (Exception e) {
|
|
||||||
// If you provide worker to user, DO NOT return
|
|
||||||
// e.getMessage()!
|
|
||||||
return ResultMap.create().put("code", 500).put("msg", e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
webServer.post("/new/${uuid}", newController);
|
|
||||||
webServer.get("/new/${uuid}", newController);
|
|
||||||
webServer.get("/status/${uuid}", new AjaxController() {
|
|
||||||
@Override
|
|
||||||
public Object ajax(ParamMap params) {
|
|
||||||
Spider spider = spiderMap.get(params.get("uuid"));
|
|
||||||
ResultMap put = ResultMap.create().put("pageCount", spider.getPageCount())
|
|
||||||
.put("status", spider.getStatus().name()).put("thread", spider.getThreadAlive());
|
|
||||||
return put;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xmlns:context="http://www.springframework.org/schema/context"
|
|
||||||
xmlns:util="http://www.springframework.org/schema/util"
|
|
||||||
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/mvc
|
|
||||||
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
|
||||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
|
|
||||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
|
|
||||||
|
|
||||||
<beans>
|
|
||||||
<bean name="server" class="us.codecraft.express.connector.jetty.JettyWebServer">
|
|
||||||
</bean>
|
|
||||||
</beans>
|
|
||||||
|
|
||||||
|
|
||||||
</beans>
|
|
Loading…
Reference in New Issue