Wednesday 20 October 2010

My first jruby javabean

I was going to write a javabean to be used inside camel in a java route like this:

from("file:src/data?noop=true").
     beanRef("addCorrelationId").
     log("log:endRoute");

in the MyRouteBuilder.java file of a standard mvn generated camel project.

Of course the bean has to be recalled in the came-context.xml:

<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://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <bean id="addCorrelationId" class="com.test.AddCorrelationId"/>

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <package>com.test</package>
  </camelContext>

</beans>

So I created inside src/main/ruby/com/test the following file (add_correlation_id.rb):
require 'java'

java_import "org.apache.camel.Exchange"
java_import "org.apache.camel.Processor"
java_import "org.slf4j.Logger"
java_import "org.slf4j.LoggerFactory"
java_import "ch.qos.logback.classic.LoggerContext"

java_package "com.test"

class AddCorrelationId
  def initialize
    @logger = LoggerFactory.getLogger("com.test.#{__FILE__}");
    @logger.debug("Hello from #{__FILE__}.");    
  end  

#java_annotation 'Exchange'
java_signature 'void process(Exchange exchange)'
  def process(exchange)
    @logger.debug(File.open(exchange.in.body.getBody.toString).read);    
    exchange.out.set_header("JMSCorrelationId", "mytestcorrelationId")
  end

end

Please note the java_signature statement to instruct camel to bind to the process function.

According to CamelInAction it should be possible to enforce the binding of certain parameter in the process function using annotation. Unfortunately I wasn't able to.

Next one need to compile jruby to a java source class:
jruby -S jrubyc --java -t src/main/java src/main/ruby/con/test
And run the maven camel task:
mvn camel:run
You should see printed the body of the files, both on console and to the target of logback (defined in src/main/resources/logback.xml). The header adding task has no effect on a file.

In order to use the compelling logback library, I had to add to the pom.xml:
 <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-core</artifactId>
      <version>${logback-version}</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>${logback-version}</version>
    </dependency>
    <dependency>
      <groupId>org.jruby</groupId>
      <artifactId>jruby-complete</artifactId>
      <version>1.5.3</version>
    </dependency>

Wednesday 6 October 2010

jruby processor for camel with jruby1.5

Here how to wrote a camel processor in jruby. A processor is a class able to modify message exchange like adding a header, changing message or just doing log.

First of all a jruby1.5 is required.

To start a camel project launch the maven task:
mvn archetype:create -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-java -DarchetypeVersion=2.4.0 -DgroupId=com.test -DartifactId=rubyProcessor

Move to the created rubyProcessor directory and create a custom src/main/ruby/com/test directory and place there the following:

require 'java'
java_import "org.apache.camel.Exchange"
java_import "org.apache.camel.Processor"

java_package "com.test"

class DownloadLogger
  java_signature 'DownloadLogger()'
  java_implements 'Processor'

  java_signature "void process(Exchange exchange)"
  def process(exchange)
    puts "Ruby downloaded: #{exchange.in.get_header("CamelFileName")}"
  end
end

Modify java source file at src/main/java/com/test/MyRouteBuilder.java to:
 from("file:src/data?noop=true").
     process(new it.unimore.cesia.DownloadLogger()).
            choice().
                when(xpath("/person/city = 'London'")).to("file:target/messages/uk").
                otherwise().to("file:target/messages/others");

Add to the pom.xml file the jruby dependency:
 
      org.jruby
      jruby-complete
      1.5.3
    
located with sonatype nexus service.

Now compile with jrubyc the ruby source class with:
/opt/jruby-1.5.1/bin/jrubyc --java src/main/ruby/com/test/download_logger.rb
cp -v  com/test/DownloadLogger.java src/main/java/it/com/test
The java source class is created in the com/test directory of the presente working directory: it might be wise to move to src/main/java, maybe, so the cp can be saved.

Then execute camel application with mvn camel:run.

After a while you should be seeing:
Ruby downloaded: message2.xml
Ruby downloaded: message1.xml