001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.example.spring.javaconfig;
018    
019    import org.apache.activemq.ActiveMQConnectionFactory;
020    import org.apache.camel.CamelContext;
021    import org.apache.camel.builder.RouteBuilder;
022    import org.apache.camel.component.jms.JmsComponent;
023    import org.apache.camel.osgi.SpringCamelContextFactory;
024    import org.apache.camel.spring.javaconfig.Main;
025    import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
026    import org.osgi.framework.BundleContext;
027    import org.springframework.beans.factory.InitializingBean;
028    import org.springframework.context.annotation.Bean;
029    import org.springframework.context.annotation.Configuration;
030    import org.springframework.osgi.context.BundleContextAware;
031    
032    //START SNIPPET: RouteConfig
033    /**
034     * A simple example router from a file system to an ActiveMQ queue and then to a file system
035     *
036     * @version 
037     */
038    @Configuration
039    public class MyRouteConfig extends SingleRouteCamelConfiguration implements InitializingBean, BundleContextAware {
040        
041        private BundleContext bundleContext;
042        
043        /**
044         * Allow this route to be run as an application
045         */
046        public static void main(String[] args) throws Exception {
047            new Main().run(args);
048        }
049        
050        public BundleContext getBundleContext() {
051            return bundleContext;
052        }
053    
054        public void setBundleContext(BundleContext bundleContext) { 
055            this.bundleContext = bundleContext;
056        }
057    
058        /**
059         * Returns the CamelContext which support OSGi
060         */
061        @Override
062        protected CamelContext createCamelContext() throws Exception {
063            SpringCamelContextFactory factory = new SpringCamelContextFactory();
064            factory.setApplicationContext(getApplicationContext());
065            factory.setBundleContext(getBundleContext());
066            return factory.createContext();
067        }
068        
069        @Override
070        protected void setupCamelContext(CamelContext camelContext) throws Exception {
071            // setup the ActiveMQ component
072            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
073            connectionFactory.setBrokerURL("vm://localhost.spring.javaconfig?marshal=false&broker.persistent=false&broker.useJmx=false");
074    
075            // and register it into the CamelContext
076            JmsComponent answer = new JmsComponent();
077            answer.setConnectionFactory(connectionFactory);
078            camelContext.addComponent("jms", answer);
079        }
080    
081        
082        public static class SomeBean {
083    
084            public void someMethod(String body) {
085                System.out.println("Received: " + body);
086            }
087    
088        }
089    
090        @Bean
091        @Override
092        public RouteBuilder route() {
093            return new RouteBuilder() {
094                public void configure() {
095                    // you can configure the route rule with Java DSL here
096    
097                    // populate the message queue with some messages
098                    from("file:src/data?noop=true").
099                            to("jms:test.MyQueue");
100    
101                    from("jms:test.MyQueue").
102                            to("file://target/test?noop=true");
103    
104                    // set up a listener on the file component
105                    from("file://target/test?noop=true").
106                            bean(new SomeBean());
107                }
108            };
109        }
110    
111        public void afterPropertiesSet() throws Exception {
112            // just to make SpringDM happy do nothing here
113        }
114    
115    }
116    //END SNIPPET: RouteConfig
117