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>