Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Tutorial updated to use Camel 1.6, SMX 1.1.0 and PAX maven plugin

...

  • MANIFEST.MF file (file required and specifying the information about the bundle to deploy on the OSGI server, dependency with another bundle, version, ... )
  • jar of the bundle to deploy on the server
Tip
PAX Project
titlePAX Project

More info about the PAX maven plugin can be found here. PAX project does not include only maven but also tools to run, debug and deploy web application. We will address them in the second part of the tutorial and particularly the web integration

...

To allow your project to be imported in Eclipse, execute the following command in the directory demo.service-bundle.

Code Block
mvn org.ops4j:maven-pax-plugin:eclipse

...

Luckily, this is not the case because the specification has tried to avoid such pitfalls and because two interesting projects exist today to simplify our life:

...

Although this tutorial is based on Spring Dynamic Modules, iPOJO can be used as an alternative.

Remark : The OSGI specification is currently working on integration of the work done by Spring with their Dynamic Modules under the RFC 124 - blueprint services and Apache Geronimo community has already started to work on that.

So now, it is time to create the interface that we will use in this project. Open Eclipse environment if not already done and create a new folder "service" in{{src/main/java/demo}} tree. Add the interface TransformService.java and copy paste the code below:

...

Next, we will create the class TransformServiceImpl implmenting implementing the interface TransformService.Create the class TransformServiceImpl.java in the folder "impl" in{{src/main/java/demo/service/impl}}.

Code Blockcode
java
java
titleTransformServiceImpl.java
package demo.service.impl;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TransformServiceImpl implements TransformService {
    private static final transient Log LOG = LogFactory.getLog(TransformServiceImpl.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(">> call >> " + answer);
        }
        LOG.info(">> call >>" + 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;
    }
}

...

The next step concerns the creation of the configuration files who will allow the to inject dependency injection and later the deployment of the bundle into the OSGI server and its registration as a 'service'.

...

Code Block
xml
xml
titledemo-service-bundle-contxt.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <!-- regular spring configuration file defining the beans for this
       bundle. The configuration of OSGi definitions is kept in a separate
       configuration file so that this file can easily be used
       for integration testing outside of an OSGi environment -->
    <bean id="transformService" class="demo.service.impl.TransformServiceImpl">
    </bean>
</beans>

...

Code Block
xml
xml
titledemo-service-bundle-contxt-osgi.xml
<?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:osgi="http://www.springframework.org/schema/osgi"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/osgi
    http://www.springframework.org/schema/osgi/spring-osgi.xsd">

	<!--
		definitions using elements of the osgi namespace can be included in
		this file. There is no requirement to keep these definitions in a
		separate file if you do not want to. The rationale for keeping these
		definitions separate is to facilitate integration testing of the
		bundle outside of an OSGi container
	-->

	<osgi:service ref="transformService">
		<osgi:interfaces>
			<value>demo.service.TransformService</value>
		</osgi:interfaces>
	</osgi:service>

</beans>
<osgi:service ref="transformService">
		<osgi:interfaces>
			<value>demo.service.TransformService</value>
		</osgi:interfaces>
	</osgi:service>

</beans>

The The xml tag osgi:service will help to register our OSGI service top of the OSGI server and will publish the interfaces as available for another bundles who would like to use them.

Remark: for more information about Spring Dynamic Modules and configuration, I recommend to read its documentation and of course the incoming OSGI R4 specification integrating 'blueprint service'

Tip
titleSpring configuration files

Two XML files have been created to work with Spring beans and Spring OSGI beans separately (as proposed by spring - section D.1 Generated Project Features at-a-glance) but you can also merge the content in one file.

Step 5 : Generate the

...

jar of the bundle

Now, that the code and the configuration files are ready, we will use maven to generate the MANIFEST.MF file describing the information about our bundle, its version number, the package to export or import, etc.

Remark : We use maven to avoid to create manually the MANIFEST file.

This command can be launched from Eclipse (if you have integrated maven within Eclipse (eclipse maven plugin)) or a Unix/Dos prompt in the folder where your pom.xml file is located:

Code Block
mvn org.ops4j:maven-pax-plugin:compile org.ops4j:maven-pax-plugin:eclipse install

If this command does not report any error, then a 'MANIFEST.MF' file containing the following information is created in the folder 'META-INF' and
a demo.service-bundle-0.1.0.jar jar is created in the target folder:

Code Block
Manifest-Version: 1.0
Export-Package: demo.service;uses:version="org.apache.commons.logging"0.1"
Private-Package: demo.service.impl
Built-By: Charlesm
Build-Jdk: 1.6.0_07
Bundle-Version: 0.1.0
Tool: Bnd-0.0.238255
Bundle-Name: Demo Service Bundle
Bnd-LastModified: 1228122578185
Created-By: Apache Maven Bundle Plugin
Bundle-ManifestVersion: 2
Bundle-SymbolicName: demo.demo.service-bundle
Import-Package: demo.service,org;version="0.1",org.apache.commons.logging
Tip
titlebnd tool

The pom of spring dm uses the maven bundle plugin and the tool bnd
to generate this manifest fileThis file is created because the POM.file contains the maven felix plugin which uses the tool bnd. Bnd stands for BuNDle tool and has been created by Peter Kriens (OSGi Technical Officer)

...

Prior to run/launch the command generating the MANIFEST file, you must modify the pom.xml file and add the following line in order to allow the OSGI bundle to import/use the services classes : demo.service

Code Block
...
<Export-Package>!demo.routing</Export-Package>
<!-- text to be added after Export-Package tag -->
<Import-Package>demo.service,*</Import-Package>
...

Run the command mvn package (= Repeat step #5).

Step 8 : Deploy the bundles

We will show you now that we can easily deploy our bundles in two OSGI servers running a different OSGI kernel:

  • Felix for ServiceMix Kernel
  • Equinox for Spring Dynamic Module

ServiceMix Kernel

As mentioned in the documentation, Apache ServiceMix Kernel is a small OSGi based runtime which provides a lightweight container onto which various components and applications can be deployed. Moreover, the server provides administration, security, logging and provisioning features who will help you during the deployment and administration steps.

If this is not yet done, download ServiceMix Kernel 1.0.0 server and install it. Launch the server by executing the command in the bin folder:

Code Block

c:\apache-servicemix-kernel-1.0.0\bin>servicemix

If this is the first time Servicemix is started, then you will see that a new data folder is created.

In order to allow our bundles to work with Camel, execute the following commands to download and install the 'Camel and Spring bundles':

      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <version>1.4.3</version>
        <!--
         | the following instructions build a simple set of public/private classes into an OSGi bundle
        -->
        <configuration>
          <instructions>
            <Bundle-SymbolicName>${bundle.symbolicName}</Bundle-SymbolicName>
            <Bundle-Version>${pom.version}</Bundle-Version>
            <Import-Package>demo.service;version="${pom.version}"</Import-Package> -- LINE TO BE ADDED
            <Include-Resource>src/main/resources</Include-Resource>
          </instructions>
        </configuration>
      </plugin>

...

Run the command mvn org.ops4j:maven-pax-plugin:compile org.ops4j:maven-pax-plugin:eclipse install (= Repeat step #5).

Step 8 : Deploy the bundles

We will show you now that we can easily deploy our bundles in two OSGI servers running a different OSGI kernel:

  • Felix for ServiceMix Kernel
  • Equinox for Spring Dynamic Module

ServiceMix Kernel

As mentioned in the documentation, Apache ServiceMix Kernel is a small OSGi based runtime which provides a lightweight container onto which various components and applications can be deployed. Moreover, the server provides administration, security, logging and provisioning features who will help you during the deployment and administration steps.

If this is not yet done, download ServiceMix Kernel 1.1.0 server and install it. Launch the server by executing the command in the bin folder:

Code Block

c:\apache-servicemix-kernel-1.1.0\bin>servicemix

If this is the first time Servicemix is started, then you will see that a new data folder is created.

In order to allow our bundles to work with Camel, execute the following commands to download and install the 'Camel and Spring bundles':

Code Block

servicemix> osgi install \-s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.jaxb-impl/2.1.6_1
servicemix> osgi install \-s mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.jaxb-api-2.1/1.2.0
Code Block
servicemix> osgi install \-s mvn:org.springframework/spring-tx/2.5.56
servicemix> osgi install \-s mvn:org.apache.camel/camel-core/1.56.0
servicemix> osgi install \-s mvn:org.apache.camel/camel-spring/1.56.0
servicemix> osgi install \-s mvn:org.apache.camel/camel-osgi/1.56.0

Next, copy the two jar into the deploy folder, first the service and next the Camel bundle.

...

When the server is started, open your favorite browser and point to the following url http://localhost:8080/admin using 'admin' as login and 'springsource' as password. In the administration page, click on the button "browse" to upload the different jars to be deployed. Select the folder where you have copied the following jars and upload them one by one (by respecting the order)

When the bundles are uploaded, the screen is refreshed and shows a list of the deployed applications like this :

...