Thursday, 30 June 2011

Camel producerTemplate in ruby

The aim is to send messagges to a queue with the camel producerTemplate. producerTemplate can be invoked from a Processor. This time I'm going to use a jruby processor.

First of all generate a camel project with:
mvn archetype:generate \\
-DarchetypeGroupId=org.apache.camel.archetypes \\ 
-DarchetypeArtifactId=camel-archetype-java \\
-DarchetypeVersion=2.7.0 -DgroupId=com.test -DartifactId=producer

Then modify pom.xml to import all dependencied:
<name>## Producer ##</name>
  <url>http://www.myorganization.org</url>
  <properties>
    <camel-version>2.7.0</camel-version>
    <log4j-version>1.2.16</log4j-version>
    <logback-version>0.9.24</logback-version>
    <activemq-version>5.5.0</activemq-version>
    <jruby-version>1.6.2</jruby-version> 
    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>${camel-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-spring</artifactId>
      <version>${camel-version}</version>
    </dependency>
    
    <!-- logging -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.5.11</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.16</version> 
    </dependency>

    <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>${jruby-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jms</artifactId>
      <version>${camel-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-camel</artifactId>
      <version>${activemq-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-core</artifactId>
      <version>${activemq-version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-script</artifactId>
      <version>${camel-version}</version>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
      <version>2.1_3</version>
    </dependency>
  </dependencies>

Define the resources needed in file src/main/resources/META-INF/spring/camel-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->

<!-- Configures the Camel Context-->

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/lang 
      http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
      http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
  

 <bean id="credentials" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name='location'>
      <value>credential.properties</value>
    </property>
  </bean>

<!--
<bean id="credentials" class="org.apache.camel.component.properties.PropertiesComponent">
  <property name="location" value="classpath:credential.properties" />
</bean>
-->
  <bean id="local-activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" >
    <property name="connectionFactory">
      <bean class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="failover://(tcp://localhost:61616)" />
        <property name="userName" value="${activemq.username}"/>
        <property name="password" value="${activemq.password}"/>
      </bean>
    </property>
  </bean>

  <lang:jruby id="producer"
              script-interfaces="org.apache.camel.Processor"
              script-source="classpath:ruby/producer.rb">
    <lang:property name="template" ref="producerTemplate" />
  </lang:jruby>


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

    <template id="producerTemplate"/>
  </camelContext>

</beans>

Please note the added 'lang' namespace in premable and relative expanded xsi:schemaLocation. Missing that will issue a error.

Note also that credential for activemq should be written in src/main/resources/credential.properties, for example:
activemq.username=system
activemq.password=manager

Than create the route in file src/main/java/com/test/MyRouteBuilder.java:
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.test;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.Main;

import static org.apache.camel.builder.xml.XPathBuilder.xpath;

/**
 * A Camel Router
 */
public class MyRouteBuilder extends RouteBuilder {

    /**
     * A main() so we can easily run these routing rules in our IDE
     */
    public static void main(String... args) throws Exception {
        Main.main(args);
    }

    /**
     * Lets configure the Camel routing rules using Java code...
     */
    public void configure() {

 from("timer://producer_timer?fixedRate=true&delay=5000&period=10000").
     errorHandler(loggingErrorHandler("com.test")).
     id("producer").
     setBody(constant("Hallo from producer")).
     beanRef("producer");

    }
}
This is a simple route which triggers each 10 seconds, creates a empty message, changes its body to 'Hallo from producer' then sends it to producer bean.



Now it's time to write the processor, which in turn uses producerTemplate. According to camel-context.xml source file should be in src/main/resources/ruby/producer.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 "it.unimore.cesia"

class Producer
  include Processor

  def initialize
    @logger = LoggerFactory.getLogger("com.test.producer")
    @logger.info("Hello from Producer")
  end  

  def setTemplate(template)
    @@template = template
  end

#java_annotation 'Exchange'
java_signature 'void process(Exchange exchange)'
  def process(exchange)
    msg = exchange.in.body
    @logger.info("Producing at #{Time.now.strftime('%H:%M')}")
    @@template.sendBody("local-activemq:queue:producer.audit", "#{msg} at #{Time.now.strftime('%H:%M')}")

  end

end

Route can be activated with:
mvn camel:run

If you want to activate logging, create a logback.xml file in src/main/resource. For example for a syslog/file logging:
<configuration scan="true" scanPeriod="30 seconds">
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>producer.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <FileNamePattern>producer-%d{yyyy-MM-dd}.log</FileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--      <pattern>%date %level [%thread] %logger{10} %msg%n</pattern> -->
      <Pattern>%date %msg%n</Pattern>
      <charset>UTF-8</charset>
    </encoder>
  </appender>

  <appender name="SYSLOG"
    class="ch.qos.logback.classic.net.SyslogAppender">
    <SyslogHost>localhost</SyslogHost>
    <Facility>DAEMON</Facility>
    <SuffixPattern>[%logger] %msg</SuffixPattern>
  </appender>

  <logger name="com.test.producer" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="FILE" /> 
    <appender-ref ref="SYSLOG" />
  </logger>

  <logger name="com.test" additivity="false">
    <level value="INFO"/>
    <appender-ref ref="FILE" /> 
    <appender-ref ref="SYSLOG" />
  </logger>


  <root level="ERROR">
<!--
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" /> -->
    <appender-ref ref="SYSLOG" />
  </root>
</configuration>

Monday, 13 June 2011

My first camel processor in scala

First create a maven project with:
mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes \\
-DarchetypeArtifactId=camel-archetype-scala -DarchetypeVersion=2.7.2 \\
-DgroupId=com.unitest -DartifactId=my_test

It creates a my_test project with a route defined in src/main/scala/com/unitest/MyRouteBuilder.scala, which can be modified as:

"timer://foo?fixedRate=true&delay=5s&period=10s" ==> {
     bean("myBean") 
     to("log:bean")
   }

Where myBean is referenced in src/main/resources/META-INF/spring/camel-context.xml as:
<bean id="myBean" class="com.unitest.MyBean"/>

and class MyBean, in folder src/main/scala/com/unitest is:
package com.unitest

import org.apache.camel.Exchange

class MyBean {

  def process(exchange: Exchange) {
    exchange.getIn.setBody("EpiReplicaQuery test")
   }

Execute with mvn camel:run and that's all.

Thursday, 9 June 2011

juniper ssl vpn appliance and shibboleth2.2.1

First step is reading:
http://shibboleth.1660669.n2.nabble.com/Juniper-SSLVPN-integration-td3575845.html

There are also some very clear instuction by P. Geenens (pgeneens@juniper.net).

Something I was unaware was the need to create a Sign-in policy, for instance:
User URLs  Sign-In Page     Authentication Realm(s)
*/saml/  my Sign-In Page  shibboleth

Where shibboleth is the label of the Authentication Realm which uses the shibboleth authentication server.

Now, change the value of Source Site Inter-Site Transfer Service URL to
https://omissis.unitest.com/idp/profile/Shibboleth/SSO?providerId=vpn.unitest.com&shire=https://vpn.unitest.com/dana-na/auth/saml-consumer.cgi&target=https://vpn.unitest.com/saml
where /saml is the path of the Sign-in policy page.

Now as the user connects to https://vpn.unitest.com/saml, she is redirected to shibboleth IdP login page and than back to ssl vpn.

Add an alternative to update-alternatives

Let's start from the use case: you install firefox4.0.1 anf just want it starts from the 'Web Browser' button in the lxde menu bar.

It looks it was about configuring the x-www-browser alternative.

I started from update-alternatives --install, but wasn't able to figure out how to name the 4 parameters.

While doing some tries, I deleted the /usr/bin/x-www-browser link, which pointed to /etc/alternatives/x-www-browser which was a link to /usr/bin/iceweasel.

I gave up and linked /usr/local/src/firefox/firefox to /etc/alternatives/x-www-browser and again to /usr/bin/x-www-browser and opla'! Not only the 'Web Browser' button works, but also the alternative stuff is ok:

sudo update-alternatives --list x-www-browser
/usr/bin/epiphany-browser
/usr/bin/iceweasel
/usr/local/src/firefox/firefox

Friday, 10 December 2010

Activemq stomp temporary queue

Temporary queues are used to the message-reply pattern. Should a service require a response from another service, the caller should create a temporary queue, and set the JMS header JMSReplyTo to the name of the temporary queue.

The answerer sends back response to the address specified in JMSReplyTo which is a temporary queue which can be read only the process who created it and that will be destroyed as soon as it has been used.

There is everywhere instructions how to implement the request-response pattern with jms, here how to do it with stomp with apache activemq.

This is the ask.rb file: it was just a slight modified version of the catstomp file in the activemq distribution.

#!/usr/bin/env ruby
#
#   Copyright 2006 LogicBlaze Inc.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
begin; require 'rubygems'; rescue; end
require 'stomp'

begin

    @port = 61613
    @host = "localhost"
    @user = "system"
    @password = "manager"

    @destination = "/queue/hallo.world"

    $stderr.print "Connecting to stomp://#{@host}:#{@port} as #{@user}\n"
    @conn = Stomp::Connection.open(@user, @password, @host, @port, true)
    $stderr.print "Sending input to #{@destination}\n"

  reply_to = "/temp-queue/"

  @headers = {'persistent'=>'false', 'reply-to'=> reply_to}

  @conn.publish @destination, "hallo", @headers

  @conn.subscribe(reply_to, { :ack =>"client" })
  $stderr.print "waiting answer from #{reply_to}\n"
  @msg = @conn.receive
  $stdout.puts @msg.body
  $stdout.flush
  @conn.ack @msg.headers["message-id"]

rescue Exception => e
  puts "rescued #{e}"
end

All the magic is just send message to the /temp-queue/ destination. Activemq take care to create a temporary queue named /remote-temp-queue/ID:hostname-46977-1290515981859-6:31:1.

The answerer looks like (again thanks to the distribution stompcat):
#!/usr/bin/env ruby
#
#   Copyright 2006 LogicBlaze Inc.
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
begin; require 'rubygems'; rescue; end
require 'stomp'

begin

    @port = 61613
    @host = "localhost"
    @user = "system"
    @password = "manager"


    @destination = "/queue/hallo.world"

    $stderr.print "Connecting to stomp://#{@host}:#{@port} as #{@user}\n"
    @conn = Stomp::Connection.open(@user, @password, @host, @port, true)
    $stderr.print "Getting output from #{@destination}\n"

    @conn.subscribe(@destination, { :ack =>"client" })
    while true
      @msg = @conn.receive
      reply_to = @msg.headers['reply-to']
      $stdout.print "reply_to: #{reply_to}\n"
      $stdout.print @msg.body
      $stdout.flush
      @conn.ack @msg.headers["message-id"]
      @conn.publish reply_to, "#{@msg.body} world"
    end

rescue Exception => e
  puts "rescued: #{e}"
end

To post the answer, it is enought to read the reply-to header from the request message and post there.

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