| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| CamelJmsToFileExample |
|
| 1.3333333333333333;1.333 |
| 1 | /** |
|
| 2 | * |
|
| 3 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
| 4 | * contributor license agreements. See the NOTICE file distributed with |
|
| 5 | * this work for additional information regarding copyright ownership. |
|
| 6 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
| 7 | * (the "License"); you may not use this file except in compliance with |
|
| 8 | * the License. You may obtain a copy of the License at |
|
| 9 | * |
|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 11 | * |
|
| 12 | * Unless required by applicable law or agreed to in writing, software |
|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 15 | * See the License for the specific language governing permissions and |
|
| 16 | * limitations under the License. |
|
| 17 | */ |
|
| 18 | ||
| 19 | package org.apache.camel.example.jmstofile; |
|
| 20 | ||
| 21 | import org.apache.camel.component.jms.JmsComponent; |
|
| 22 | import javax.jms.ConnectionFactory; |
|
| 23 | import org.apache.activemq.ActiveMQConnectionFactory; |
|
| 24 | import org.apache.camel.CamelTemplate; |
|
| 25 | import org.apache.camel.CamelContext; |
|
| 26 | import org.apache.camel.Exchange; |
|
| 27 | import org.apache.camel.Processor; |
|
| 28 | import org.apache.camel.builder.RouteBuilder; |
|
| 29 | import org.apache.camel.impl.DefaultCamelContext; |
|
| 30 | ||
| 31 | ||
| 32 | /** |
|
| 33 | * An example class for demonstrating some of the basics behind camel |
|
| 34 | * |
|
| 35 | * This example will send some text messages on to a JMS Queue, consume them and |
|
| 36 | * persist them to disk |
|
| 37 | * |
|
| 38 | * @version $Revision: 529902 $ |
|
| 39 | * |
|
| 40 | */ |
|
| 41 | 0 | public class CamelJmsToFileExample { |
| 42 | ||
| 43 | public static void main(String args[]) throws Exception{ |
|
| 44 | // START SNIPPET: e1 |
|
| 45 | 0 | CamelContext context=new DefaultCamelContext(); |
| 46 | // END SNIPPET: e1 |
|
| 47 | // Set up the ActiveMQ JMS Components |
|
| 48 | // START SNIPPET: e2 |
|
| 49 | 0 | ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); |
| 50 | // note we can explicity name the component |
|
| 51 | 0 | context.addComponent("test-jms",JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); |
| 52 | // END SNIPPET: e2 |
|
| 53 | // Add some configuration by hand ... |
|
| 54 | // START SNIPPET: e3 |
|
| 55 | 0 | context.addRoutes(new RouteBuilder(){ |
| 56 | ||
| 57 | 0 | public void configure(){ |
| 58 | 0 | from("test-jms:queue:test.queue").to("file://test"); |
| 59 | // set up a listener on the file component |
|
| 60 | 0 | from("file://test").process(new Processor(){ |
| 61 | ||
| 62 | 0 | public void process(Exchange e){ |
| 63 | 0 | System.out.println("Received exchange: "+e.getIn()); |
| 64 | 0 | } |
| 65 | }); |
|
| 66 | 0 | } |
| 67 | }); |
|
| 68 | // END SNIPPET: e3 |
|
| 69 | // Camel template - a handy class for kicking off exchanges |
|
| 70 | // START SNIPPET: e4 |
|
| 71 | 0 | CamelTemplate template =new CamelTemplate(context); |
| 72 | // END SNIPPET: e4 |
|
| 73 | // Now everything is set up - lets start the context |
|
| 74 | 0 | context.start(); |
| 75 | // now send some test text to a component - for this case a JMS Queue |
|
| 76 | // The text get converted to JMS messages - and sent to the Queue test.queue |
|
| 77 | // The file component is listening for messages from the Queue test.queue, consumes |
|
| 78 | // them and stores them to disk. The content of each file will be the test test we sent here. |
|
| 79 | // The listener on the file component gets notfied when new files are found ... |
|
| 80 | // that's it! |
|
| 81 | // START SNIPPET: e5 |
|
| 82 | 0 | for(int i=0;i<10;i++){ |
| 83 | 0 | template.sendBody("test-jms:queue:test.queue","Test Message: "+i); |
| 84 | } |
|
| 85 | // END SNIPPET: e5 |
|
| 86 | 0 | Thread.sleep(1000); |
| 87 | 0 | context.stop(); |
| 88 | 0 | } |
| 89 | } |