DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
Spring Example
The spring example is a simple refactor of the first example to show how to use the Spring approach to working with Camel. In this example we just write RouteBuilder implementations, then we write a Spring ApplicationContext XML file to configure Camel so that it auto-discovers the routes on the classpath.
To run the example we use the Camel Maven Plugin. For example from the source or binary distribution the following should work
cd examples/camel-example-spring mvn camel:run
What this does is boot up the Spring ApplicationContext defined in the file META-INF/spring/camel-context.xml on the classpath. This is a regular Spring XML document which uses the Camel Xml Configuration to configure a CamelContext. Notice how the <camelContext> element is configured
<camelContext xmlns="http://activemq.apache.org/camel/schema/spring">
<package>org.apache.camel.example.spring</package>
...
The packages attribute defines the comma separated list of Java package names which are recursively searched for Java classes on the classpath which implement the RouteBuilder interface. What this means is that Camel will automatically detect our MyRouteBuilder class and automatically install its routes. Of course, in practice you should not specify org.apache.camel (or a sub package of this) as the package name because this will instruct Camel to search in its own packages for your routes.
This approach, of using Java code to write the routes in the DSL and just wire together in XML what we really need to allows us to use the right language for the job and minimise the amount of XML bloat. Of course if you prefer you can create all of your routes in the Xml Configuration.
Also note at the end of this XML example file we explicitly configure the ActiveMQ component with details of how to connect to the broker.
2 Comments
James Zhang
Using spring.xml to Walk through an Example
springExampleUsingNamespaces.xml
The example uses spring.xml to configure Routes .
<?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. --> <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.0.xsd http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd "> <!-- START SNIPPET: example --> <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="test-jms:queue:test.queue"/> <process ref="logger"/> <to uri="file:target/data"/> <process ref="logger"/> </route> </camelContext> <bean id="logger" class="org.apache.camel.processor.Logger"/> <bean id="test-jms" class="org.apache.camel.component.jms.JmsComponent"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="vm://localhost?broker.persistent=false"/> </bean> </property> </bean> <!-- END SNIPPET: example --> </beans>Main.java
public class Main { public static void main(String args[]) throws Exception { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext( "org/apache/camel/example/spring/springExampleUsingNamespaces.xml"); SpringCamelContext camelContext = (SpringCamelContext) applicationContext .getBean("camel"); CamelTemplate template = new CamelTemplate(camelContext); camelContext.start(); template.sendBody("test-jms:queue:test.queue", "Test Message" ); Thread.sleep(1000); camelContext.stop(); } }James Zhang
Change the ETL Example to use JMS
from("file:src/data?noop=true").convertBodyTo(PersonDocument.class) .convertBodyTo(CustomerEntity.class).to( "activemq:queue:test.queue"); from("activemq:queue:test.queue").setHeader( FileComponent.HEADER_FILE_NAME, el("${in.body.userName}.xml")) .to("file:target1");