Apache Storm - Stateful bolt - 调度方法 运行

Apache Storm - Stateful bolt - scheduling method run

将状态完整的 apache storm bolt 插入到拓扑中,假设它看起来如下:

 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);
     }
 ...
 }

我需要触发一种方法来更新状态 (wordCounts),比方说每 X 秒一次,与是否接收到事件无关。这在 Apache Storm 有状态螺栓中可能吗?是否可以简单地安排这样的方法在定义的时间间隔内重复 运行?

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

我不确定我明白为什么您需要定期更新状态,但是如果您需要 运行 一些代码,那么 tick 元组可能适合您。 https://kitmenke.com/blog/2014/08/04/tick-tuples-within-storm/