骆驼动态构建处理器bean调用
Camel dynamically build processor bean call
所以这是我针对一个主题的 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= 行是我想要动态的,因为在查看 header 中的 MSGNAME 之前我不知道要调用哪个 bean 处理器。我试过使用 recipientList 但失败了,因为它们不是端点而是处理器 bean,我试过只使用 ref:DDS_C2_{$in.header.MSGNAME}MediationBean,但骆驼不会启动并抱怨简单在这里标记。有没有办法在骆驼 spring 配置中做到这一点?
我尝试了一种解决方法,方法是使用检查 MSGNAME 的标记,然后调用相应的处理器 bean,但我需要为每种消息类型设置条件。这可行,但与每个主题都有一条路线相比效率极低。
我考虑过只编写一个处理器,它会在 java 代码中调用适当的 bean 处理器,但我不确定这是否是完成我需要的事情的正确方法以及它是否会比使用标签更有效率。
感谢您的帮助。
最好的方法可能是创建一个具有 ProcessorEndpoint 的新组件,如 here 所述。
工作示例(带处理器)
创建自定义组件:
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);
}
通过在 META-INF/services/org/apache/camel/component
目录中创建一个名为 specified-processor
的文件(这将是组件的 uri 方案)来注册您的组件,其中包含以下内容(full documentation ):
class=com.mgyongyosi.sample.component.SpecifiedProcessor
来自 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"/>
如果是 Beans
Java DSL:.toD("bean:DDS_C2_${in.header.MSGNAME}MediationBean")
XML: <toD uri="bean:DDS_C2_${in.header.MSGNAME}MediationBean"/>
所以这是我针对一个主题的 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= 行是我想要动态的,因为在查看 header 中的 MSGNAME 之前我不知道要调用哪个 bean 处理器。我试过使用 recipientList 但失败了,因为它们不是端点而是处理器 bean,我试过只使用 ref:DDS_C2_{$in.header.MSGNAME}MediationBean,但骆驼不会启动并抱怨简单在这里标记。有没有办法在骆驼 spring 配置中做到这一点?
我尝试了一种解决方法,方法是使用检查 MSGNAME 的标记,然后调用相应的处理器 bean,但我需要为每种消息类型设置条件。这可行,但与每个主题都有一条路线相比效率极低。
我考虑过只编写一个处理器,它会在 java 代码中调用适当的 bean 处理器,但我不确定这是否是完成我需要的事情的正确方法以及它是否会比使用标签更有效率。
感谢您的帮助。
最好的方法可能是创建一个具有 ProcessorEndpoint 的新组件,如 here 所述。
工作示例(带处理器)
创建自定义组件:
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); }
通过在
META-INF/services/org/apache/camel/component
目录中创建一个名为specified-processor
的文件(这将是组件的 uri 方案)来注册您的组件,其中包含以下内容(full documentation ):class=com.mgyongyosi.sample.component.SpecifiedProcessor
来自 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"/>
如果是 Beans
Java DSL:.toD("bean:DDS_C2_${in.header.MSGNAME}MediationBean")
XML: <toD uri="bean:DDS_C2_${in.header.MSGNAME}MediationBean"/>