如何每秒调用nextTuple()一定次数?

时间:2016-06-15 23:00:17

标签: apache-storm

有没有办法每秒调用spout的nextTuple方法一定次数?

1 个答案:

答案 0 :(得分:2)

你可以做到这一点,但计算当前分钟调用nextTuple()的方式,如果你发出了所需数量的元组,只计算return而没有发出元组。每次在新的一分钟内第一次调用nextTuple()时,您只需将计数器重置为零。

这样的事情:

private int counter = 0;
private int currentMinute = 0;
private final int tuplesPerMinute = 5;

public void nextTuple() {
    if(counter == tuplesPerMinute) {
        int newMinute = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis());
        if(newMinute <= currentMinute) {
            return;
        }

        counter = 0;
        currentMinute = newMinute;
    }

    ++counter;
    collector.emit(...);
}