您的位置:首页 > 运维架构 > Apache

Apache ServiceMix应用之深入Apache Camel

2015-07-02 14:47 741 查看
预备知识:

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
plaincopy

<?xml version="1.0" encoding="utf-8"?>  

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"  

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

      xsi:schemaLocation="  

      http://www.osgi.org/xmlns/blueprint/v1.0.0  

      http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">  

  <camelContext xmlns="http://camel.apache.org/schema/blueprint">  

    <route>  

      <from uri="file:camel/blueprin-plain-input"/>  

      <log message="Copying ${file:name} to the output directory"/>  

      <to uri="file:camel/blueprint-plain-output"/>  

    </route>  

  </camelContext>  

</blueprint>  

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

[html] view
plaincopy

<?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:camel="http://camel.apache.org/schema/spring"  

       xsi:schemaLocation="  

          http://www.springframework.org/schema/beans  

          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  

          http://camel.apache.org/schema/spring  

          http://camel.apache.org/schema/spring/camel-spring-2.10.3.xsd">  

  <camelContext xmlns="http://camel.apache.org/schema/spring">  

    <route>  

      <from uri="file:camel/spring-plain-input"/>  

      <log message="Copying ${file:name} to the output directory"/>  

      <to uri="file:camel/spring-plain-output"/>  

    </route>  

  </camelContext>  

</beans>  

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

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

[java] view
plaincopy

package org.apache.servicemix.examples.camel;  

  

import org.slf4j.Logger;  

import org.slf4j.LoggerFactory;  

  

import java.util.Date;  

  

public class MyTransform  {  

  

    private static final transient Logger LOG = LoggerFactory.getLogger(MyTransform.class);  

  

    private boolean verbose = true;//是否打印冗长字符串">>>>"  

    private String prefix = "MyTransform";//日志的消息前缀  

  

    public Object transform(Object body) {  

        String answer = prefix + " set body:  " + new Date();//拼接日志字符串  

        if (verbose) {  

            System.out.println(">>>> " + answer);  

            LOG.info(">>>> " + answer);  

        }else  

        {  

            System.out.println(answer);  

            LOG.info(answer);  

  

  

        }  

          

        return answer;  

    }  

  

    public boolean isVerbose() {  

        return verbose;  

    }  

  

    public void setVerbose(boolean verbose) {  

        this.verbose = verbose;  

    }  

  

    public String getPrefix() {  

        return prefix;  

    }  

  

    public void setPrefix(String prefix) {  

        this.prefix = prefix;  

    }  

}  

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

[java] view
plaincopy

package org.apache.servicemix.examples.camel;  

  

import org.apache.camel.builder.RouteBuilder;  

import org.apache.camel.spring.Main;  

  

public class MyRouteBuilder extends RouteBuilder {  

    public static void main(String[] args) throws Exception{  

        new Main().run(args);  

    }  

  

    public void configure() {  

        MyTransform transform = new MyTransform();//创建MyTransform的一个实例  

        transform.setPrefix("JavaDSL");//设置日志前缀为"JavaDSL"  

        transform.setVerbose("false");//不打印冗长串">>>>"  

          

        from("timer://javaTimer?fixedRate=true&period=2000")//从定时器取数据  

            .bean(transform, "transform")//执行MyTransform的transform方法              

           .to("log:ExampleRouter");//将定时器数据路由到日志模块          

    }      

}  

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

[html] view
plaincopy

<?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:ctx="http://www.springframework.org/schema/context"  

       xmlns:camel="http://camel.apache.org/schema/spring"  

       xmlns:osgix="http://www.springframework.org/schema/osgi-compendium"  

       xsi:schemaLocation="  

       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  

       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd  

       http://www.springframework.org/schema/osgi-compendium http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd">  

  

  <camel:camelContext xmlns="http://camel.apache.org/schema/spring">  

    <!-- install the Java DSL route builder -->  

    <package>org.apache.servicemix.examples.camel</package>  

    <!-- install the route which is defined with xml tags -->  

    <route>  

      <from uri="timer://springTimer?fixedRate=true&period=2000"/>  

      <bean ref="myTransform" method="transform"/>  

      <to uri="log:ExampleRouter"/>  

    </route>  

  </camel:camelContext>  

  

  <bean id="myTransform" class="org.apache.servicemix.examples.camel.MyTransform">  

    <property name="prefix" value="${prefix}"/>  

  </bean>  

     

    <osgix:cm-properties id="preProps" persistent-id="org.apache.servicemix.examples">  

        <prop key="prefix">MyTransform</prop><!--前缀名成为MyTransform-->  

    </osgix:cm-properties>  

  

    <ctx:property-placeholder properties-ref="preProps" />  

  

</beans>  

看配置文件,<package>org.apache.servicemix.examples.camel</package>通过引用MyRouteBuilder类来定义路由,而紧接着下面的<route>通过配置文件来定义路由。前者直接在MyRouteBuilder中使用MyTransform,后者通过注入的方式使用MyTransform。如果想要完成配置文件无法提供的更强大的、自定义的功能则用前者,否则的话建议用后者。

第四步:创建pom文件(这里只把文件贴出来,不再赘述)

[html] view
plaincopy

<?xml version="1.0" encoding="UTF-8"?>  

<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">  

</a>    <modelVersion>4.0.0</modelVersion>  

  

    <parent>  

        <groupId>org.apache.servicemix.examples</groupId>  

        <artifactId>camel-examples</artifactId>  

        <version>4.5.3</version>  

    </parent>  

  

    <artifactId>camel-osgi</artifactId>  

    <packaging>bundle</packaging>  

    <name>Apache ServiceMix :: Features :: Examples :: Camel OSGi</name>  

    <description>Camel example using OSGi instead of JBI</description>  

  

    <dependencies>  

        <dependency>  

            <groupId>org.slf4j</groupId>  

            <artifactId>slf4j-api</artifactId>  

            <scope>provided</scope>  

        </dependency>  

        <dependency>  

            <groupId>org.apache.camel</groupId>  

            <artifactId>camel-spring</artifactId>  

        </dependency>  

    </dependencies>  

  

    <build>  

        <plugins>  

            <plugin>  

                <groupId>org.apache.felix</groupId>  

                <artifactId>maven-bundle-plugin</artifactId>  

                <configuration>  

                    <instructions>  

                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>  

                        <Bundle-Description>${project.description}</Bundle-Description>  

                        <Import-Package>*</Import-Package>  

                        <Private-Package>org.apache.servicemix.examples.camel</Private-Package>  

                    </instructions>  

                </configuration>  

            </plugin>  

        </plugins>  

    </build>  

  

</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了。两者可用的情况下就看个人习惯了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: