1. Building an OSGi Bundle
通过调用Maven Archetype生成Maven Project,例如:
(1) Apache CXF code-first archetype
mvn archetype:generate -DarchetypeGroupId=org.apache.servicemix.tooling -DarchetypeArtifactId=servicemix-cxf-code-first-osgi-bundle -DarchetypeVersion=2012.01.0.fuse-7-060 -DgroupId=GroupId -DartifactId=ArtifactId -Dversion=Version
(2) Apache CXF WSDL-first archetype
mvn archetype:generate -DarchetypeGroupId=org.apache.servicemix.tooling -DarchetypeArtifactId=servicemix-cxf-wsdl-first-osgi-bundle -DarchetypeVersion=2012.01.0.fuse-7-060 -DgroupId=GroupId -DartifactId=ArtifactId -Dversion=Version
(3) Apache Camel archetype
mvn archetype:generate -DarchetypeGroupId=org.apache.servicemix.tooling -DarchetypeArtifactId=servicemix-camel-osgi-bundle -DarchetypeVersion=2012.01.0.fuse-7-061 -DgroupId=org.fusesource.example -DartifactId=camel-bundle
2. Modifying an Existing Maven Project
(1) Change the package type to bundle
<project .> ... <packaging>bundle</packaging> ... </project>
(2) Add the bundle plug-in to your POM
<project .> ... <build> <defaultGoal>install</defaultGoal> <plugins> ... <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> <Bundle-SymbolicName>${project.groupId}.${pro ject.artifactId}</Bundle-SymbolicName> <Import-Package>*</Import-Package> </instructions> </configuration> </plugin> </plugins> </build> ... </project>
(3) Customize the JDK compiler version
<project .> ... <build> <defaultGoal>install</defaultGoal> <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> ... </project>
3. Packaging a Web Service in a Bundle
(1) 修改pom.xml文件中的package类型为bundle
(2) 添加CXF 运行需要的依赖:org.apache.cxf.bundle.
<project .> ... <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> ... <Require- Bundle> org.apache.cxf.bundle </Require-Bundle> ... </instructions> </configuration> </plugin> </plugins> </build> ... </project>
(3) 添加需要的Import-Package:
<project .> ... <build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <extensions>true</extensions> <configuration> <instructions> ... <Import-Package> javax.jws, javax.wsdl, javax.xml.bind, javax.xml.bind.annotation, javax.xml.namespace, javax.xml.ws, META-INF.cxf, META-INF.cxf.osgi, org.apache.cxf.bus, org.apache.cxf.bus.spring, org.apache.cxf.bus.resource, org.apache.cxf.configuration.spring, org.apache.cxf.resource, org.apache.cxf.jaxws, org.springframework.beans.factory.config, * </Import-Package> ... </instructions> </configuration> </plugin> </plugins> </build> ... </project>
4. Bundle 解析依赖问题
常见的问题有下面几个:
(1) Required features or bundles are not installed
(2) Import-Package header is incomplete,主要注意下面几点:
A. 确保 Import-Package中包含*
B. Maven bundle plug-in无法计算出动态依赖
C. Maven bundle plug-in无法计算META-INF/spring/下面的XML配置文件中所需的依赖
D. Maven bundle plug-in可以计算出OSGI-INF/blueprint下面的XML配置文件中所需的依赖
解决上述两个依赖问题的步骤:
(1) 检查已经安装的Bundles和Festures:osgi:list,features:list
(2) 安装但不启动Bundle:
karaf@root> osgi:install MyBundleURL #生成Bundle Id:218
(3) 使用下面的命令确保动态导入:
karaf@root> dev:dynamic-import 218
(4) 解析Bundle:
karaf@root> osgi:resolve 218
(5) 获取该Bundle所需的完整Import-Package:
karaf@root> package:imports 218
(6) 将上一步生成的Import-Package结果Copy至POM文件中的Maven bundle plug-in中的Import-Package处,
重新打包工程并安装。
5. Deploying Features
Fuse ESB Enterprise提供了一个灵活的部署单元:featurs,用于部署多个Bundle.
(1) Creating a Feature:按照下面的步骤进行。
A. Create a custom feature repository
创建文件:C:\Projects\features.xml
<?xml version="1.0" encoding="UTF-8"?> <features name="CustomRepository"> </features>
B. Add a feature to the custom feature repository
<?xml version="1.0" encoding="UTF-8"?> <features name="MyFeaturesRepo"> <feature name="example-camel-bundle"> <bundle>file:C:/Projects/camel-bundle/target/camel-bundle-1.0-SNAPSHOT.jar </bundle> </feature> </features>
检查系统中已经有的feature:
karaf@root> features:refreshUrl
karaf@root> features:list
karaf@root> features:list | grep example-camel-bundle
C. Add the local repository URL to the features service
features:addUrl file:C:/Projects/features.xml
检查Features URL:
karaf@root> features:listUrl
D. Add dependent features to the feature
<?xml version="1.0" encoding="UTF-8"?> <features name="MyFeaturesRepo"> <feature name="example-camel-bundle"> <bundle>file:C:/Projects/camel-bundle/target/camel-bundle-1.0-SNAPSHOT.jar </bundle> <feature version="7.0.0.fuse-060">camel-core</feature> <feature version="7.0.0.fuse-060">camel-spring-osgi</feature> <feature version="7.0.0.fuse-060">servicemix-camel</feature> </feature> </features>
E. Add OSGi configurations to the feature
<?xml version="1.0" encoding="UTF-8"?> <features name="MyFeaturesRepo"> <feature name="example-camel-bundle"> <config name="org.fusesource.fuseesb.example"> prefix=MyTransform </config> </feature> </features>
下面Spring配置文件中的${prifix}将被替换成"MyTransform":
<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:osgi="http://camel.apache.org/schema/osgi" xmlns:osgix="http://www.springframework.org/schema/osgicompendium" .> ... <bean id="myTransform" class="org.fusesource.fuseesb.example.MyTransform"> <property name="prefix" value="${prefix}" /> </bean> <osgix:cm-properties id="preProps" persistentid="org.fusesource.fuseesb.example"> <prop key="prefix">DefaultValue</prop> </osgix:cm-properties> <ctx:property-placeholder properties-ref="preProps" /> </beans>
(2) Deploying a Feature
A. Installing at the console
karaf@root> features:refreshUrl
karaf@root> features:install example-camel-bundle
B. Uninstalling at the console
karaf@root> features:uninstall example-camel-bundle
C. Hot Deploy
Copy Featurs Repository 至Fuse ESB Enterprise 的安装目录下的deploy目录下。
D. Adding a feature to the boot configuration
编辑/etc/org.apache.felix.karaf.features.cfg配置文件中的featuresRepositories和featuresBoot值,添加自定义的
Feature Repository.
6. Example:Generating and Running an EIP Bundle
(1)
mvn archetype:generate -DarchetypeGroupId=org.apache.servicemix.tooling -DarchetypeArtifactId=servicemix-camel-osgi-bundle -DarchetypeVersion=2012.01.0.fuse-7-061 -DgroupId=org.fusesource.example -DartifactId=camel-bundle
(2) mvn clean install
(3) osgi:install -s file:/home/fdc/temp/mvn/camel-bundle/target/camel-bundle-1.0-SNAPSHOT.jar
(4) 输出:
>>>> MyTransform set body: Sat Jul 14 11:05:09 CST 2012
>>>> MyTransform set body: Sat Jul 14 11:05:11 CST 2012
>>>> MyTransform set body: Sat Jul 14 11:05:13 CST 2012
7. Generating and Running a Web Services Bundle
(1)
mvn archetype:generate -DarchetypeGroupId=org.apache.servicemix.tooling -DarchetypeArtifactId=servicemix-cxf-code-first-osgi-bundle -DarchetypeVersion=2012.01.0.fuse-7-061 -DgroupId=org.fusesource.example -DartifactId=cxf-code-first-bundle
(2) 编辑生成工程的POM文件,给<Import-Package>元素添加:*
(3) mvn clean install
(4) osgi:install -s file:/home/fdc/temp/mvn/cxf-code-first-bundle/target/cxf-code-first-bundle-1.0-SNAPSHOT.jar
(5) 查看WSDL文件:
http://localhost:8181/cxf/PersonServiceCF?wsdl