apache camel 用来复制网站页面
我需要将一些客户的网站从其它地方迁移到我的诗篇在线建站系统中去,由此开始了将近一个星期的与camel的接触,确实是一个宏大的程序。先看一张camel的router。apache camel 路由图
对应的java DSL:
from("jms:TOOL.CLONE.PAGE").beanRef("clonepage") .split().body() .choice() .when().mvel("request.body[0] == 'img'").beanRef("clonepage", "processImg") .when().mvel("request.body[0] == 'css'").beanRef("clonepage", "processCss").to("direct:cssimg") .when().mvel("request.body[0] == 'script'").beanRef("clonepage", "processScript") .otherwise().beanRef("clonepage", "processOthers"); from("direct:cssimg").split().body().beanRef("clonepage", "processImg");
@Override protected void configure() { super.configure(); // lets add in any RouteBuilder instances we want to use bind(MyRouteBuilder.class); bind(Printer.class); } /** * Lets configure the JMS component, parameterizing some properties from the * jndi.properties file */ @Provides @JndiBind("jms") JmsComponent jms(@Named("activemq.brokerURL") String brokerUrl) { return JmsComponent.jmsComponent(new ActiveMQConnectionFactory(brokerUrl)); } @Provides @JndiBind("myBean") SomeBean someBean(Injector injector) { return injector.getInstance(SomeBean.class); } private static final String PERSISTENCE_UNIT_NAME = "p-unit";public static EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); //@Provides// EntityManager emp(){// return MyModule.factory.createEntityManager();// } @Provides @JndiBind("hpb") HttpProcessBean hpb(Injector injector) { return injector.getInstance(HttpProcessBean.class); } @Provides @JndiBind("extrator") ArticleExtrator extrator(Injector injector){ return injector.getInstance(ArticleExtrator.class); } @Provides @JndiBind("clonepage") ClonePage clonepage(Injector injector){ return injector.getInstance(ClonePage.class); }
public void processCss(@Header("sitetr") String siteThemeRoot, @MVEL("request.body[1]") final String pageUrl,@MVEL("request.body[2]") String saveFileName,Exchange exchange){ProducerTemplate template = context.createProducerTemplate();//in-out pattern. Exchange ecc = template.send("http4://localhost",new Processor() {@Overridepublic void process(Exchange exchange) throws Exception {exchange.getIn().setHeader(Exchange.HTTP_URI,"http4://" + pageUrl);}}); Message out = ecc.getOut(); String c = out.getBody(String.class); List<String[]> relations = new ArrayList<String[]>(); //url(../Images/lian.gif) Pattern p = Pattern.compile("url\\s*?\\(['"\\s]*(.*?)['"\\s]*\\)"); Matcher m = p.matcher(c); String[] hu = getHostnameUri(pageUrl); while(m.find()){ String url = m.group(1); relations.add(getOneItem(url, "img", null, hu[0], hu[1])); } exchange.getOut().setHeader("sitetr", siteThemeRoot); exchange.getOut().setBody(relations); template.sendBodyAndHeader("file://" + siteThemeRoot,c,Exchange.FILE_NAME,saveFileName);}