代码之家  ›  专栏  ›  技术社区  ›  gshepherd7

Camel动态构建处理器bean调用

  •  3
  • gshepherd7  · 技术社区  · 8 年前

    下面是一个主题的bean设置示例。请记住,有多个bean。。。每种消息类型一个

    通过每种消息类型都有一个路由,我已经能够成功地接收和处理我的所有消息类型,但我想简化上下文文件,所以我只需要一个路由来接收我的所有主题。

    下面是我的路线设置的样子。。。

        <route id="Netty7001Route">
           <from uri="netty4:tcp://192.168.200.3:7001…"/>
           <to uri=”seda:ProcessRoute”/>
        </route>
    
        <route id=”ProcessRoute”/>
          <from uri=”seda:ProcessRoute”/>
               <setHeader headerName="LOGMESSAGE"><simple>A10::Entering endpoint for track message ${in.header.MSGNAME} via netty4</simple></setHeader>
               <to uri="log:messageLogger?level=ERROR"/>
               <setHeader headerName="LOGMESSAGE"><simple>A11::Leaving endpoint for track message ${in.header.MSGNAME}</simple></setHeader>
               <to uri="log:messageLogger?level=ERROR"/>
               <setHeader headerName="LOGMESSAGE"><constant>A14::Entering Mediation</constant></setHeader>
               <to uri="log:messageLogger?level=ERROR"/>
               **<process ref="DDS_C2_NewSystemReferencePointMediationBean"/>**
           <setHeader headerName="MSGNAME"><constant>C2_TM_NewSystemReferencePoint</constant></setHeader>
           <setHeader headerName="MSGTYPE"><constant>C2_NewSystemReferencePoint</constant></setHeader>
               <setHeader headerName="LOGMESSAGE"><constant>A15::Leaving Mediation</constant></setHeader>
               <to uri="log:messageLogger?level=ERROR"/>
               <setHeader headerName="LOGMESSAGE"><constant>A17::Sending message to DDS domain 4</constant></setHeader>   
               <to uri="log:messageLogger?level=ERROR"/>
           <to uri="dds://4/C2_TM_NewSystemReferencePoint?typeName=C2_NewSystemReferencePoint"/>
        </route>
    

    因此,上面的process ref=line是我想要动态的,因为在我查看标头中的MSGNAME之前,我不知道要调用哪个bean处理器。我尝试使用recipientList,但失败了,因为它们不是端点,而是处理器bean,我尝试只使用ref:DDS\u C2{$in.header.MSGNAME}MediationBean,但camel不会启动并抱怨这里的简单标记。在camel-spring配置中有什么方法可以做到这一点吗?

    我曾想过只编写一个处理器,在java代码中调用适当的bean处理器,但我不确定这是否是做我需要的事情的正确方法,以及它是否比使用标记更有效。

    谢谢你的帮助。

    1 回复  |  直到 8 年前
        1
  •  0
  •   mgyongyosi    8 年前

    最好的方法是使用ProcessorEndpoint创建一个新组件,如前所述 here .

    工作示例(带处理器)

    1. package com.mgyongyosi.sample.component;
      
      // imports
      
      public class SpecifiedProcessor extends DefaultComponent {
      
      @Override
      protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
          Object registryObj = getCamelContext().getRegistry().lookupByName(remaining);
      
          if(!(registryObj instanceof Processor))
              throw new IllegalArgumentException("The Processor with the specified name was not found in the Registry.");
      
          return new ProcessorEndpoint(uri, this, (Processor)registryObj);
      }
      
    2. 通过创建名为 specified-processor META-INF/services/org/apache/camel/component 包含以下内容的目录( full documentation ):

      class=com.mgyongyosi.sample.component.SpecifiedProcessor
      
    3. Java DSL的使用示例:

      from("timer:helloworld?period=5000")
              .setHeader("MSGNAME", constant("NewSystemReferencePoint"))
              .toD("specified-processor://DDS_C2_${in.header.MSGNAME}MediationBean")
              .log("${body}");
      

    在XML中:

    <toD uri="specified-processor://DDS_C2_${in.header.MSGNAME}MediationBean"/>
    

    如果是豆类

    Java DSL: .toD("bean:DDS_C2_${in.header.MSGNAME}MediationBean")

    <toD uri="bean:DDS_C2_${in.header.MSGNAME}MediationBean"/>