JAVA學習筆記_camel

Apache Camel是Apache基金會下的一個開源項目,它是一個基於規則路由和處理的引擎,提供企業集成模式的Java對象的實現,通過應用程序接口 或稱爲陳述式的Java領域特定語言(DSL)來配置路由和處理的規則。其核心的思想就是從一個from源頭得到數據,通過processor處理,再發到一個to目的的.
這個from和to可以是我們在項目集成中經常碰到的類型:一個FTP文件夾中的文件,一個MQ的queue,一個HTTP request/response,一個webservice等等.
Camel可以很容易集成到standalone的應用,在容器中運行的Web應用,以及和Spring一起集成.
下面用一個示例,介紹怎麼開發一個最簡單的Camel應用.
1,從http://camel.apache.org/download.html下載Jar包.在本文寫作的時候最新版本是2.9. 本文用的是2.7,從2.7開始要求需要JRE1.6的環境.
下載的zip包含了Camel各種特性要用到的jar包.
在本文入門示例用到的Jar包只需要:camel-core-2.7.5.jar,commons-management-1.0.jar,slf4j-api-1.6.1.jar.
2,新建一個Eclipse工程,將上面列出的jar包設定到工程的Classpath.
新建一個如下的類:運行後完成的工作是將d:/temp/inbox/下的所有文件移到d:/temp/outbox
[java] view plaincopy
  1. public class FileMoveWithCamel {  
  2.     public static void main(String args[]) throws Exception {  
  3.         CamelContext context = new DefaultCamelContext();  
  4.         context.addRoutes(new RouteBuilder() {  
  5.         public void configure() {  
  6.         //from("file:d:/temp/inbox?noop=true").to("file:d:/temp/outbox");   
  7.         from("file:d:/temp/inbox/?delay=30000").to("file:d:/temp/outbox");  
  8.         }  
  9.         });  
  10.         context.start();  
  11.         boolean loop =true;  
  12.         while(loop){  
  13.             Thread.sleep(25000);  
  14.         }          
  15.         context.stop();  
  16.         }  
  17. }  
上面的例子體現了一個最簡單的路由功能,比如d:/temp/inbox/是某一個系統FTP到Camel所在的系統的一個接收目錄.
d:/temp/outbox爲Camel要發送的另一個系統的接收目錄.
from/to可以是如下別的形式,讀者是否可以看出Camel是可以用於系統集成中做路由,流程控制一個非常好的框架了呢?
from("file:d:/temp/inbox/?delay=30000").to("jms:queue:order");//delay=30000是每隔30秒輪詢一次文件夾中是否有文件.
3,再給出一個從from到to有中間流程process處理的例子:
[java] view plaincopy
  1. public class FileProcessWithCamel {  
  2.     public static void main(String args[]) throws Exception {  
  3.         CamelContext context = new DefaultCamelContext();      
  4.         context.addRoutes(new RouteBuilder() {  
  5.               
  6.         public void configure() {  
  7.         FileConvertProcessor processor = new FileConvertProcessor();  
  8.         from("file:d:/temp/inbox?noop=true").process(processor).to("file:d:/temp/outbox");  
  9.         }  
  10.         });  
  11.           
  12.         context.start();  
  13.         boolean loop =true;  
  14.         while(loop){  
  15.             Thread.sleep(25000);  
  16.         }  
  17.         context.stop();  
  18.         }  
  19. }  
這裏的處理只是簡單的把接收到的文件多行轉成一行
[java] view plaincopy
  1. public class FileConvertProcessor implements Processor{  
  2.     @Override  
  3.     public void process(Exchange exchange) throws Exception {      
  4.         try {  
  5.             InputStream body = exchange.getIn().getBody(InputStream.class);  
  6.             BufferedReader in = new BufferedReader(new InputStreamReader(body));  
  7.             StringBuffer strbf = new StringBuffer("");  
  8.             String str = null;  
  9.             str = in.readLine();  
  10.             while (str != null) {                  
  11.                 System.out.println(str);  
  12.                 strbf.append(str + " ");  
  13.                 str = in.readLine();                  
  14.             }  
  15.             exchange.getOut().setHeader(Exchange.FILE_NAME, "converted.txt");  
  16.             // set the output to the file  
  17.             exchange.getOut().setBody(strbf.toString());  
  18.         } catch (IOException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22. }  
在Eclipse裏運行的時候,Camel默認不會把log信息打印到控制檯,這樣出錯的話,異常是看不到的,需要把log4j配置到項目中.
[java] view plaincopy
  1. log4j.appender.stdout = org.apache.log4j.ConsoleAppender  
  2. log4j.appender.stdout.Target = System.out  
  3. log4j.appender.stdout.layout = org.apache.log4j.PatternLayout  
  4. log4j.appender.stdout.layout.ConversionPattern = %-5p %d [%t] %c: %m%n  
  5. log4j.rootLogger = debug,stdout  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章