Sunday 3 April 2011

JBoss 5 + Apache Camel + Spring + EJB

The following is a step-by-step guide to how to make Apache Camel 2.6.0 and Spring work with JBoss 5.

Step 1. Install JBoss
Download JBoss Application Server 5.1.0.GA. Extract the contents of the zip file to a directory of your choice (i.e. c:\jboss-5.1.0.GA). We are going to use the default server for this example.

Step 2. Install Snowdrop
Download JBoss Snowdrop version 1.1.0.GA. Snowdrop is a utility package that contains JBoss-specific extensions to the Spring Framework. Extract the contents of the zip file (spring.deployer) to the deployers folder (C:\jboss-5.1.0.GA\server\default\deployers):

Step 3. Download camel-jboss package scan loader
Download camel-jboss from camel-extra. The lirary contains JBoss specific package scan classloader to be used when Camel is running inside JBoss Application Server. JBoss uses LPGL license which means that camel-jboss cannot be hosted at Apache. Alternatively you can get a copy of the library from here.

Step 4. Install camel-jboss in Maven repository
Install the camel-jboss library in your Maven repository as follows:

mvn install:install-file -DgroupId=org.apache.camel -DartifactId=camel-jboss -Dversion=2.3.0 -Dpackaging=jar -Dfile=/path/to/camel-jboss-2.3.0.jar

Step 5. Include Maven dependencies
Add the following dependencies to your ejb's pom:

            org.jboss.ejb3
            jboss-ejb3-ext-api
            1.0.0
            
                
                    org.jboss.javaee
                    jboss-ejb-api
                
                
                    org.jboss.metadata
                    jboss-metadata
                
            
        
        
            javax.ejb
            ejb-api
            3.0
            provided
        
        
            org.springframework
            spring-beans
            3.0.2.RELEASE
        
        
            org.springframework
            spring-core
            3.0.2.RELEASE
        
        
            org.jboss.snowdrop
            snowdrop-deployers
            1.1.0.GA
        

        
            org.apache.camel
            camel-core
            2.6.0
        

        
            org.apache.camel
            camel-spring
            2.6.0
        

        
            org.apache.xbean
            xbean-spring
            3.4
            
                
                    org.springframework
                    spring
                
            
        

        
            org.apache.camel
            camel-jboss
            2.3.0
        

Step 6. Add JBoss class resolver
You will have to set the class resolver on the CamelContext which can be done using Java DSL as follows:

PackageScanClassResolver jbossResolver = new JBossPackageScanClassResolver();

  CamelContext context = new DefaultCamelContext();
  context.setPackageScanClassResolver(jbossResolver);

or adding the following bean definition in the jboss-spring.xml file using Spring XML:



Step 7. Create your RouteBuilder and add it to jboss-spring.xml
By doing so you let JBoss Application Server container manage Camel’s lifecycle. First you define the route:
package org.example.camel;

import org.apache.camel.builder.RouteBuilder;

/**
 * @author Eduardo Sanchez-Ros
 */
public class ExampleRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        
        from("file:C:\\temp\\source")
                .convertBodyTo(String.class)
                .process(new ExampleRouteProcessor())
                .to("file:C:\\temp\\destination");

    }
}

and then you add it to the jboss-spring.xml file:



    
    
        
    

Step 8. Create your enterprise application
To demonstrate the previous I have created an example project that will create a route from file://C:\temp\source folder to file://C:\temp\destination and display the content of the file by calling a Stateless session bean.

Step 9. Build and deploy your ear
Copy your enterprise archive into the deploy folder.

You can download the source code from here.

No comments:

Post a Comment