簡體   English   中英

除了長URI之外,還可以用Spring bean配置駱駝端點嗎?

[英]Alternative to long URI to configure a Camel Endpoint with Spring beans?

我試圖找到一種使用駱駝上下文中的路由中的終結點聲明引用的spring bean配置駱駝終結點的方法,但是它不起作用。

例如,有時用許多參數定義端點URI太可怕了(!!),用bean及其屬性配置端點會容易得多。 (甚至更好的是,當使用XML配置端點時,or元素應該具有像常規Bean這樣的子元素,我們可以在其中配置端點的參數)。

下面的第一種方法很好用,非常標准且非常簡單。 第二種方法是我想使用的方法,但是它不起作用。 我嘗試了很多變化,但沒有成功! 實際上,下面的第三種選擇對於Camel開發人員來說只是一個有趣的建議,但這也說明了我的觀點。

在下面的示例中,我僅為文件端點配置了3個參數,但是想象一下URI包含10個參數! 我的問題是如何使第二種方法正常工作,我確定有一個簡單的解決方案!? 我也嘗試過使用factory-bean和factory方法,但是它也不起作用。

1)在XML(Spring bean)中配置駱駝端點的標准方法:

...
<camel:camelContext id="camelContext"  >

    <camel:route id="deviceDataLogsPoller"  >
        <camel:from uri="file://long/path/to/input?preMove=../inprogress&amp;move=../done&amp;moveFailed=../error" />

        <camel:log message="Input device data file read from file in input folder {{im.filePoller.folder.input}}." loggingLevel="INFO" />

    </camel:route>
</camel:camelContext>

2)我期望有效的替代方法,但這對我不起作用(對我來說!):

<bean id="filePoller" class="org.apache.camel.component.file.FileEndpoint" >

    <property name="camelContext" ref="camelContext" />
    <property name="localWorkDirectory" value="/long/path/to/input" />
    <property name="preMove"   value="../inprogress" />
    <property name="move"       value="../done" />
    <property name="moveFailed" value="../error" />
    ...
</bean>

...
<camel:camelContext id="camelContext"  >

    <camel:route id="deviceDataLogsPoller"  >
        <camel:from ref="filePoller" />

        <camel:log message="Input device data file read from file in input folder {{im.filePoller.folder.input}}." loggingLevel="INFO" />

    </camel:route>
</camel:camelContext>

3)將來會很有趣的替代方案(混合在上述兩個替代方案之間):

...

    <camel:route id="deviceDataLogsPoller"  >
        <camel:from uri="file://long/path/to/input" >
             <property name="preMove"   value="../inprogress" />
             <property name="move"       value="../done" />
             <property name="moveFailed" value="../error" />
             ...
        </camel:from>

        <camel:log message="Input device data file read from file in input folder {{im.filePoller.folder.input}}." loggingLevel="INFO" />

    </camel:route>
</camel:camelContext>

到底什么對您不起作用?

安裝完成后,按預期完成了工作:

<bean id="filePoller" class="org.apache.camel.component.file.FileEndpoint">
    <property name="file" value="src/data/bean-ref" />
    <property name="move" ref="moveExpression"/>
</bean>

<bean id="moveExpression" class="org.apache.camel.model.language.SimpleExpression">
    <constructor-arg value="${file:parent}/.done/${file:onlyname}" />
</bean>

<camelContext xmlns="http://camel.apache.org/schema/spring" id="camelContext">
    <route>
        <from ref="filePoller" />
        <log message="${body}" />
    </route>
</camelContext> 

注意:

  • 該屬性file是強制性的
  • 特性movemoveFailed ,和preMove類型不是java.lang.String但類型的org.apache.camel.Expression和必須相應地初始化。
  • 屬性moveExpression需要完整的文件表達式。 如果僅使用.done而不是${file:parent}/.done/${file:onlyname}則文件將重命名為.done而不會移動到名為.done的目錄中。

正如我在上一條評論中所述,我能夠使端點的Bean配置正常工作(請參見上面的評論),但是與畢竟只使用URI相比,這種方法最終變得更加復雜和繁重!!

像我在上面的第3個替代方案中所建議的那樣,擁有一種配置端點的方法會更有趣。 也許如果我有時間,我將嘗試通過params元素構造完整的URI來創建自己的和標簽,以包裝現有標簽和標簽...! 我也可以將此建議給駱駝開發人員。

請參見下面的示例,該示例說明將來配置端點(或使用我想編碼的XML包裝器)可能會很有趣:

<camel:route id="deviceDataLogsPoller">

    <camel:from uri="file://long/path/to/input" >

         <param name="preMove"   value="../inprogress" />
         <param name="move"       value="../done" />
         <param name="moveFailed" value="../error" />
         ...
    </camel:from>

    ...

</camel:route>

不幸的是,目前無法實現上面所示的端點配置,但是我認為這是一個很好的選擇! 目前,唯一的方法是要么將所有參數都指定為一個很長的URI中的params,要么將端點配置為一個普通的Bean,並隱含所有復雜性(有關詳細信息,請參見上面的評論)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM