Apache ServiceMix应用之深入Apache Camel

转自:http://blog.csdn.net/lubiaopan/article/details/16371387

阅读本文章以前建议先阅读《Apache ServiceMix 初探》

预备知识:
BluePrint
OSGI
Maven
Java DSL
Apache Camel
Apache ActiveMQ

ESB最核心的功能便是应用集成和服务路由,Apache ServiceMix完成这两大核心功能的尖兵利器便是Apache Camel。Apache Camel是一个开源的、功能丰富的应用集成框架,它支持常见的EIP模式,是一个强大的基于规则的路由引擎,可以轻松的实现消息路由和消息转换,ServiceMix对Camel进行了深度集成来支持各种复杂的ESB功能。
关于Apache Camel的介绍:
    http://camel.apache.org/(主页)
    http://marshal.easymorse.com/archives/1431
    http://blog.csdn.net/njchenyi/article/details/5174261
关于EAI模式的介绍:
    http://www.eaipatterns.com/toc.html

发布路由的4种方式
在ServiceMix上发布Camel Routes有两大类4种方式,所谓的两大类是指:其一,直接通过简单的xml配置文件发布;其二,通过自定义bundle的方式发布。而每类下面既可以通过Spring容器发布又可以通过BluePrint容器发布,所以说有4种方式。下面对这4种方式进行介绍,然后比较他们的优缺点。
1、基于blueprint容器,通过简单的Blueprint XML文档发布路由,发布方式及实验见《Apache ServiceMix 初探》

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"  
  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.       xsi:schemaLocation="  
  5.       http://www.osgi.org/xmlns/blueprint/v1.0.0  
  6.       http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">  
  7.   <camelContext xmlns="http://camel.apache.org/schema/blueprint">  
  8.     <route>  
  9.       <from uri="file:camel/blueprin-plain-input"/>  
  10.       <log message="Copying ${file:name} to the output directory"/>  
  11.       <to uri="file:camel/blueprint-plain-output"/>  
  12.     </route>  
  13.   </camelContext>  
  14. </blueprint>  

2、基于spring容器,通过简单的Spring XML文档发布路由(与blueprint相比,配置上没有太大的区别,只不过是换了一种容器而已),发布方式及实验见《Apache ServiceMix 初探》

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:camel="http://camel.apache.org/schema/spring"  
  5.        xsi:schemaLocation="  
  6.           http://www.springframework.org/schema/beans  
  7.           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.           http://camel.apache.org/schema/spring  
  9.           http://camel.apache.org/schema/spring/camel-spring-2.10.3.xsd">  
  10.   <camelContext xmlns="http://camel.apache.org/schema/spring">  
  11.     <route>  
  12.       <from uri="file:camel/spring-plain-input"/>  
  13.       <log message="Copying ${file:name} to the output directory"/>  
  14.       <to uri="file:camel/spring-plain-output"/>  
  15.     </route>  
  16.   </camelContext>  
  17. </beans>  

3、基于Spring容器,自定义bundle发布路由(以apache-servicemix-4.5.3附带的例子进行演示,例子所在目录:\apache-servicemix-4.5.3\examples\camel\camel-osgi)

第一步:创建一个MyTransform类(这里对示例中的代码进行了一些调整)

[java]  view plain copy
 
  1. package org.apache.servicemix.examples.camel;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5.   
  6. import java.util.Date;  
  7.   
  8. public class MyTransform  {  
  9.   
  10.     private static final transient Logger LOG = LoggerFactory.getLogger(MyTransform.class);  
  11.   
  12.     private boolean verbose = true;//是否打印冗长字符串">>>>"  
  13.     private String prefix = "MyTransform";//日志的消息前缀  
  14.   
  15.     public Object transform(Object body) {  
  16.         String answer = prefix + " set body:  " + new Date();//拼接日志字符串  
  17.         if (verbose) {  
  18.             System.out.println(">>>> " + answer);  
  19.             LOG.info(">>>> " + answer);  
  20.         }else  
  21.         {  
  22.             System.out.println(answer);  
  23.             LOG.info(answer);  
  24.   
  25.   
  26.         }  
  27.           
  28.         return answer;  
  29.     }  
  30.   
  31.     public boolean isVerbose() {  
  32.         return verbose;  
  33.     }  
  34.   
  35.     public void setVerbose(boolean verbose) {  
  36.         this.verbose = verbose;  
  37.     }  
  38.   
  39.     public String getPrefix() {  
  40.         return prefix;  
  41.     }  
  42.   
  43.     public void setPrefix(String prefix) {  
  44.         this.prefix = prefix;  
  45.     }  
  46. }  


第二步:创建路由类(这里对示例中的代码进行了一些调整)

[java]  view plain copy
 
  1. package org.apache.servicemix.examples.camel;  
  2.   
  3. import org.apache.camel.builder.RouteBuilder;  
  4. import org.apache.camel.spring.Main;  
  5.   
  6. public class MyRouteBuilder extends RouteBuilder {  
  7.     public static void main(String[] args) throws Exception{  
  8.         new Main().run(args);  
  9.     }  
  10.   
  11.     public void configure() {  
  12.         MyTransform transform = new MyTransform();//创建MyTransform的一个实例  
  13.         transform.setPrefix("JavaDSL");//设置日志前缀为"JavaDSL"  
  14.         transform.setVerbose("false");//不打印冗长串">>>>"  
  15.           
  16.         from("timer://javaTimer?fixedRate=true&period=2000")//从定时器取数据  
  17.             .bean(transform, "transform")//执行MyTransform的transform方法              
  18.            .to("log:ExampleRouter");//将定时器数据路由到日志模块          
  19.     }      
  20. }  

第三步:在META-INF/spring文件夹下创建beans.xml

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:ctx="http://www.springframework.org/schema/context"  
  5.        xmlns:camel="http://camel.apache.org/schema/spring"  
  6.        xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"  
  7.        xsi:schemaLocation="  
  8.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  9.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
  10.        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd  
  11.        http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd">  
  12.   
  13.   <camel:camelContext xmlns="http://camel.apache.org/schema/spring">  
  14.     <!-- install the Java DSL route builder -->  
  15.     <package>org.apache.servicemix.examples.camel</package>  
  16.     <!-- install the route which is defined with xml tags -->  
  17.     <route>  
  18.       <from uri="timer://springTimer?fixedRate=true&period=2000"/>  
  19.       <bean ref="myTransform" method="transform"/>  
  20.       <to uri="log:ExampleRouter"/>  
  21.     </route>  
  22.   </camel:camelContext>  
  23.   
  24.   <bean id="myTransform" class="org.apache.servicemix.examples.camel.MyTransform">  
  25.     <property name="prefix" value="${prefix}"/>  
  26.   </bean>  
  27.      
  28.     <osgix:cm-properties id="preProps" persistent-id="org.apache.servicemix.examples">  
  29.         <prop key="prefix">MyTransform</prop><!--前缀名成为MyTransform-->  
  30.     </osgix:cm-properties>  
  31.   
  32.     <ctx:property-placeholder properties-ref="preProps" />  
  33.   
  34. </beans>  

看配置文件,<package>org.apache.servicemix.examples.camel</package>通过引用MyRouteBuilder类来定义路由,而紧接着下面的<route>通过配置文件来定义路由。前者直接在MyRouteBuilder中使用MyTransform,后者通过注入的方式使用MyTransform。如果想要完成配置文件无法提供的更强大的、自定义的功能则用前者,否则的话建议用后者。
第四步:创建pom文件(这里只把文件贴出来,不再赘述)

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 <a target="_blank" href="http://maven.apache.org/maven-v4_0_0.xsd'>">http://maven.apache.org/maven-v4_0_0.xsd">  
  3. </a>    <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <parent>  
  6.         <groupId>org.apache.servicemix.examples</groupId>  
  7.         <artifactId>camel-examples</artifactId>  
  8.         <version>4.5.3</version>  
  9.     </parent>  
  10.   
  11.     <artifactId>camel-osgi</artifactId>  
  12.     <packaging>bundle</packaging>  
  13.     <name>Apache ServiceMix :: Features :: Examples :: Camel OSGi</name>  
  14.     <description>Camel example using OSGi instead of JBI</description>  
  15.   
  16.     <dependencies>  
  17.         <dependency>  
  18.             <groupId>org.slf4j</groupId>  
  19.             <artifactId>slf4j-api</artifactId>  
  20.             <scope>provided</scope>  
  21.         </dependency>  
  22.         <dependency>  
  23.             <groupId>org.apache.camel</groupId>  
  24.             <artifactId>camel-spring</artifactId>  
  25.         </dependency>  
  26.     </dependencies>  
  27.   
  28.     <build>  
  29.         <plugins>  
  30.             <plugin>  
  31.                 <groupId>org.apache.felix</groupId>  
  32.                 <artifactId>maven-bundle-plugin</artifactId>  
  33.                 <configuration>  
  34.                     <instructions>  
  35.                         <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>  
  36.                         <Bundle-Description>${project.description}</Bundle-Description>  
  37.                         <Import-Package>*</Import-Package>  
  38.                         <Private-Package>org.apache.servicemix.examples.camel</Private-Package>  
  39.                     </instructions>  
  40.                 </configuration>  
  41.             </plugin>  
  42.         </plugins>  
  43.     </build>  
  44.   
  45. </project>  

第五步:编译、安装、运行
转到examples/camel/camel-osgi文件夹下,运行mvn install,耐心等待,编译成功之后会在examples/camel/camel-osgi文件夹下出现一个target文件夹,里面便是我们需要的bundle。
在ServicMix控制台上运行features:install  examples-camel-osgi命令安装编译好的bundle,安装完成后观察控制台,我的截图如下:

由图可见:通过XML配置的路由信息和通过MyRouteBuilder实现的路由信息都打印在了控制台上。前者带">>>>"符号,前缀为“MyTransfor”,后者不带">>>>"符号,前缀为“JavaDSL”。控制台上打印的内容是System.out.println()语句运行的结果,而路由的终结点是<to uri="log:ExampleRouter"/>,通过log:display命令查看打印的日志。
卸载该bundle的命令为:features:uninstall examples-camel-osgi
4、通过Blueprint发布osgi bundle的方式发布路由
此处不再详细描述,流程和3大致相同,ServiceMix自带的readme.txt描述的也很清楚了
4种方式的对比

Spring和BluePrint的对比

如果想得到基于OSGI框架的、更优的集成和服务注册方案,则优先考虑使用Blueprint,因为Blueprint有更强的针对性,OSGI联盟对blueprint进行了很多有针对性的、特殊的开发;当然,使用spring也完全没有问题,如果对spring已经有了丰富的实践经验,it is ok to use spring。

简单配置文件和自定义bundle对比

这两者没有孰优孰劣之分,大部分情况下直接通过xml一配就ok了;如果需要自定义一些功能,需要用到自己的辅助类的时候就该考虑用bundle了。两者可用的情况下就看个人习惯了。

猜你喜欢

转载自coffeehot.iteye.com/blog/2100449