Monday, February 28, 2011

Beginners guide to Apache Camel with ServiceMix: Part2

This is part 2 of the Beginners guide to Apache Camel with ServiceMix. The first part explained how to set up the environment, to run the examples developed in this post. You can find part 1 here. In this section we will create a Maven project with the camel-spring archetype, change the route with Fuse IDE for Camel, test the route in Eclipse and deploy the route to ServiceMix.


Developing, testing and deploying a simple route
Create Maven project in Eclipse
  • In Eclipse go to File - New - Project...
  • Choose Maven - Maven Project
  • Choose Use default Workspace location and Next
  • Select an archetype
    • Catalog: Fuse Catalog
    • Filter: camel
    • Choose: camel-archetype-spring
    • Next
Click to enlarge 
  • Set archetype parameters:
    • GroupId: tutorial
    • ArtifactId: simple_router
    • Version: 1.0.0
    • Package: tutorial.simple_router
    • Finish
Click to enlarge
  • This creates a new Maven project with the following structure. It would be advised to change the name of the camel xml file to something more descriptive (simple-router in this case), as this is the file that will be deployed.
 
  • If we double click this file we can get a graphical overview of the routing together with the properties of each node. You can change the mapping by deleting nodes, dragging in new nodes and changing the properties of each node.
Click to enlarge

Click to enlarge
Testing the route in Eclipse
  • To test the route click on the root of the project - Run As - Run Configurations.
Click to enlarge
  • Run Configuration:
    • Base directory: Set the base directory to your project root "C:\Documents and Settings\Administrator\workspace\simple_router"
    • Goals: camel:run
    • Run
Click to enlarge
  • If everything succeeds you should see something like this: "Apache Camel 2.6.0 (CamelContext: camel-1) started in 1.421 seconds"
Deploying the route to ServiceMix
Deploying the route to ServiceMix is as simple as copying the camel xml file (spring file) to the deploy directory in ServiceMix. 
  • Fire up ServiceMix
    • In cmd change directory to the ServiceMix\bin directory. 
    • Execute servicemix.bat
Click to enlarge
  • Copy the "simple-router.xml" file 
    • found in this directory C:\Documents and Settings\Administrator\workspace\simple_router\src\main\resources\META-INF\spring
    • ...to the deployment directory directory C:\Program Files\Apache Software Foundation\apache-servicemix-4.3.0-fuse-02-00\deploy
  • To see if the route has been successfully deployed you can use the Apache Felix Web Console. 
    • To install the console run the following command in karaf prompt:
      features:install webconsole
    • You can access the Web Console from the following address:
      http://localhost:8181/system/console
    • Use "smx" as default username and "sxm" as default password.
Click to enlarge
Testing the route
  • To see if the route works, drop a file in the ServiceMix\src\data directory.
Click to enlarge
  • Moments later the file should land on the ServiceMix\target\messages target directory (either "uk" or "other" depending on the file content)
Click to enlarge

21 comments:

  1. it's works, thanks for the nice tutoriel, but i don't understand very well the use case. finally i'm new in fuse esb, can you give me some advice for a simple helloworld example.

    ReplyDelete
  2. Hi Safi, I'm glad the tutorial worked for you. I had no specific use case in mind for this. My main aim was to get a simple Fuse IDE for Camel example working and deployed to Fuse ESB (ServiceMix). The tutorial shows you how to start a simple Camel development. Once your environment has been set up and the maven project created and deployed, you can modify it to meet any requirement. I think this is the power of FuseIDE for Camel. In the past it wasn't so easy to change a scenario without breaking something.

    Regarding your request for a Fuse ESB example; the above tutorial already deployed a simple Camel route to the Fuse ESB (ServiceMix). Remember, Fuse ESB consists of the different Fuse components like Fuse Mediation Router (Camel), Fuse Message Broker (ActiveMQ) etc. In this tutorial, I deployed the Camel route by simply copying the Camel XML file to the deploy directory of the Fuse ESB. Once this is done, we only need to fire up the Fuse ESB and not all the individual Fuse components.

    I'm hoping to throw ActiveMQ into the mix with my next post. It will explain how to deploy an ActiveMQ\Camel example to ServiceMix.

    Cheers,
    Shaun

    ReplyDelete
  3. Hi Shaun,
    Thanks for response, and i'm waiting for you're next post and hope it help me.

    ReplyDelete
  4. I tried this exercise, I got the following error:
    (Any Ideas)

    Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at com.ning.http.client.AsyncHttpClient.(AsyncHttpClient.java:147)
    at org.sonatype.aether.connector.async.AsyncRepositoryConnector.(AsyncRepositoryConnector.java:150)
    at org.sonatype.aether.connector.async.AsyncRepositoryConnectorFactory.newInstance(AsyncRepositoryConnectorFactory.java:110)
    at org.sonatype.aether.impl.internal.DefaultRemoteRepositoryManager.getRepositoryConnector(DefaultRemoteRepositoryManager.java:333)
    at org.sonatype.aether.impl.internal.DefaultArtifactResolver.resolve(DefaultArtifactResolver.java:456)
    at org.sonatype.aether.impl.internal.DefaultArtifactResolver.resolveArtifacts(DefaultArtifactResolver.java:220)
    at org.sonatype.aether.impl.internal.DefaultArtifactResolver.resolveArtifact(DefaultArtifactResolver.java:197)
    at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.loadPom(DefaultArtifactDescriptorReader.java:268)
    at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.readArtifactDescriptor(DefaultArtifactDescriptorReader.java:173)
    at org.sonatype.aether.impl.internal.DefaultRepositorySystem.readArtifactDescriptor(DefaultRepositorySystem.java:316)
    at org.apache.maven.plugin.internal.DefaultPluginDependenciesResolver.resolve(DefaultPluginDependenciesResolver.java:108)
    at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getPluginDescriptor(DefaultMavenPluginManager.java:143)
    at org.apache.maven.plugin.DefaultBuildPluginManager.loadPlugin(DefaultBuildPluginManager.java:63)
    at org.apache.maven.plugin.prefix.internal.DefaultPluginPrefixResolver.resolveFromProject(DefaultPluginPrefixResolver.java:137)
    at org.apache.maven.plugin.prefix.internal.DefaultPluginPrefixResolver.resolveFromProject(DefaultPluginPrefixResolver.java:120)
    at org.apache.maven.plugin.prefix.internal.DefaultPluginPrefixResolver.resolve(DefaultPluginPrefixResolver.java:84)
    at org.apache.maven.lifecycle.internal.MojoDescriptorCreator.findPluginForPrefix(MojoDescriptorCreator.java:262)
    at org.apache.maven.lifecycle.internal.MojoDescriptorCreator.getMojoDescriptor(MojoDescriptorCreator.java:222)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator.calculateTaskSegments(DefaultLifecycleTaskSegmentCalculator.java:106)
    at org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator.calculateTaskSegments(DefaultLifecycleTaskSegmentCalculator.java:86)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:98)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:534)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
    Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:244)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:230)
    ... 34 more

    ReplyDelete
  5. I suspect there was already an installation of m2eclipse installed. Make sure you uninstall it before installing FUSE IDE. You can find these steps in part 1: http://shoosthuizen-integration.blogspot.com/2011/02/beginners-guide-to-apache-camel-with_28.html.

    ReplyDelete
  6. hey i am having the same problem as 'Human Computer Class' is having and i am sure there is not previous m2eclipse installed. i installed the one provided with the link i.e. http://repo.fusesource.com/beta/eclipse/update/

    I am almost lost with the problem here. Any help will be greatly appreciated.

    ps. i have a doubt if it has got something to do with slf4j or log4j version. but i am not sure what it is

    Using:
    maven: 3.0.2
    jdk: 1.6.0_23
    eclipse:3.6.2

    ReplyDelete
  7. Nice Post.
    In fact, I also ran into the same issue of java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory. Fuse Camel IDE uses maven 3.0.2.xxxx embededded version. I created a maven installation for 3.0.3 and used that to run the sample camel:run. It worked fine.

    ReplyDelete
  8. Thanks Sriraman that worked. I downloaded Maven 3.0.3. Went to Window->Preferences->Maven->Installations, clicked the Add button.
    Selected the Maven 3.0.3 install directory. I also changed the Maven Runtime pointed to by my project run configuration to point to Maven 3.0.3. Auni hopefully this helps.

    ReplyDelete
  9. FYI
    I got the following error when deploying the file to ServiceMix (apache-servicemix-4.3.1-fuse-01-09):
    karaf@root> Exception in thread "SpringOsgiExtenderThread-1" java.lang.IllegalStateException: BeanFa
    ctory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationC
    ontext
    at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(
    AbstractRefreshableApplicationContext.java:171)

    However I searched around and found:
    http://fusesource.com/issues/sr/jira.issueviews:searchrequest-fullcontent/temp/SearchRequest.html?pid=10000&fixfor=10656&component=10090&sorter/field=issuekey&sorter/order=DESC&tempMax=1000

    Specifically:
    =============================================
    In this context the message: "BeanFactory not initialized or already closed" is caused because there is an application context that was waiting for dependencies that were never resolved before shutdown. These messages are hard to grok, but any time you see the following line 2 lines down in the stack trace this is the reason.

    at org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor.close(DependencyWaiterApplicationContextExecutor.java:345)

    So this error message is a symptom of another issue where a spring application context can't start because of missing osgi dependencies.

    Spring has updated this for Spring DM 2.0 so that it doesn't attempt to do this if the context is still pending resolution. I am hoping they backport and release a 1.2.1, but not holding my breath. In the mean time, when you see these messages they themselves can be ignored, but search up through the log for a BeanDefinitionStoreException and that will point you to the bundle and Spring context that failed.
    ==========================================

    So I used the Apache Felix Web Console on the
    Bundles tab to resolve all unresolved the bundles.

    Works now, however it would be nice to know which bundle(s) caused the problem. Anybody know of any debug mechanisms that would aid me in doing so?

    ReplyDelete
  10. Hi!
    i'm also waiting for another good tutoriel from you. the first one is very interessting and i would show for my student! thanks

    ReplyDelete
  11. Hi Safi,

    I've been wanting to do one, but I'm a bit caught up with things at work. Do you have anything specific in mind?

    Regards,
    Shaun

    ReplyDelete
  12. Hi Shaun and thanks for answer!
    I would like to know it's possible for apache camel / servicemix to read data from database?
    Regards,
    Safi

    ReplyDelete
  13. Hi Shaun, Thanks a lot for the nice tutorial its working. I need to understand clearly how messages are routing back end. Could you please elaborate camel with service mix.
    Thanks in advance….
    Shaik

    ReplyDelete
  14. Hi Shaik. I'm glad you like the tutorial. The detailed way in which Camel routes the message is abstract to the developer and is not important in simple scenarios like this one. What is important however is the outcome of the route. You can check what the outcome of the route is supposed to be by either looking at the graphical overview (as displayed in the tutorial), or you can view the generated XML source code by choosing the "Source" tab instead of the "Design" tab at the bottom left of your screen.

    ReplyDelete
  15. Hi Shaun,

    Your post is very nice. I am new to ESB and need to use Apache Servicemix for this. Need help on Apache ServiceMix with sample example.

    ReplyDelete
  16. Hi Shaun,

    Your post is excellent. It worked like a magic in the first attempt it self. This is great. As of now i am not sure what and how it worked, but it worked and now i will dig into it more to understand in detail.

    Please post more detail examples which would help everyone in understanding it.

    regards
    Suneel

    ReplyDelete
  17. Hi Shaun,

    A very good post on the ServiceMix (Fuse ESB) configuration.
    I am new to this framework (ServiceMix) and trying only the Apache ServiceMix.

    Not able to find a step-by-step approach for configuring Email connectivity with any Mail Server.
    Any help in this regard will be appreciated.

    Thanks,

    Sanjay

    ReplyDelete
  18. The default username/password combination for web console when one uses JBoss Fuse ESB now would be karaf/karaf . Also didn't relies until i look at the route that the inout file needed is an xml file.

    ReplyDelete
  19. Hi ,
    Your article is very nice.
    I am very new to servicemix. now in my project we are using apache servicemix4.4.1-fuse-03-06, cxf, camel , spring and webserivces .

    Can give a post on how to work by using above things ?
    and also I downloaded servicemix 4.4.1 from apache sit, but instance folder is not coming .
    so while sarting up the servicemix get error like can not find the folder instance.
    how can I download this ?
    is there any differences between servicemix 4.4.1 and servicemix4.4.1-fuse-03-06?

    Kindly help me out

    Regards,
    Kishore

    ReplyDelete
  20. Good article, I've seen many articles today, but only this article is of interest to me, thanks judi poker online yang aman dan terpercaya

    ReplyDelete
  21. Great Article shaun, I am new to servicemix and camel. In article while setting up the IDE the link to the fuse IDE is not working as Fuse is now with JBoss. can we develop the same code in Jboss and deploy on service mix? Is there any additional steps for this.

    ReplyDelete