如何每秒调用nexttuple()特定次数?

b09cbbtk  于 2021-06-21  发布在  Storm
关注(0)|答案(1)|浏览(327)

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

dba5bblo

dba5bblo1#

你可以做到这一点,但要数多久 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(...);
}

相关问题