Search code examples
methodsapache-stormscheduling

Apache Storm - Stateful bolt - scheduling method run


Having a state-full apache storm bolt plugged into the topology, let's say it looks as follows:

 public class WordCountBolt extends BaseStatefulBolt<KeyValueState<String, Long>> {
     private KeyValueState<String, Long> wordCounts;
     private OutputCollector collector;
 ...
     @Override
     public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
       this.collector = collector;
     }
     @Override
     public void initState(KeyValueState<String, Long> state) {
       wordCounts = state;
     }
     @Override
     public void execute(Tuple tuple) {
       String word = tuple.getString(0);
       Integer count = wordCounts.get(word, 0);
       count++;
       wordCounts.put(word, count);
       collector.emit(tuple, new Values(word, count));
       collector.ack(tuple);
     }
 ...
 }

I would need to trigger a method to update the state (wordCounts), let's say each X seconds, independently of receiving an event or not. Is this possible in Apache Storm statefull bolts? Is it possible to simply schedule a method like this to be run in a defined interval repetitively?

public void updateState() {
      wordCounts.put("NewKey", 1);
}

Solution

  • I'm not sure I follow why you need to update the state periodically, but if you need to run some code every so often, tick tuples might work for you. https://kitmenke.com/blog/2014/08/04/tick-tuples-within-storm/