001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.camel.example.jmstofile;
020    
021    import org.apache.camel.component.jms.JmsComponent;
022    import javax.jms.ConnectionFactory;
023    import org.apache.activemq.ActiveMQConnectionFactory;
024    import org.apache.camel.CamelTemplate;
025    import org.apache.camel.CamelContext;
026    import org.apache.camel.Exchange;
027    import org.apache.camel.Processor;
028    import org.apache.camel.builder.RouteBuilder;
029    import org.apache.camel.impl.DefaultCamelContext;
030    
031    
032    /**
033     * An example class for demonstrating some of the basics behind camel
034     * 
035     * This example will send some text messages on to a JMS Queue, consume them and 
036     * persist them to disk
037     *
038     * @version $Revision: 529902 $
039     * 
040     */
041    public class CamelJmsToFileExample {
042    
043        public static void main(String args[]) throws Exception{
044            // START SNIPPET: e1
045            CamelContext context=new DefaultCamelContext();
046            // END SNIPPET: e1
047            // Set up the ActiveMQ JMS Components
048            // START SNIPPET: e2
049            ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
050            // note we can explicity name the component
051            context.addComponent("test-jms",JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
052            // END SNIPPET: e2
053            // Add some configuration by hand ...
054            // START SNIPPET: e3
055            context.addRoutes(new RouteBuilder(){
056    
057                public void configure(){
058                    from("test-jms:queue:test.queue").to("file://test");
059                    // set up a listener on the file component
060                    from("file://test").process(new Processor(){
061    
062                        public void process(Exchange e){
063                            System.out.println("Received exchange: "+e.getIn());
064                        }
065                    });
066                }
067            });
068            // END SNIPPET: e3
069            // Camel template - a handy class for kicking off exchanges
070            // START SNIPPET: e4
071            CamelTemplate template =new CamelTemplate(context);
072            // END SNIPPET: e4
073            // Now everything is set up - lets start the context
074            context.start();
075            // now send some test text to a component - for this case a JMS Queue
076            // The text get converted to JMS messages - and sent to the Queue test.queue
077            // The file component is listening for messages from the Queue test.queue, consumes
078            // them and stores them to disk. The content of each file will be the test test we sent here.
079            // The listener on the file component gets notfied when new files are found ...
080            // that's it!
081            // START SNIPPET: e5
082            for(int i=0;i<10;i++){
083                template.sendBody("test-jms:queue:test.queue","Test Message: "+i);
084            }
085            // END SNIPPET: e5
086            Thread.sleep(1000);
087            context.stop();
088        }
089    }