1

I need to emit an event for the first time the aggregated value reaches 100, this is what it looks right now

StreamsBuilder sb = new StreamsBuilder();
KTable<Integer, Integer> example =
    sb.stream(inputs, Consumed.with(Serdes.Integer(), Serdes.Integer()))
    .groupByKey()
    .aggregate(
        () -> 0,
        (key, value, currentValue) -> {
          var newValue = currentValue + value;
          if (currentValue != 100 && newValue == 100) {
            PublishEvent(key, "REACH 100!");
          }
          return newValue;
        },
        Materialized.with(Serdes.Integer(), Serdes.Integer())
    );

Is it good practice to do PublishEvent() in the aggregate function because it may create a side effect during the aggregation? if it is, I am not sure how to write this PublishEvent() function? if not, how could I emit for the first time the aggregated value reaches 100?

niukasu
  • 267
  • 2
  • 4
  • 8
  • Do you need to publish an event each time the aggregate reaches 100 (which means your aggregate can increase/decrease), only the first time or your aggregate is only increasing ? – fhussonnois Nov 17 '20 at 08:56
  • @fhussonnois it is only increasing – niukasu Nov 17 '20 at 22:10

0 Answers0