英文:
Duplication of Apache Camel messages
问题
如何根据某个头部条件创建一些Camel消息的副本?例如,我有一个带有头部key=value1, value2, value3
的消息,我想要有三个相同消息的副本,分别具有键key=value1
,key=value2
,key=value3
。
我尝试在这里https://camel.apache.org/components/3.20.x/找到一些组件,但没有找到合适的示例。
英文:
How can I create duplicates of some Camel messages by some header condition? For example, I have a message with header key=value1,value2,value3
, and I want to have three copies of the same messages with keys key=value1
, key=value2
, key=value3
.
I tried to find some components here https://camel.apache.org/components/3.20.x/, but haven't found any suitable example.
答案1
得分: 0
尝试使用multicast
示例代码:
from("direct:input")
.choice()
.when(header("myHeader").isEqualTo("duplicate"))
.to("direct:duplicate")
.end()
.to("someOtherEndpoint");
from("direct:duplicate")
.multicast()
.to("direct:output1")
.to("direct:output2")
.end();
英文:
Try with multicast
Example code:
from("direct:input")
.choice()
.when(header("myHeader").isEqualTo("duplicate"))
.to("direct:duplicate")
.end()
.to("someOtherEndpoint");
from("direct:duplicate")
.multicast()
.to("direct:output1")
.to("direct:output2")
.end();
答案2
得分: 0
from("input:queue")
.split().method(SplitClass.class, "splitHeaders")
.to("output:queue");
public class SplitClass{
public List<String> splitHeaders(@Header("key") String key) {
List<String> values = Arrays.asList(key.split(","));
List<String> messages = new ArrayList<>();
for (String value : values) {
String message = ExchangeHelper.convertToType(exchange, String.class, exchange.getIn());
exchange.getIn().setHeader("key", value);
messages.add(message);
}
return messages;
}
}
英文:
You are using the split component of camel, then it's working.
from("input:queue")
.split().method(SplitClass.class, "splitHeaders")
.to("output:queue");
public class SplitClass{
public List<String> splitHeaders(@Header("key") String key) {
List<String> values = Arrays.asList(key.split(","));
List<String> messages = new ArrayList<>();
for (String value : values) {
String message = ExchangeHelper.convertToType(exchange, String.class, exchange.getIn());
exchange.getIn().setHeader("key", value);
messages.add(message);
}
return messages;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论