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 java.util.List;
020    
021    import org.apache.activemq.ActiveMQConnectionFactory;
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.RoutesBuilder;
024    import org.apache.camel.builder.RouteBuilder;
025    import org.apache.camel.component.jms.JmsComponent;
026    
027    import org.apache.camel.osgi.SpringCamelContextFactory;
028    import org.apache.camel.spring.SpringCamelContext;
029    import org.apache.camel.spring.javaconfig.Main;
030    import org.apache.camel.spring.javaconfig.SingleRouteCamelConfiguration;
031    import org.osgi.framework.BundleContext;
032    import org.springframework.beans.factory.InitializingBean;
033    import org.springframework.context.annotation.Bean;
034    import org.springframework.context.annotation.Configuration;
035    import org.springframework.osgi.context.BundleContextAware;
036    
037    //START SNIPPET: RouteConfig
038    /**
039     * A simple example router from a file system to an ActiveMQ queue and then to a file system
040     *
041     * @version $Revision: 17117 $
042     */
043    @Configuration
044    public class MyRouteConfig extends SingleRouteCamelConfiguration implements InitializingBean, BundleContextAware {
045        
046        private BundleContext bundleContext;
047        
048        /**
049         * Allow this route to be run as an application
050         *
051         * @param args
052         * @throws Exception 
053         */
054        public static void main(String[] args) throws Exception {
055            new Main().run(args);
056        }
057        
058        public BundleContext getBundleContext() {
059            return bundleContext;
060        }
061    
062        public void setBundleContext(BundleContext bundleContext) { 
063            this.bundleContext = bundleContext;
064        }
065        
066        
067        /**
068         * Returns the CamelContext which support OSGi
069         */
070        @Override
071        protected CamelContext createCamelContext() throws Exception {
072            SpringCamelContextFactory factory = new SpringCamelContextFactory();
073            factory.setApplicationContext(getApplicationContext());
074            factory.setBundleContext(getBundleContext());
075            return factory.createContext();
076        }
077        
078        @Override
079        // setup the ActiveMQ component and register it into the camel context
080        protected void setupCamelContext(CamelContext camelContext) throws Exception {
081            JmsComponent answer = new JmsComponent();
082            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
083            connectionFactory.setBrokerURL("vm://localhost.spring.javaconfig?marshal=false&broker.persistent=false&broker.useJmx=false");
084            answer.setConnectionFactory(connectionFactory);        
085            camelContext.addComponent("jms", answer);        
086        }
087    
088        
089        public static class SomeBean {
090            public void someMethod(String body) {
091                System.out.println("Received: " + body);
092            }
093        }
094    
095        @Bean
096        @Override
097        // you can confige the route rule with Java DSL here
098        public RouteBuilder route() {
099            return new RouteBuilder() {
100                public void configure() {
101                    // populate the message queue with some messages
102                    from("file:src/data?noop=true").
103                            to("jms:test.MyQueue");
104    
105                    from("jms:test.MyQueue").
106                            to("file://target/test?noop=true");
107    
108                    // set up a listener on the file component
109                    from("file://target/test?noop=true").
110                            bean(new SomeBean());
111                }
112            };
113        }
114    
115        public void afterPropertiesSet() throws Exception {
116            // just to make SpringDM happy do nothing here
117        }
118    }
119    //END SNIPPET: RouteConfig