Nikita Gorshkov
Nikita Gorshkov

Reputation: 1697

How can I call nextTuple() certain number of times per second?

Is there any way to call nextTuple method of a spout certain number of times per second?

Upvotes: 0

Views: 63

Answers (1)

Matthias J. Sax
Matthias J. Sax

Reputation: 62350

You can accomplish this, but counting how ofter nextTuple() was called in the current minute, and just return without emitting a tuple if your desired number of tuples got emitted. Each time, nextTuple() is called for the first time in a new minute, you just reset the counter to zero.

Something like this:

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

Upvotes: 2

Related Questions