I have recently been introduced to the world of Java and OSGI. It’s a nice world to live in and a frustrating one for beginners. I’ll try to share some of my experiences here with the hope that someone will find them useful.
OSGi in brief: “Any framework that implements the OSGi standard provides an environment for the modularization of applications into smaller bundles. Each bundle is a tightly coupled, dynamically loadable collection of classes, jars, and configuration files that explicitly declare their external dependencies (if any)”.
Currently I am utilizing OSGI principles through the use of camel and servicemix (camel is an extremely powerful framework for any system integrator while servicemix is an open source ESB and SOA toolkit).
Camel is an open source integration framework based on Enterprise Integration patterns
In simple terms Camel is a framework that uses EIP standards to implement a wide range of integrations. For example if you needed the following to communicate seamlessly and synchronously; two independent websites that communicate to a desktop app which in turn links to various third party applications for both reports and real-time transactions. Camel is built out of the box to support such integrations.
For more detailed info on camel please check the following links, you should also check out the camel in action book.
ServiceMix is the container where all the magic happens. Once you have built your bundles running camel, Apache ActiveMQ, Apache CXF etc, they are deployed to servicemix to perform their duties.
ServiceMix runs out of the box, once you download it navigate to the bin folder and fire the servicemix.bat file. For interactive use its best if you run servicemix.bat from a cmd. Older versions of servicemix will return errors when used with Java 7 so it is advisable to run your instance of servicemix using Java6
You will need to have Java (preferably java 6) installed on your computer
Apache Maven (it should be in your path), servicemix and eclipse
An internet connection. (some dependencies might need to be downloaded)
Now the interesting part
Since it is a hello world case, we are going to develop a simple camel bundle that picks files from a particular folder and drops them into another folder.
Camel can use both java and spring DSL though in my short experience you will use more spring DSL than java especially if you are interacting in ESB architecture.
The example I have used is very simple but it still illustrates Camels’ fundamentals i.e. a routing-engine builder. It allows you to define your own routing rules, decide from which sources to accept messages, and determine how to process and send those messages to other destinations (this example doesn’t have processing but we shall explore it at a later stage).
Our hello world app uses two routes.
From source folder
To destination folder
<to uri=”file://c:/dev/out” />
All files dropped in the in folder will be copied to the out folder immediately. Get the code from github
Step 1: compile the source. With mvn compile
Step 2: install the bundle with mvn install
Step 3: deploy bundle
There are two ways of deploying a bundle to servicemix.
1: Hot deployment: just drop your bundle in the deploy folder, service mix will install and start it (the bundle is the jar found in the target folder).
2: use Karaf on the cmd. Once you run servicemix.bat in a command shell, it opens a root instance of Karaf. Use this command to install your bundle.
Osgi:install -s mvn:url where url in this case = com.helloworld.filecopy.spring/spring-dsl/1.0.0
The url is in the format groupId/artifact-id/artifact-version. The –s option, commands Karaf to install and start the bundle at the same time.
Personally I think the osgi:install is a better option to the hot deploy method since it gives you the option to update your bundle in service mix time and time again after the first deployment just by performing step2 without having to copy any jar files.
Drop a file in the folder you created c:/dev/in. it should be copied to the c:/dev/out folder immediately.
Extra tip: Check out WebConsole
Let’s try this for now and if it works the sky will be the limit. We shall explore other aspects of Camel in other hello-worlds.
Like my post? Please support me by clicking on the Mersi button