2014-01-06 169 views
0

我是Apache Camel的新手。我已經將我的應用程序設置爲使用基於XML配置的Apache Camel。我的配置包含多個具有相似步驟的路線。我試圖找到一種方法來將這些不同路線的共同或重複部分放置在一個地方,並將它們從路線中引用而不是一次又一次地重複它們?如何寫簡潔的apache駱駝xml

例如,在我下面的駱駝路線配置中,路線2重複了路線1的幾個步驟。那麼是否有辦法提取路線1和路線2的常見步驟,然後參考路線1和2中提取的部分?

<context:property-placeholder location="classpath:quartz.properties" /> 

<context:component-scan base-package="com"></context:component-scan> 

    <camel:route> 
     <camel:from uri="quartz://deadlines/SDGWD?cron=15+34+14+?+*+MON-SUN+*" /> 
     <camel:onCompletion> 
      <camel:to uri="seda:checkAnyPendingDeadlines"/> 
     </camel:onCompletion> 
     <camel:to uri="bean:sdgwdNotifier" /> 
     <camel:choice> 
      <camel:when> 
       <camel:method ref="deadlineHandler" method="canProcessDeadline" /> 
       <camel:bean ref="deadlineHandler" method="prepareDeadline" /> 
       <camel:bean ref="sdgwdProcessor" method="initiateMessageProcessing" /> 
       <camel:bean ref="schedulerXdrTransformer" method="marshall" /> 
       <camel:to uri="wmq:SU.SES" /> 
       <camel:bean ref="sdgwdProcessor" method="waitForAcknowledgment" /> 
       <camel:bean ref="sdgwdProcessor" method="afterMessageProcessed" /> 
       <camel:bean ref="deadlineHandler" method="onDeadlineProcessingCompletion" /> 
      </camel:when> 
      <camel:otherwise> 
       <camel:bean ref="deadlineHandler" method="enqueDeadline" /> 
      </camel:otherwise> 
     </camel:choice> 
    </camel:route> 


    <camel:route> 
     <camel:from uri ="seda:checkAnyPendingDeadlines"/> 
     <camel:onCompletion> 
      <camel:to uri ="seda:checkAnyPendingDeadlines"/> 
     </camel:onCompletion> 
     <camel:to uri="bean:deadlineHandler?method=getNextProcessableDeadline" /> 
     <camel:choice> 
      <camel:when> 
       <camel:method ref="deadlineHandler" method="canProcessDeadline" /> 
       <camel:bean ref="deadlineHandler" method="prepareDeadline" /> 
       <camel:choice> 
        <camel:when> 
         <camel:simple>${body.deadline} == ${type:settlementcontrol.scheduler.model.Deadline.SDGW} </camel:simple> 
         <camel:bean ref="sdgwdProcessor" method="initiateMessageProcessing" /> 
         <camel:bean ref="schedulerXdrTransformer" method="marshall" /> 
         <camel:to uri="wmq:SU.SES" /> 
         <camel:bean ref="sdgwdProcessor" method="waitForAcknowledgment" /> 
         <camel:bean ref="sdgwdProcessor" method="afterMessageProcessed" /> 
         <camel:bean ref="deadlineHandler" method="onDeadlineProcessingCompletion" /> 
        </camel:when> 
       </camel:choice> 
      </camel:when> 
      <camel:otherwise> 
       <camel:bean ref="deadlineHandler" method="enqueDeadline" /> 
      </camel:otherwise> 
     </camel:choice> 
    </camel:route> 

感謝, Vaibhav的

回答

1

是常見的途徑,其中包含重複的流程的一部分,是否足夠?如果是這樣,那麼創建這樣的事情:

<camel:route id="myCommonPartOfFlow"> 
    <camel:from uri="direct-vm:common-in"/> 
    [common part] 
</camel:route> 

您可以從您的主要路線,如今調用你的子(myCommonPartOfFlow)路線:

<camel:to uri="direct-vm:common-in/> 
+0

謝謝@羅伯特。我已經使用直接組件提取出了通用部分。但是,我仍然希望避免重複的子路線上還有很少的步驟。 Apache駱駝文檔稱AOP已被棄用。那麼有沒有更好的方法來圍繞建議實現AOP? – Vaibhav

+0

你能解釋爲什麼這些步驟不能成爲普通流程的一部分(或舉例),讓我更好地把握這個問題? –

0

您可以使用一個direct組件來創建子路徑這一因素通用部分或拆分路由,以便公共路由擁有自己的路由,然後其他路由可以將消息發送到公共路由。例如,如果您有兩條路線執行以下過程「過程A」 - >「過程B」 - >「過程C」和「過程D」 - >「過程B」 - >「過程E」,則可以分離將B處理成它自己的路由並執行以下操作:

from(「jms:queue:processB」) - 「Process C」 - > end() 進程A - >設置Header「JMSReplyTo」,jms:queue: processC - >到(「jms:queue:processB」) 進程C - >設置Header「JMSReplyTo」,jms:queue:processE - >到(「jms:queue:processB」)

爲了簡潔我使用Java DSL,但使用XML DSL也可以做到這一點。我還使用「過程A」等來抽象可能是路線中的多個步驟。無論過程是什麼,連接路線的概念都是一樣的。這種方法的好處是它允許「流程B」的聯合來處理額外的負載並在您的基礎設施周圍分配處理。一旦你進入Asynchrounous編程,你的能力會提高很多。