Camel運行樣本

來源:互聯網
上載者:User

標籤:camel

這是一個很簡單的Camel例子,代碼如下:

public static void main(String[] args) throws Exception {//建立Camel上下文DefaultCamelContext camelContext = new DefaultCamelContext();//添加一個路由,參數為路由建造者camelContext.addRoutes(new RouteBuilder() {@Overridepublic void configure() throws Exception {this.from("file:H:/temp/in").process(new Processor() {@Overridepublic void process(Exchange exchange) throws Exception {GenericFile<File> gf = exchange.getIn().getBody(GenericFile.class);File file = gf.getFile();PrintStream ps = new PrintStream(System.out);BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));String line = null;while((line=br.readLine())!=null) {ps.println(line);}ps.close();br.close();}}).to("file:H:/temp/out");}});//啟動上下文camelContext.start();//防止主線程退出Object object = new Object();synchronized (object) {object.wait();}}

寫這個例子的原因不在於說通過這個例子的表面能學到多少東西,而是要通過這個例子挖掘出其背後的運行機制與原理。這裡配置的路由很簡單,一個起始節點,一個處理器,一個目的節點,其目的就是將一個目錄下的檔案搬運到另一個檔案夾,處理器只是將檔案(限於文字檔)的內容列印到控制台。

這個例子雖然簡單,但這也足以通過它來瞭解Camel的核心運行機制,且待下回分解。

Camel運行樣本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.