2009-12-04 77 views
3

我想運行一個簡單的例子Apache Camel複製該文件從一個目錄複製到另一個:CamelContext.start()不阻止

CamelContext context = new DefaultCamelContext(); 
context.addRoutes(new RouteBuilder() { 

    public void configure() throws Exception { 
    from("file://c:/fromdir/").to("file://c:/todir/"); 
    } 
}); 
context.start(); 

如果我運行使用Apache的駱駝這個例子2.0.0退出程序在context.start();之後立即執行任何操作。如果在CamelContext開始之後添加Thread.sleep(30000);,後臺線程將執行其工作,並將文件從源複製到目標目錄30秒。

但是,如果我使用Apache Camel 1.6.2運行相同的代碼,start()方法會自動阻止,並且我不需要讓主線程進入睡眠狀態以便獲取文件複製。我沒有發現這種行爲從駝峯1.x變爲2.x的提示。這真的是預期的行爲嗎?是否有可能讓start()方法阻止Camel 2.0.0中的執行?在駱駝上下文

感謝

回答

7

呀調用start()應該永遠不會阻塞線程。而這正是駱駝2.0的行爲。

您可以使用org.apache.camel.util中的MainSupport類作爲阻塞的起點,直到您按下ctrl + c或在CamelContext上調用stop()。

參見主要用於擴展MainSupport並且能夠從Spring XML文件加載Camel的camel-spring。

+0

在Camel 2.2.0 start()中再次阻塞。你回到以前的行爲了嗎? – 2010-03-17 10:21:04

+0

你能否提供Main方法的一個小例子...我從最近2天開始嘗試這個方法。但無法構建一個運行示例:( – 2011-05-05 18:11:10

+1

請參閱:http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html – 2011-05-05 18:19:11

10

或者你可以context.start之後添加

Thread.currentThread().join(); 

();