Monday, February 3, 2014

Create Apache Camel OSGi Bundle Maven Project From Scratch

There are plenty maven projects which create Apache Camel OSGi bundles. But there is very few place I can find to simply explain how to create such a kind project from scratch. This blog will exmain the details about this process. Frankly speaking, it may seems very simple, but it took me several hours to get it work. Hopefully, people who are interested in this process do not have to go through the same processes as I did.

Create Maven Project with Apach Camel Routes

Here is the script I created for creating maven project with apache camel route [I use cygwin in my windows environment. If you run dos cmd, you can replace the \ with ^ for line continuation.]:


mvn archetype:generate \
  -DarchetypeGroupId=org.apache.camel.archetypes \
  -DarchetypeArtifactId=camel-archetype-spring  \
  -DarchetypeVersion=2.10.4 \
  -DgroupId=com.vha.esb.apexus \
  -DartifactId=ce-reject-notify \
  -Dversion=1.0.0-SNAPSHOT

You can put this into a file, namely, generateProject.sh for linux OS, or generateProject.bat. If you run under linux os or cygwind, make sure change the file permission:
    chmod 755 generatePoject.sh

Now you can run the script, it will generate all files with a defaul apache camel route, which simple copy a file from src/data to target/message/uk or target/message/others. Aparatly, apache camel community did an excellent job to make this process go smoothly. After the build process is completed, run the following commands:

    mvn clean install
    mvn camel:run
If you now check the target/messages/uk and target/messages/others, you will find an xml file existing in each directory.

Import Project to Eclipse

The projects created by the running the script is ready to be imported to eclipse IDE.

Create OSGi Bundle

To create OSGi bundle, we need to instruct maven to build bundle using the org.apache.felix maven plugin. Here is my pom.xml file




 4.0.0

 com.vha.esb.apexus
 ce-reject-notify
 bundle
 1.0.0-SNAPSHOT

 A Camel Spring Route
 http://www.myorganization.org

 
  UTF-8
  UTF-8
 

 
  
   org.apache.camel
   camel-core
   2.10.4
  
  
   org.apache.camel
   camel-spring
   2.10.4
  

  
  
   org.slf4j
   slf4j-api
   1.6.6
  
  
   org.slf4j
   slf4j-log4j12
   1.6.6
  
  
   log4j
   log4j
   1.2.17
  

  
  
   org.apache.camel
   camel-test-spring
   2.10.4
   test
  

 

 
  install

  
   
    org.apache.felix
    maven-bundle-plugin
    true
    
     target/META-INF
     
      ${project.groupId}.${project.artifactId}
      
      
       org.apache.cxf,
       org.apache.cxf.binding,
       org.apache.cxf.binding.corba,
       org.apache.cxf.binding.soap,
       org.apache.cxf.binding.soap.spring,
       org.apache.cxf.bus,
       org.apache.cxf.bus.resource,
       org.apache.cxf.bus.spring,
       org.apache.cxf.buslifecycle,
       org.apache.cxf.catalog,
       org.apache.cxf.configuration,
       org.apache.cxf.configuration.spring,
       org.apache.cxf.endpoint,
       org.apache.cxf.headers,
       org.apache.cxf.management,
       org.apache.cxf.management.jmx,
       org.apache.cxf.phase,
       org.apache.cxf.resource,
       org.apache.cxf.service.factory,
       org.apache.cxf.transport,
       org.apache.cxf.transport.http,
       org.apache.cxf.transport.http.policy,
       org.apache.cxf.transport.http_jetty,
       org.apache.cxf.transport.jms,
       org.apache.cxf.transports.http,
       org.apache.cxf.workqueue,
       org.apache.cxf.wsdl,
       org.apache.cxf.wsdl11,
       org.apache.ws.security.action,
       org.apache.ws.security.processor,
       org.springframework.beans.factory.config,
       org.springframework.mail.javamail,
       *
      
     
    
   






   
    org.apache.maven.plugins
    maven-compiler-plugin
    2.5.1
    
     1.6
     1.6
    
   
   
    org.apache.maven.plugins
    maven-resources-plugin
    2.4.3
    
     UTF-8
    
   

   
   
    org.apache.camel
    camel-maven-plugin
    2.10.4
   
  
 




Note: 2 places are changed: one packaging tag. By default, it is jar. Now it is bundle:
    bundle
The other place is the plugin:
   
    org.apache.felix
    maven-bundle-plugin
    true
    
     target/META-INF
     
      ${project.groupId}.${project.artifactId}
      
      
       org.apache.cxf,
       org.apache.cxf.binding,
       org.apache.cxf.binding.corba,
       org.apache.cxf.binding.soap,
       org.apache.cxf.binding.soap.spring,
       org.apache.cxf.bus,
       org.apache.cxf.bus.resource,
       org.apache.cxf.bus.spring,
       org.apache.cxf.buslifecycle,
       org.apache.cxf.catalog,
       org.apache.cxf.configuration,
       org.apache.cxf.configuration.spring,
       org.apache.cxf.endpoint,
       org.apache.cxf.headers,
       org.apache.cxf.management,
       org.apache.cxf.management.jmx,
       org.apache.cxf.phase,
       org.apache.cxf.resource,
       org.apache.cxf.service.factory,
       org.apache.cxf.transport,
       org.apache.cxf.transport.http,
       org.apache.cxf.transport.http.policy,
       org.apache.cxf.transport.http_jetty,
       org.apache.cxf.transport.jms,
       org.apache.cxf.transports.http,
       org.apache.cxf.workqueue,
       org.apache.cxf.wsdl,
       org.apache.cxf.wsdl11,
       org.apache.ws.security.action,
       org.apache.ws.security.processor,
       org.springframework.beans.factory.config,
       org.springframework.mail.javamail,
       *
      
     
    
   

Now we are readly to deploy the bundle to karaf container. The command is:

    osgi:install mvn:com.vha.esb.apexus/ce-reject-notify/1.0.0-SNAPSHOT
    osgi:start 244

Now, if your karaf container is at ${karaf_home}, you should see the directory ${karaf_home}/src/data. Now, let's copy the 2 xml files coming with the build to that directory. Shortly, you should see message1.xml and message2.xml are placed at ${karaf_home}/target/messages/uk and ${karaf_home}/target/messages/other directory, respectively.

Understanding Apache Camel Route

The archtetypes: camel-archetype-spring create a default camel route which copy the xml file from one place to anotehr. I add another route using quartz end point as shown blow:






 
  
 


 

 

 



 
  
   
  
            
   
    
   
   
   
  
   
   
  
   
   
    
     /person/city = 'London'
     
     
    
    
     
     
    
   
  
  
  
 



The update route shows few things. First, it is a spring configuration file. We can define beans inside the configuraion. Secondly, we can define multiple routes within a camelContext.

The camel configuration file shown above is OK, but not very practical because we hard-coded thte value. Now, i want to explain how to use property files. To use property file, we need to update the came-context.xml file. Here is the list of the file content.






       ......

 
  
 



 
  
  
  
   
  
            
   
    
   
   
   
  
   
         ......
  
  
 



As you can see, we use spring PropertyPlaceholderConfigurer bean to load file. and in the camel route you use {{my.value}}, where my.value is a variable. I deployed the file osgi.spring.demo.cfg under ${KARAF.HOME}/etc. The ${KARAF.HOME} is the karaf container installation. The file osgi.spring.demo.cfg has the following contents:

activemq.broker.url=failover:(tcp://uit-amq-1.corp.vha.com:61616,tcp://uit-amq-2.corp.vha.com:61616)
name.value=Gary Liu
cron.value=0/5+*+*+*+*+?

After my new deployment of the bundle, in the karaf log, I can see the following out put every 5 seconds, because in the quartz end point, I have defined the field for second 0/5, which is every 5 seconds.

14:48:50,003 | INFO  | amel-21_Worker-2 | rg.apache.camel.util.CamelLogger  176 | 174 - org.apache.camel.camel-core - 2.10.4 | The message contains Gary Liu at 2014-02-12 14:48:50 failover:(tcp://uit-amq-1.corp.vha.com:61616,tcp://uit-amq-2.corp.vha.com:61616)
14:48:55,001 | INFO  | amel-21_Worker-3 | rg.apache.camel.util.CamelLogger  176 | 174 - org.apache.camel.camel-core - 2.10.4 | The message contains Gary Liu at 2014-02-12 14:48:55 failover:(tcp://uit-amq-1.corp.vha.com:61616,tcp://uit-amq-2.corp.vha.com:61616)

Conclusion

Basically, to create Apache Camel route OSGi bundles from scratch, you can simply use the org.apache.camel.archetypes archetype to generate a project. Then you use the apache felix maven plugin to generate OSGi bundle.

No comments:

Post a Comment

Anypoint Studio Error: The project is missing Munit lIbrary to run tests

Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...