You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 14 Next »

Introduction

The focus of this Tutorial is to introduce you how you can create, design a simple SOA solution using Camel and OSGI. Most of the current and commercial SOA solutions rely on standards XML/WSDL/BPMN/BPEL and Business Processes (designed through WYSIWYG editor like Eclispe or propriatary product) which are deployed and orchestrated into a Business Process Expression Language engine.

Such a solution can be envisaged for big companies because the skills/time to market are important or when different development standards exist
inside the company like (Java, .NET).

 In the years 90, such developments have been made using CORBA or COM+/DCOM Microsoft technology. But, both of the CORBA and COM approaches had (and still have) significant problems and limitations. Complexity is a big issue. Any of the data that is passed requires very specific formatting, and many of the rules for programming are simply too difficult to follow without encountering errors. Even at the height of their popularity, CORBA was used primarily in large system development staffed by legions of programmers, while COM was used, often reluctantly, by teams of Microsoft developers.

Open Standards Gateway Initiative provides a Java-based platform for integrating both Java and non-Java application components. This technology provides the standardized functions for constructing applications from small, individual components and deploying them as a single application. The core component of OSGi is the OSGi Framework. The OSGi Framework is a very specific class-loading model. In addition to its normal Java execution environment, this framework incorporates an OSGi modules layer that adds private classes for modules and allows for controlled linking between modules. The OSGi Framework also includes life-cycle management for starting, stopping, adding and removing modules, as well as a service registry for sharing objects between modules.

Coupling the OSGI framework with a lightweight Enterprise Service Bus will allow you to design easily the routing between your different modules. A module can be a Plain Old Java Object (POJO), a Web Service designed with Apache CxF framework, a component like an ordering system. In fact, the module or bundle which is the term used by OSGI represent the level of granularity that you identify for your application.

 In this first part of the OSGI tutorial, we will show you how to :

- Create a simple service (derived from the camel-osgi example) module (interface + implementation) and package it as a bundle,

- Create a Camel context with a small routing and package it as a separate bundle,

- The Camel routing will use an OSGI reference to call the simple service

The second part of this tutorial will be derived from the "Reporting Incident Tutorial" and will show you a more real application (which is web based) can be re-scoped into a OSGI application.

Prerequisites

This tutorial uses :

Note: The sample project can be downloaded, see the resources section.

Step 1 : Initial Project Setup

First, we create two eclipse projects using the maven archetype 'spring-osgi-bundle-archetype'. This archetype is helpful because it generates a pom.xml file that we will use with maven goal(s) to create the :

- 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

To create the simple service project, execute the following command in your Unix/Dos console.

mvn archetype:create -DarchetypeGroupId=org.springframework.osgi -DarchetypeArtifactId=spring-osgi-bundle-archetype -DarchetypeVersion=1.2.0-m2 -DgroupId=demo -DartifactId=demo.service-bundle -Dversion=0.1

Remarks :

- The archetype version depends on the version of Spring Dynamic Module used and must be adapted in consequence,

- According to your project, you can change the artifactId to point to com.mycompany.application.service, and groupId com.mycompany.application

 To create the Camel project, execute the following command

mvn archetype:create -DarchetypeGroupId=org.springframework.osgi -DarchetypeArtifactId=spring-osgi-bundle-archetype -DarchetypeVersion=1.2.0-m2 -DgroupId=demo -DartifactId=demo.camel-bundle -Dversion=0.1

Two folders are created with the following name :

- demo.service-bundle

- demo.camel-bundle

Next, you import these projects into your favorite workspace of Eclipse.

Step 2 : Develop the interface

Developing an OSGI project could be 'potentially' time consuming regarding to :
- The learning curve of the new technology to acquire,
- Packaging of the components, registering of the components,
- How to call the OSGI server ? How Can I have access to the bundle context ?

You could figure out that developing OSGI is similar to developp EJB components using EJB 1.0 specification. Remember that time when the only way to design an enterprise solution was to use EJB with its proxy, stub-skeleton classes, the protocol RMI/IIOP (blocked by most of the firewall) for the communication between the client and the server, ... and so on and so on

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 :

  • iPOJO (Apache Felix)
  • Spring Dynamic Modules (Spring)

The goals of these frameworks are to :

  • Design the business logic as POJO,
  • Inject dependency(ies) though IoC,
  • Handle lifecycle of the bundle and its relation with BundleContext

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

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

package demo.service;

public interface TransformService {

	public Object transform(Object obj);

}

Step 3 : Create the class implementing the interface

Next, we will create the class 'TransformServiceImpl' implmenting the interface 'TransformService'.

Create the class 'TransformServiceImpl.java' under the folder src/main/java/demo/service

package demo.service;

import java.util.Date;

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

/**
 * @version $Revision: 640450 $
 */
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;
    }
}

Step 4 : Create the spring configuration files

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

a) Dependency Injection

Create the file 'demo-service-bundle-contxt.xml' under the folder 'src/main/resources/META-INF/spring'

<?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.TransformServiceImpl">
	</bean>
</beans>

b) OSGI 'Injection'

Create the file 'demo-service-bundle-contxt-osgi.xml' under the folder 'src/main/resources/META-INF/spring'

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

Remark : for more information about Spring Dynamic Modules and configuration, I recommend to read their documentation

Step 5 : Generate the MANIFEST.MF file and 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 n°, the package to export or import.

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

mvn package

If this command does not report any error, then

  • A 'MANIFEST.MF' file containing the following information is created in the folder 'META-INF' :
Manifest-Version: 1.0
Export-Package: demo.service;uses:="org.apache.commons.logging"
Built-By: Charlesm
Build-Jdk: 1.6.0_07
Bundle-Version: 0.1.0
Tool: Bnd-0.0.238
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.apache.commons.logging

Remark : the pom of spring dm uses the maven bundle plugin and the tool bnd to generate this manifest file.

  • a 'demo.service-bundle-0.1.0.jar' jar is created in the folder 'target'

Step 6 : Create the Camel context file and OSGI dependency

The next step is quite simple for Camel users because we will create two configurations files, one containing the routing and the other a reference to our TransformationService deployed in a OSGI bundle.

  • For the routing, create the following file under the folder 'src/main/resources/META-INF/spring" of the project 'demo.camel-bundle'
<?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-2.5.xsd
       http://activemq.apache.org/camel/schema/spring
       http://activemq.apache.org/camel/schema/spring/camel-spring.xsd">

  <camelContext xmlns="http://activemq.apache.org/camel/schema/spring">
    <route>
      <from uri="timer://myTimer?fixedRate=true&amp;period=10000"/>
      <bean ref="myTransform" method="transform"/>
      <to uri="log:ExampleRouter"/>
    </route>
  </camelContext>
</beans>

The routing defined here is a timer who will every 10s call the POJO 'MyTransform' and send the result to the 'camel:log' component. As, you can see, this is a pure Camel config file without any reference to a OSGI bundle

  • To inject the dependency, we will create a second file named 'bundle-context-osgi.xml' in the same folder
<?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">

      <osgi:reference id="myTransform" interface="demo.service.TransformService"/>

</beans>

Remarks :
- The id of the bean referenced 'myTransform' used by the camel context has a reference to the OSGI interface 'demo.service.TransformService'
- How could we imagine something more simplest ? We don't have to call a JNDI server with a reference or something like that. Only a reference to the service interface.

Step 6 : Generate the manifest and jar file

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

Unfortunately, the MANIFEST.MF file of the Camel bundle does not contain a link to the package 'demo.Service' that the bean 'myTransform' required in order the instantiate the class. This link is not added by the bnd tool during the generation step. So we have to edit the file contained in the jar file 'demo.camel-bundle-0.1.jar' in order to add the following line at the end of the MANIFEST.MF file.

Remark : if someone find how to avoid this, he/she is welcome (wink)

Import-Package: demo.service

Save the MANIFEST.MF file (and the jar containing it)

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

SerciceMix Kernel

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 folder 'bin' :

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

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

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

servicemix> osgi install \-s mvn:org.springframework/spring-tx/2.5.5
servicemix> osgi install \-s mvn:org.apache.camel/camel-core/1.5.0
servicemix> osgi install \-s mvn:org.apache.camel/camel-spring/1.5.0
servicemix> osgi install \-s mvn:org.apache.camel/camel-osgi/1.5.0

Next, copy the two jar into the folder 'deploy', first the service and next the camel bundle. After a few seconds, you should see on the servicemix log console the following text :

>> call >> MyTransform set body:  Mon Dec 01 11:02:28 CET 2008

Remarks :
- In case of trouble, use the command : osgi list to see if all the deployed bundles are installed correctly and have their status equals to 'active
- To see the log of ServiceMix, use the command : log d

Spring DM server

TODO

#Resources

  •  
  • No labels