2017-07-27 98 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