יום רביעי, 12 ביוני 2013

Sending a message from java code to Camel Route

The following simple code demonstrates sending a Message to Camel Route from java code 

 
  1: package org.apache.camel.example.console;
  2: import java.util.ArrayList;
  3: import java.util.List;
  4: import java.util.Map;
  5: 
  6: import org.apache.camel.CamelContext;
  7: import org.apache.camel.Exchange;
  8: import org.apache.camel.Predicate;
  9: import org.apache.camel.ProducerTemplate;
 10: import org.apache.camel.builder.RouteBuilder;
 11: import org.apache.camel.impl.DefaultCamelContext;
 12: import org.apache.camel.model.dataformat.BindyType;
 13: import org.apache.camel.model.language.ELExpression;
 14: 
 15: public class TestCodeForBlog {
 16:   
 17:   public static RouteBuilder createRoute() {
 18:     return new RouteBuilder() {
 19:       public void configure() throws Exception {
 20:       from("direct:fromCode").
 21:       setHeader(Exchange.FILE_NAME, constant("test.txt"))
 22:       .to("file:target/TestmyFiles").
 23:         to("stream:out");
 24:       }
 25:     };
 26:     }
 27:   
 28:   public static void main(String[] args) {
 29:     try {
 30:       CamelContext context = new DefaultCamelContext();  
 31:       context.addRoutes(createRoute());
 32:       context.start();
 33:       
 34:       Thread.sleep(1000);
 35:       
 36:       ProducerTemplate template = context.createProducerTemplate();
 37:       
 38:       template.sendBody("direct:fromCode", "Hello zvika");
 39:       
 40:       Thread.sleep(1000);
 41:        
 42:        System.out.print("I'm out");
 43:        
 44:     } catch (Exception e) {
 45:       
 46:       e.printStackTrace();
 47:     }
 48:   }
 49: }

The test message converted automaticly into stream and send to the file and to the output console .
The producerTamplate class allows to get access into direct endpoint of the Camel Route

אין תגובות:

הוסף רשומת תגובה