Search code examples
apache-storm

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?


Solution

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