apache camel 用来复制网站页面

24 篇文章 0 订阅
17 篇文章 0 订阅

apache camel让你在很宏观的层面编写程序,几乎不像是在写程序。

我需要将一些客户的网站从其它地方迁移到我的诗篇在线建站系统中去,由此开始了将近一个星期的与camel的接触,确实是一个宏大的程序。先看一张camel的router。apache camel 路由图

对应的java DSL:

1 from("jms:TOOL.CLONE.PAGE").beanRef("clonepage")
2                                 .split().body()
3                     .choice()
4                     .when().mvel("request.body[0] == 'img'").beanRef("clonepage""processImg")
5                     .when().mvel("request.body[0] == 'css'").beanRef("clonepage""processCss").to("direct:cssimg")
6                     .when().mvel("request.body[0] == 'script'").beanRef("clonepage""processScript")
7                                     .otherwise().beanRef("clonepage""processOthers");
8 from("direct:cssimg").split().body().beanRef("clonepage""processImg");

流程说明:

从诗篇建站系统的后台发出一个复制页面的指令,将要复制的页面的参数包装后发送到activemq去。接下来就交给apache camel处理了,这是运行在另一个VM里面,和建站系统是解构的。camel从消息队列中获取消息,然后将要克隆的页面取下来,这是第一步,因为取下来的页面中包含许多img,css,javascript链接,这些链接在clonepage的默认处理方法中获取,然后交个下一个路由,当然css里面还包含有img图片,必须额外处理。

 

先看看克隆的效果吗?这是开源中国社区的首页克隆: http://demo.m3958.com/cos ,请注意没有任何手工修改,完全是程序自动克隆的。

 

这是guice配置:

01    @Override
02     protected void configure() {
03         super.configure();
04  
05         // lets add in any RouteBuilder instances we want to use
06         bind(MyRouteBuilder.class);
07         bind(Printer.class);
08     }
09  
10     /**
11      * Lets configure the JMS component, parameterizing some properties from the
12      * jndi.properties file
13      */
14     @Provides
15     @JndiBind("jms")
16     JmsComponent jms(@Named("activemq.brokerURL") String brokerUrl) {
17         return JmsComponent.jmsComponent(new ActiveMQConnectionFactory(brokerUrl));
18     }
19      
20     @Provides
21     @JndiBind("myBean")
22     SomeBean someBean(Injector injector) {
23         return injector.getInstance(SomeBean.class);
24     }
25      
26     private static final String PERSISTENCE_UNIT_NAME = "p-unit";
27     public static EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
28      
29 //  @Provides
30 //    EntityManager emp(){
31 //      return MyModule.factory.createEntityManager();
32 //    }
33      
34     @Provides
35     @JndiBind("hpb")
36     HttpProcessBean hpb(Injector injector) {
37         return injector.getInstance(HttpProcessBean.class);
38     }
39      
40     @Provides
41     @JndiBind("extrator")
42     ArticleExtrator extrator(Injector injector){
43         return injector.getInstance(ArticleExtrator.class);
44     }
45      
46      
47     @Provides
48     @JndiBind("clonepage")
49     ClonePage clonepage(Injector injector){
50         return injector.getInstance(ClonePage.class);
51     }

处理css的代码片段:

 

01 public void processCss(@Header("sitetr") String siteThemeRoot, @MVEL("request.body[1]"final String pageUrl,@MVEL("request.body[2]") String saveFileName,Exchange exchange){
02     ProducerTemplate template = context.createProducerTemplate();
03      
04     //in-out pattern.
05     Exchange ecc = template.send("http4://localhost",new Processor() {
06         @Override
07         public void process(Exchange exchange) throws Exception {
08             exchange.getIn().setHeader(Exchange.HTTP_URI,"http4://" + pageUrl);
09         }
10     });
11      
12     Message out = ecc.getOut();
13     String c = out.getBody(String.class);
14     List<String[]> relations = new ArrayList<String[]>();
15     //url(../Images/lian.gif)
16     Pattern p = Pattern.compile("url\\s*?\\(['\"\\s]*(.*?)['\"\\s]*\\)");
17     Matcher m = p.matcher(c);
18     String[] hu = getHostnameUri(pageUrl);
19     while(m.find()){
20         String url = m.group(1);
21         relations.add(getOneItem(url, "img"null, hu[0], hu[1]));
22     }
23     exchange.getOut().setHeader("sitetr", siteThemeRoot);
24     exchange.getOut().setBody(relations);
25     template.sendBodyAndHeader("file://" + siteThemeRoot,c,Exchange.FILE_NAME,saveFileName);
26 }

出处: http://my.oschina.net/u/148211/blog/17462
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值