经过三个月的osgi学习,今天打算总结一下osgi与blueprint结合,并且使用camel、cxf等框架去做ESB.
camel是apache下面一个非常著名的框架,定义了一套路由规则,他的根本原理就是,一端进来——处理——另外一端出去,基于这个高度抽象模型,他扩展的范围非常广,可以去整合HTTP, ActiveMQJMSJBIMINA or CXF
首先先了解下camel与cxf发布webservice
webservice经常用于ESB,如果不用框架自己去实现的话会非常麻烦,所以我们要站在巨人的肩膀上去实现。请看下面blueprint.xml配置
<?xml version="1.0" encoding="UTF-8"?> <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:camel-cxf="http://camel.apache.org/schema/blueprint/cxf" xmlns:cxfcore="http://cxf.apache.org/blueprint/core" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.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 http://aries.apache.org/xmlns/jpa/v1.1.0 http://aries.apache.org/schemas/jpa/jpa_110.xsd http://aries.apache.org/xmlns/transactions/v1.0.0 http://aries.apache.org/schemas/transaction/transactionv10.xsd http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd" > <!--Web service--> <camel-cxf:cxfEndpoint id="myService" address="http://0.0.0.0:8018/myosgi/camelwebservice" serviceClass="com.cn.yyc.osgi.imywebservice" bindingId="http://www.w3.org/2003/05/soap/bindings/HTTP/"> <camel-cxf:properties> <entry key="dataFormat" value="POJO"/> </camel-cxf:properties> </camel-cxf:cxfEndpoint> <bean id="test1" class="com.cn.yyc.Test1"/> <camelContext id="myCamelContext" xmlns="http://camel.apache.org/schema/blueprint" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd"> <route > <from uri="myService" /> <log message="Request:${in.header.operationName}"/> <choice> <when> <simple>${in.header.operationName} == 'test1'</simple> <to uri="bean:test1"/> </when> <otherwise> <to uri="bean:test"/> </otherwise> </choice> </route> </camelContext> </blueprint>上面配置中serviceClass的配置是一个接口,接口方法中可以定义接受的webservice参数和返回的参数,下面路由节点to到一个资源的时候,可以到很多地方,如果到bean的话,那么类就需要继承org.apache.camel.Processor,实现process方法
import org.apache.camel.Exchange; import org.apache.camel.Processor; /** * * @author Administrator */ public class Test1 implements Processor{ @Override public void process(Exchange exchange) throws Exception { Response response=new Response(); exchange.getout().setBody(response); } }
因此pom.xml,文件需要引入依赖
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.15.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.0.4</version>
<type>jar</type>
</dependency>
maven打包成bundle的时候,需要配置如下
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <executions> <execution> <phase>deploy</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> <configuration> <skip>false</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-javadoc-plugin</artifactId> <version>2.10.3</version> <configuration> <aggregate>true</aggregate> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.7.2</version> <configuration> <skip>false</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.3.1</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> <manifestEntries> <Class-Path>.</Class-Path> <Built-By>yyc</Built-By> <Bundle-ManifestVersion>2</Bundle-ManifestVersion> <Bundle-Name>${project.groupId}.${project.ArtifactId}</Bundle-Name> <Bundle-SymbolicName>${project.groupId}.${project.ArtifactId}</Bundle-SymbolicName> <Bundle-Version>${project.version}</Bundle-Version> <Bundle-vendor>${project.groupId}</Bundle-vendor> <Import-Package> org.osgi.framework,org.slf4j,org.apache.camel,org.apache.cxf.message </Import-Package> </manifestEntries> </archive> </configuration> </plugin> </plugins> </build>可以将打包好的jar包放入servicemix运行,即可拿soapui软件测试
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。