2016-12-06 143 views
0

因此,现在我试图将路由从XML文件导入到Java DSL中。Apache Camel将XML路由和Bean从文件加载到CamelContext中

我一直试图从this link开始,但既然它是一个这样简单的例子,它并没有真正帮助我,也没有指向更复杂的例子。

我的问题是我的骆驼路线使用豆。 PropertiesComponentFileIdempotentRepository以及其他的豆在XML文件中定义,供XML文件中的路由使用。

我原来的Spring配置看起来像下面这样:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> 

<bean id="bean1" class="class1" /> 
<bean id="bean2" class="class2" /> 
<bean id="bean3" class="FileIdempotentRepository"> [...] </bean> 
<bean id="properties" class="PropertiesComponent"> [...] </bean> 

    <camelContext xmlns="http://camel.apache.org/schema/spring"> 
     <route> 
      <from uri="{{someplace}}&amp;filter=#bean1" /> 
      <setHeader headerName="FileRepoKey"> 
       <simple>${file:name}-${file:modified}</simple> 
      </setHeader> 
      <idempotentConsumer messageIdRepositoryRef="bean3"> 
       <header>FileRepoKey</header> 
       <process ref="bean2" /> 
       <to uri="{{otherplace}}"/> 
      </idempotentConsumer> 
     </route> 
    </camelContext> 
</beans> 

那么,如何转换这个烂摊子成由Java DSL从引入路由可用的东西吗?

我明白从我看这个链接,我需要做一些事情,如转换<camelContext><routes>。但留在豆给我一个错误沿线︰

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.springframework.org/schema/beans", local:"beans"). Expected elements are [...] 

我需要改变什么?或者,我是否可以在XML文件中没有bean,以便通过链接中使用的Java导入它?

+0

问题是您的原始文件是Spring XML上下文..不仅仅是您引用的链接中的XML简单路由。您正试图解决的要求和/或问题是什么?换句话说..你为什么要/需要从XML文件加载路由? –

+0

基本上,老板希望能够快速添加路由而不必编写正确的Java实现。留下新的路线在下一个版本中实施,而不是立即执行。 – Jsmith

+0

Gotcha ..所以我怀疑路线导入专长支持阅读将基本上重新实现Spring的整个bean b/c。如果你阅读页面上的注释..你会发现路由的复杂性是有限制的 - 没有共享异常处理程序,拦截器等等。豆类会落入那个 –

回答

0

我想我应该问过这个不同的方式,也许有人会想到这样。

它可能会给你所有的恶梦,我不知道。被警告。

如此以来,理念是“有东西可能从旁边的Java的XML文件中运行”下面的最终结果是约:

public static void main(String[] args) throws Exception { 

    Main main = new Main(); 

    //the XML file has a CamelContext in it. 
    main.setApplicationContextUri("myRoutes.xml"); 
    main.start();//instantiates the CamelContext so we can use it in Java 
    List<CamelContext> camelContexts = main.getCamelContexts(); //should only have 1 item in the list 
    CamelContext context = camelContexts.get(0); 

    //in order to add a component to the registry the following is needed for set up 
    // afterwards, should just be able to add anything to the registry with registry.put("name", object) 
    final SimpleRegistry registry = new SimpleRegistry(); 
    final CompositeRegistry compositeRegistry = new CompositeRegistry(); 
    compositeRegistry.addRegistry(context.getRegistry()); 
    compositeRegistry.addRegistry(registry); 
    ((DefaultCamelContext) context).setRegistry(compositeRegistry); 

    final FileIdempotentRepository myFileStore = new FileIdempotentRepository(); 
    File myFile = new File("idempotentRepoFiles/myFileStore.txt"); 

    final TimeStampFileFilter<?> myFileFilter = new TimeStampFileFilter<Object>(0L); 
    registry.put("myFileFilter", myFileFilter); 

    //512MB 
    myFileStore.setMaxFileStoreSize(536870912L); 
    myFileStore.setFileStore(myFile); 
    myFileStore.setCacheSize(100000); 

    //add a route to the CamelContext that was initially created in the XML file 
    context.addRoutes(new RouteBuilder() { 

     @Override 
     public void configure() throws Exception { 
      onException(myException.class) 
       .handled(true); 
      onException(GenericFileOperationFailedException.class) 
       .onException(SocketException.class) 
       .maximumRedeliveries(2) 
       .redeliveryDelay(5000L) 
       ; 
      Processor myProcessor = new myProcessor(); 
      from("{{myStart}}&filter=#myFileFilter") 
      .setHeader("myFileRepoKey", simple("${file:name}-${file:modified}")) 
      .idempotentConsumer(header("myFileRepoKey"), myFileStore) 
      .process(myProcessor) 
      .to("{{myEnd}}") 
      ; 

     } 

    }); 

    context.start(); 
    main.run(); 
} 

基本上是:在Spring的XML文件中创建一个CamelContext,初始化它,抓住它,修改它以包含用Java构建的路由。

0

Camel中的路由定义可以基于XML(Spring DSL或Blueprint DSL)或基于Java(Java DSL)。路线定义可以用两种语言同等表示。

在Spring应用程序中,您可以在文件中定义bean,并在导入的其他文件中定义路径。在外部文件中定义的路径可以引用在主文件中定义的bean。

弹簧的main.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> 

    <bean id="bean1" class="class1" /> 
    <bean id="bean2" class="class2" /> 
    <bean id="bean3" class="FileIdempotentRepository"> [...] </bean> 
    <bean id="properties" class="PropertiesComponent"> [...] </bean> 

    <import resource="camel-routes.xml"/> 
    <camelContext xmlns="http://camel.apache.org/schema/spring"> 
     <routeContextRef ref="ExternalRoutes"/> 
    </camelContext> 
</beans> 

骆驼routes.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> 

    <routeContext id="ExternalRoutes" xmlns="http://camel.apache.org/schema/spring"> 

     <route id="ARoute"> 
      <from uri="direct:startHere" /> 
      <to uri="bean:bean3" /> 
     </route> 
    </routeContext> 
</beans> 

您可以导入多个外部文件,当然。只需以不同的方式命名每个RouteContext即可。

如果修改RouteContext之一,则必须重新启动应用程序。如果您需要更加动态的应用程序,请尝试使用an OSGi container来运行您的骆驼路线,以便您可以轻松地对应用程序进行模块化并在运行时添加/删除功能。

相关问题