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.jmstofile; 018 019 import javax.jms.ConnectionFactory; 020 021 import org.apache.activemq.ActiveMQConnectionFactory; 022 import org.apache.camel.CamelContext; 023 import org.apache.camel.CamelTemplate; 024 import org.apache.camel.Exchange; 025 import org.apache.camel.Processor; 026 import org.apache.camel.builder.RouteBuilder; 027 import org.apache.camel.component.jms.JmsComponent; 028 import org.apache.camel.impl.DefaultCamelContext; 029 030 /** 031 * An example class for demonstrating some of the basics behind camel This 032 * example will send some text messages on to a JMS Queue, consume them and 033 * persist them to disk 034 * 035 * @version $Revision: 529902 $ 036 */ 037 public final class CamelJmsToFileExample { 038 039 private CamelJmsToFileExample() { 040 } 041 042 public static void main(String args[]) throws Exception { 043 // START SNIPPET: e1 044 CamelContext context = new DefaultCamelContext(); 045 // END SNIPPET: e1 046 // Set up the ActiveMQ JMS Components 047 // START SNIPPET: e2 048 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); 049 // note we can explicity name the component 050 context.addComponent("test-jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); 051 // END SNIPPET: e2 052 // Add some configuration by hand ... 053 // START SNIPPET: e3 054 context.addRoutes(new RouteBuilder() { 055 056 public void configure() { 057 from("test-jms:queue:test.queue").to("file://test"); 058 // set up a listener on the file component 059 from("file://test").process(new Processor() { 060 061 public void process(Exchange e) { 062 System.out.println("Received exchange: " + e.getIn()); 063 } 064 }); 065 } 066 }); 067 // END SNIPPET: e3 068 // Camel template - a handy class for kicking off exchanges 069 // START SNIPPET: e4 070 CamelTemplate template = new CamelTemplate(context); 071 // END SNIPPET: e4 072 // Now everything is set up - lets start the context 073 context.start(); 074 // now send some test text to a component - for this case a JMS Queue 075 // The text get converted to JMS messages - and sent to the Queue 076 // test.queue 077 // The file component is listening for messages from the Queue 078 // test.queue, consumes 079 // them and stores them to disk. The content of each file will be the 080 // test test we sent here. 081 // The listener on the file component gets notfied when new files are 082 // found ... 083 // that's it! 084 // START SNIPPET: e5 085 for (int i = 0; i < 10; i++) { 086 template.sendBody("test-jms:queue:test.queue", "Test Message: " + i); 087 } 088 // END SNIPPET: e5 089 Thread.sleep(1000); 090 context.stop(); 091 } 092 }