From 7fd27d9b22286d8eb3644fac1e2ddb54a878656f Mon Sep 17 00:00:00 2001 From: "yihua.huang" Date: Tue, 17 Dec 2013 23:57:23 +0800 Subject: [PATCH] add spring 4 mvc for worker --- pom.xml | 2 +- webmagic-worker/pom.xml | 67 +++++++++++++++++- .../webmagic/worker/SpiderManager.java | 68 +++++++++---------- .../webmagic/worker/web/SpiderController.java | 20 ++++++ .../src/main/resources/log/log4j.xml | 21 ++++++ .../spring/applicationContext-freemarker.xml | 34 ++++++++++ .../spring/applicationContext-myBatis.xml | 21 ++++++ .../applicationContext-webmagic-worker.xml | 21 +++--- .../resources/spring/applicationContext.xml | 43 ++++++++++++ .../src/main/webapp/WEB-INF/jsp/404.jsp | 12 ++++ .../src/main/webapp/WEB-INF/jsp/500.jsp | 18 +++++ .../src/main/webapp/WEB-INF/web.xml | 53 +++++++++++++++ 12 files changed, 333 insertions(+), 47 deletions(-) create mode 100644 webmagic-worker/src/main/java/us/codecraft/webmagic/worker/web/SpiderController.java create mode 100644 webmagic-worker/src/main/resources/log/log4j.xml create mode 100644 webmagic-worker/src/main/resources/spring/applicationContext-freemarker.xml create mode 100644 webmagic-worker/src/main/resources/spring/applicationContext-myBatis.xml create mode 100644 webmagic-worker/src/main/resources/spring/applicationContext.xml create mode 100644 webmagic-worker/src/main/webapp/WEB-INF/jsp/404.jsp create mode 100644 webmagic-worker/src/main/webapp/WEB-INF/jsp/500.jsp create mode 100644 webmagic-worker/src/main/webapp/WEB-INF/web.xml diff --git a/pom.xml b/pom.xml index ae9fb26d..0ee156a3 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ UTF-8 UTF-8 - 3.1.1.RELEASE + 4.0.0.RELEASE webmagic-parent diff --git a/webmagic-worker/pom.xml b/webmagic-worker/pom.xml index 4bb7c90b..58386f1d 100644 --- a/webmagic-worker/pom.xml +++ b/webmagic-worker/pom.xml @@ -12,6 +12,7 @@ us.codecraft webmagic-worker 0.4.3-SNAPSHOT + war @@ -19,6 +20,59 @@ webmagic-scripts 0.4.3-SNAPSHOT + + + org.codehaus.jackson + jackson-core-lgpl + 1.9.8 + + + org.mybatis + mybatis + 3.1.1 + + + + org.mybatis + mybatis-spring + 1.1.1 + + + + + org.codehaus.jackson + jackson-mapper-lgpl + 1.9.7 + + + + org.freemarker + freemarker + 2.3.19 + + + org.springframework + spring-test + ${spring-version} + test + + + + org.springframework + spring-aop + ${spring-version} + + + + org.aspectj + aspectjrt + 1.7.2 + + + org.aspectj + aspectjweaver + 1.7.2 + us.codecraft express.java @@ -31,14 +85,25 @@ org.springframework - spring-asm + spring-webmvc ${spring-version} + + + javax.servlet + javax.servlet-api + 3.1.0 + org.springframework spring-context ${spring-version} + + org.springframework + spring-context-support + ${spring-version} + diff --git a/webmagic-worker/src/main/java/us/codecraft/webmagic/worker/SpiderManager.java b/webmagic-worker/src/main/java/us/codecraft/webmagic/worker/SpiderManager.java index 99d78263..067c0252 100644 --- a/webmagic-worker/src/main/java/us/codecraft/webmagic/worker/SpiderManager.java +++ b/webmagic-worker/src/main/java/us/codecraft/webmagic/worker/SpiderManager.java @@ -20,45 +20,45 @@ import java.util.concurrent.ConcurrentHashMap; @Component public class SpiderManager implements InitializingBean { - @Autowired - private WebServer webServer; + @Autowired + private WebServer webServer; - private Map spiderMap = new ConcurrentHashMap(); + private Map spiderMap = new ConcurrentHashMap(); - 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; - } + 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")); + @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; } - }); - } + }); + } } diff --git a/webmagic-worker/src/main/java/us/codecraft/webmagic/worker/web/SpiderController.java b/webmagic-worker/src/main/java/us/codecraft/webmagic/worker/web/SpiderController.java new file mode 100644 index 00000000..905064ec --- /dev/null +++ b/webmagic-worker/src/main/java/us/codecraft/webmagic/worker/web/SpiderController.java @@ -0,0 +1,20 @@ +package us.codecraft.webmagic.worker.web; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; + +/** + * @author code4crafter@gmail.com + */ +@Controller("spider") +@RequestMapping("spider") +public class SpiderController { + + @RequestMapping("create") + @ResponseBody + public ModelAndView create(){ + return null; + } +} diff --git a/webmagic-worker/src/main/resources/log/log4j.xml b/webmagic-worker/src/main/resources/log/log4j.xml new file mode 100644 index 00000000..c2b5a2f5 --- /dev/null +++ b/webmagic-worker/src/main/resources/log/log4j.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/webmagic-worker/src/main/resources/spring/applicationContext-freemarker.xml b/webmagic-worker/src/main/resources/spring/applicationContext-freemarker.xml new file mode 100644 index 00000000..286f2946 --- /dev/null +++ b/webmagic-worker/src/main/resources/spring/applicationContext-freemarker.xml @@ -0,0 +1,34 @@ + + + + + + + + + 0 + zh_CN + yyyy-MM-dd HH:mm:ss + yyyy-MM-dd + #.## + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/webmagic-worker/src/main/resources/spring/applicationContext-myBatis.xml b/webmagic-worker/src/main/resources/spring/applicationContext-myBatis.xml new file mode 100644 index 00000000..222df020 --- /dev/null +++ b/webmagic-worker/src/main/resources/spring/applicationContext-myBatis.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/webmagic-worker/src/main/resources/spring/applicationContext-webmagic-worker.xml b/webmagic-worker/src/main/resources/spring/applicationContext-webmagic-worker.xml index 41f40ba1..adc334f9 100755 --- a/webmagic-worker/src/main/resources/spring/applicationContext-webmagic-worker.xml +++ b/webmagic-worker/src/main/resources/spring/applicationContext-webmagic-worker.xml @@ -1,19 +1,18 @@ - - - - - - - - + + + + diff --git a/webmagic-worker/src/main/resources/spring/applicationContext.xml b/webmagic-worker/src/main/resources/spring/applicationContext.xml new file mode 100644 index 00000000..85f9974e --- /dev/null +++ b/webmagic-worker/src/main/resources/spring/applicationContext.xml @@ -0,0 +1,43 @@ + + + + + + + + web_messages + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/webmagic-worker/src/main/webapp/WEB-INF/jsp/404.jsp b/webmagic-worker/src/main/webapp/WEB-INF/jsp/404.jsp new file mode 100644 index 00000000..12344c4b --- /dev/null +++ b/webmagic-worker/src/main/webapp/WEB-INF/jsp/404.jsp @@ -0,0 +1,12 @@ +<%@ page language="java" contentType="text/html; charset=utf8" + pageEncoding="utf8"%> + + + + + 404 + + +

Page not found!

+ + \ No newline at end of file diff --git a/webmagic-worker/src/main/webapp/WEB-INF/jsp/500.jsp b/webmagic-worker/src/main/webapp/WEB-INF/jsp/500.jsp new file mode 100644 index 00000000..150df3a7 --- /dev/null +++ b/webmagic-worker/src/main/webapp/WEB-INF/jsp/500.jsp @@ -0,0 +1,18 @@ +<%@ page language="java" contentType="text/html; charset=utf8" + pageEncoding="utf8" isErrorPage="true" import="java.io.*"%> + + + + + 500 + + +页面出错啦! +<% + + StringWriter stringWriter = new StringWriter(); + exception.printStackTrace(new PrintWriter(stringWriter)); + out.println(stringWriter.toString()); +%> + + \ No newline at end of file diff --git a/webmagic-worker/src/main/webapp/WEB-INF/web.xml b/webmagic-worker/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000..eb253f34 --- /dev/null +++ b/webmagic-worker/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,53 @@ + + + Archetype Created Web Application + + + contextConfigLocation + + classpath*:spring/applicationContext*.xml, + + + + + contextClass + org.springframework.web.context.support.XmlWebApplicationContext + + + + + log4jConfigLocation + classpath:log/log4j.xml + + + + log4jRefreshInterval + 60000 + + + + + spring + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + classpath:/spring/applicationContext*.xml + + 1 + + + spring + / + + + 404 + /WEB-INF/jsp/404.jsp + + + 500 + /WEB-INF/jsp/500.jsp + + +