Apache Camel消息的重复

huangapple go评论64阅读模式
英文:

Duplication of Apache Camel messages

问题

如何根据某个头部条件创建一些Camel消息的副本?例如,我有一个带有头部key=value1, value2, value3的消息,我想要有三个相同消息的副本,分别具有键key=value1key=value2key=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(&quot;input:queue&quot;)
    .split().method(SplitClass.class, &quot;splitHeaders&quot;)
        .to(&quot;output:queue&quot;);

public class SplitClass{
    public List&lt;String&gt; splitHeaders(@Header(&quot;key&quot;) String key) {
        List&lt;String&gt; values = Arrays.asList(key.split(&quot;,&quot;));
        List&lt;String&gt; messages = new ArrayList&lt;&gt;();
        for (String value : values) {
            String message = ExchangeHelper.convertToType(exchange, String.class, exchange.getIn());
            exchange.getIn().setHeader(&quot;key&quot;, value);
            messages.add(message);
        }
        return messages;
    }
}

huangapple
  • 本文由 发表于 2023年2月19日 20:36:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定