2017-07-27 21 views
0

我剛開始在我的一個項目中使用Camel。我試圖用Spring配置Camel,但遇到問題。
我不想使用xml配置,而是使用基於Spring的Annotations來配置路由和處理器。CamelContext沒有采用Spring註釋的路線

我的應用程序是一個獨立的Spring應用程序,它將作爲Jar運行。 爲了保持應用程序的運行,我有一個空的預定方法,每運行x分鐘。

下面是我build.gralde

// Spring // 
compile('org.springframework:spring-core:5.0.0.RC2') 
compile('org.springframework:spring-context:5.0.0.RC2') 
compile('org.springframework:spring-beans:5.0.0.RC2') 
compile('org.springframework:spring-context-support:5.0.0.RC2') 
// Apache // 
// Camel // 
compile('org.apache.camel:camel-core:2.19.1') 
compile('org.apache.camel:camel-spring:2.19.1') 

快照beans.xml

<context:annotation-config/> 
<tx:annotation-driven/> 
<context:component-scan base-package="my.package" /> 

<camelContext id="aggregatorCamelContext" autoStartup="true" xmlns="http://camel.apache.org/schema/spring"> 
     <package> 
      my.package.camel 
     </package>   
</camelContext> 

樣品的依賴RouteBuilder

@Component 
public class SampleRoute extends RouteBuilder { 

    @Autowired 
    MyClass myObject; 

    @Override 
    public void configure() throws Exception { 

     from("file:filelist") 
      .process(myObject) 
      .to("file:processedList"); 
    } 

} 

爲了保持應用程序活着(我知道有點哈克,但就足夠了現在)

@Component 
@EnableScheduling 
public class KeepitAlive { 

    @Scheduled(fixedRate = 1000l) 
    public void run(){ 
     System.out.println("KeepitAlive.run "+ Thread.currentThread().getName()); 
    } 
} 

Main Class。我曾嘗試這兩種方法,初始化Spring上下文以及駱駝爲主,但沒有運氣

public class MyApplication { 

    public static void main(String[] args) throws Exception { 
     /*AbstractXmlApplicationContext context = 
       new ClassPathXmlApplicationContext("path/to/beans.xml");*/ 

     Main main = new Main(); 
     main.setApplicationContextUri("path/to/beans.xml"); 
     main.start(); 
    } 

} 

如果我把我的路線camelContext聲明本身,它的作品精美絕倫,

<route> 
     <from uri="file:filelist"/> 
     <to uri="file:processedlist"/> 
    </route> 

我我們也研究過駱駝春季整合documentation,但它也包含基於xml的配置。
任何人都可以請指導我正確的方向。

+1

使用'run'方法在'Main'類如果從Apache的駱駝。看到這個FAQ:http://camel.apache.org/running-camel-standalone-and-have-it-keep-running.html –

回答

0

最終得出結論。需要擴展SpringRouteBuilder而不是RouteBuiler以上的SampleRoute類。
有人在爭論的問題,我建議一旦通過Camel in Action書。
不知何故,我在開始時就錯過了這本書,這讓我花費了大量的時間來計算出本書所涵蓋的瑣碎事情。通過

<package> my.package.camel </package>

0

您使用駱駝自己的包掃描,如果你想駱駝找到春天@Component駱駝路由您應該使用<contextScan>

見駱駝春天文檔瞭解更多:http://camel.apache.org/spring.html

+0

我也試過,但駱駝不能自動發現。雖然你在評論中提到過,但我沒有在'Main'上嘗試'run'方法。它似乎可以用'main.run()'方法,而不是'RouteBuilder'的'main.start()'。謝謝你的幫助。 – Bond

+0

駱駝並沒有正式支持Spring 5.0.x.我們甚至沒有嘗試在該版本上進行升級和測試。這將在後來的Camel發行版中得到正式支持,當時Spring 5是GA,並且我們正在升級。 –

+0

明白了。我懷疑這一點。將看看我是否可以降低版本。 – Bond