I needed to have same <key,value> message to be given to two different branch but reading from single stream. Both branches should get all messages (i.e. my predicate for both branches is same.
I tried below code but I see only first branch is getting message and second one is not getting.
KStream<String, byte[]>[] branches = builder.<String, byte[]>stream("source-topic)
.branch((key, val) -> true, // this goes to output topic1
(key, val) -> true); // this goes to output topic2
// branch[0] map on logic1Ondata and send to topic1
branches[0].map(logic1OnData).filter(
(key, value) -> {
if (key == null || value == null)
return false;
return value.data() != null;
}).to("topic1", Produced.with(Serdes.String(), Serdes.String())
// branch[1] map on logic2Ondata and send to topic2
branches[1].map(logic2OnData).filter(
(key, value) -> {
if (key == null || value == null)
return false;
return value.data() != null;
}).to("topic2", Produced.with(Serdes.String(), Serdes.String())
The expectation is both branches should get the same message from the "source-topic". Each branch individually validate on logic1OnData and logic2OnData and write to topic1 and topic2 But i see only branch[0] receiving messages and not messages to branches[1]
If I branching is not an option, I would like to know is there any alternate way.
EDIT: Branch is kind of if else. due to this if first predicate matches, it will not consider rest. So due to this only topic1 gets message for above logic.
Instead with below way, I can get the message is given to multiple paths.
KStream<String, byte[]>[] stream = builder.<String, byte[]>stream("source-topic);
stream.map(logic1OnData).filter(
(key, value) -> {
if (key == null || value == null)
return false;
return value.data() != null;
}).to("topic1", Produced.with(Serdes.String(), Serdes.String())
stream.map(logic2OnData).filter(
(key, value) -> {
if (key == null || value == null)
return false;
return value.data() != null;
}).to("topic2", Produced.with(Serdes.String(), Serdes.String())
This change gives every message to both maps. Further filtered and written to respective topics.