Spring Java Config ExampleThe spring java config example is a simple refactor of the spring example since Camel 2.0 to show how to use the Spring JavaConfig approach to working with Camel. In this example we just write RouteBuilder implementations, then Camel will find it through your configuration. 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-javaconfig mvn camel:run You need to add camel-spring-javaconfig dependency into pom.xml and also set the configure class or base package in the camel plugin configuration.
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-javaconfig</artifactId>
</dependency>
Here is the configuration for camel plugin. <plugins> <!-- Allows the routes to be run via 'mvn camel:run' --> <plugin> <groupId>org.apache.camel</groupId> <artifactId>camel-maven-plugin</artifactId> <version>${project.version}</version> <configuration> <duration>5s</duration> <configClasses>org.apache.camel.example.spring.javaconfig.MyRouteConfig</configClasses> <!--You could set the base package directory and let spring to find the config classes for you <basedPackages>org.apache.camel.example.spring.javaconfig</basedPackages> --> </configuration> </plugin> </plugins> What this does is boot up the Spring ApplicationContext defined in the file MyRouteConfig class on the classpath. This is a regular Java file which has the Spring JavaConfig annotation to configure a CamelContext. /** * A simple example router from a file system to an ActiveMQ queue and then to a file system * * @version $Revision$ */ @Configuration public class MyRouteConfig extends SingleRouteCamelConfiguration implements InitializingBean, BundleContextAware { private BundleContext bundleContext; /** * Allow this route to be run as an application * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { new Main().run(args); } public BundleContext getBundleContext() { return bundleContext; } public void setBundleContext(BundleContext bundleContext) { this.bundleContext = bundleContext; } /** * Returns the CamelContext which support OSGi */ @Override protected CamelContext createCamelContext() throws Exception { SpringCamelContextFactory factory = new SpringCamelContextFactory(); factory.setApplicationContext(getApplicationContext()); factory.setBundleContext(getBundleContext()); return factory.createContext(); } @Override // setup the ActiveMQ component and register it into the camel context protected void setupCamelContext(CamelContext camelContext) throws Exception { JmsComponent answer = new JmsComponent(); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); connectionFactory.setBrokerURL("vm://localhost.spring.javaconfig?marshal=false&broker.persistent=false&broker.useJmx=false"); answer.setConnectionFactory(connectionFactory); camelContext.addComponent("jms", answer); } public static class SomeBean { public void someMethod(String body) { System.out.println("Received: " + body); } } @Bean @Override // you can confige the route rule with Java DSL here public RouteBuilder route() { return new RouteBuilder() { public void configure() { // populate the message queue with some messages from("file:src/data?noop=true"). to("jms:test.MyQueue"); from("jms:test.MyQueue"). to("file://target/test?noop=true"); // set up a listener on the file component from("file://target/test?noop=true"). bean(new SomeBean()); } }; } public void afterPropertiesSet() throws Exception { // just to make SpringDM happy do nothing here } } In the method of setupCamelContext(CamelContext camelContext), we setup the JMS component's connection factory and register the component into the camelcontext. You can override this method if you want to setup the another connection factory or start up a JMS broker here. You can write the route rule with Java DSL in the route() method. This approach, of using Java code to write the routes in the DSL and Spring will help your configure the Camel context as the Spring Java Config Example shows. |