This page explains how you can create an OSGi bundle that contains both Java code and a Spring XML file with the endpoints definition. The example shows a file poller endpoint with a servicemix-bean POJO
Creating the Maven project
First, create and empty directory and add the following pom.xml file. We add the bundle plugin and configure it to import the servicemix-file and servicemix-bean packages as well as the org.apache.servicemix.common.osgi
package
Components version
When you're running this example, make sure to configure the correct component version in your pom.xml file
e.g. for ServiceMix 4.3.0, choose the 2011.01 version
<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 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.apache.servicemix</groupId> <artifactId>osgi-sample</artifactId> <packaging>bundle</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.servicemix</groupId> <artifactId>servicemix-bean</artifactId> <version>2011.01</version> </dependency> </dependencies> <build> <plugins> <!-- configure the bundle plugin with some additional imports --> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>2.3.4</version> <extensions>true</extensions> <configuration> <instructions> <Import-Package> org.apache.servicemix.file,org.apache.servicemix.bean,org.apache.servicemix.common.osgi,* </Import-Package> </instructions> </configuration> </plugin> <!-- let's use Java 5 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> </project>
Adding the Java source files in src/main/java
We create the Java source files under the src/main/java
folder as usual:
package org.apache.servicemix.pojo; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.servicemix.bean.Property; public class Pojo { private static final Log LOG = LogFactory.getLog(Pojo.class); public void handleFile(@Property(name="org.apache.servicemix.file.name") String name) { LOG.info("Handling file " + name); } }
Adding the Spring XML file to META-INF/spring
in src/main/resources
Any Spring XML file in the META-INF/spring
folder will be automatically loaded and started upon deployment. We add a Spring XML file with our endpoint definitions to this folder. Make sure it includes the org.apache.servicemix.common.osgi.EndpointExporter
that registers the endpoints with the NMR.
<?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:file="http://servicemix.apache.org/file/1.0" xmlns:bean="http://servicemix.apache.org/bean/1.0" xmlns:ns="urn:test:example" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://servicemix.apache.org/file/1.0 http://servicemix.apache.org/file/1.0/servicemix-file.xsd http://servicemix.apache.org/bean/1.0 http://servicemix.apache.org/bean/1.0/servicemix-bean.xsd" > <file:poller service="ns:poller" endpoint="endpoint" targetService="ns:bean" targetEndpoint="endpoint" file="/home/gert/test/poller"/> <bean:endpoint service="ns:bean" endpoint="endpoint" beanClassName="org.apache.servicemix.pojo.Pojo"/> <!-- use this special class to register the endpoints with the NMR --> <bean class="org.apache.servicemix.common.osgi.EndpointExporter" /> </beans>
Build and test
Now, run a mvn clean install
from your project directory to start the Maven build.
[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building test [INFO] task-segment: [clean, install] [INFO] ------------------------------------------------------------------------ [INFO] [clean:clean] [INFO] Deleting directory /home/gert/Projects/test/target [INFO] [resources:resources] [INFO] Using default encoding to copy filtered resources. Downloading: http://repo1.maven.org/maven2/org/apache/ws/security/wss4j/1.5.2/wss4j-1.5.2.pom [INFO] [compiler:compile] [INFO] Compiling 1 source file to /home/gert/Projects/test/target/classes ... [INFO] [bundle:bundle] [INFO] [install:install] [INFO] Installing /home/gert/Projects/test/target/test-1.0-SNAPSHOT.jar to /home/gert/.m2/repository/org/apache/servicemix/test/1.0-SNAPSHOT/test-1.0-SNAPSHOT.jar [INFO] [bundle:install] [INFO] Parsing file:/home/gert/.m2/repository/repository.xml [INFO] Installing org/apache/servicemix/test/1.0-SNAPSHOT/test-1.0-SNAPSHOT.jar [INFO] Writing OBR metadata [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 12 seconds [INFO] Finished at: Mon Jun 01 09:05:29 CEST 2009 [INFO] Final Memory: 32M/192M [INFO] ------------------------------------------------------------------------
After the build has finished succesfully, just copy the JAR file you find in the target directory into the ServiceMix 4 deploy
directory. Afterwards, verify that the bundle has been started by typing the osgi/list
command in the ServiceMix 4 console.
smx@root:/> osgi/list START LEVEL 100 ID State Spring Level Name ... [ 158] [Active ] [Started] [ 60] test (1.0.0.SNAPSHOT)