Class ThroughputControl

java.lang.Object
org.jpos.util.ThroughputControl

public class ThroughputControl extends Object
ThroughputControl limits the throughput of a process to a maximum number of transactions in a given period of time. As an example, the following code will cap the transaction count at 15 every second (a.k.a. 15 TPS).

 ThroughputControl throughput = new ThroughputControl(15, 1000);

 while (isConditionTrue()) {
     throughput.control();
     // Do stuff.
 }

  • Constructor Summary

    Constructors
    Constructor
    Description
    ThroughputControl(int[] maxTransactions, int[] periodInMillis)
    Constructs a throttle with multiple parallel rate-limit windows.
    ThroughputControl(int maxTransactions, int periodInMillis)
    Constructs a throttle limiting throughput to maxTransactions per period.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    This method should be called on every transaction.
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ThroughputControl

      public ThroughputControl(int maxTransactions, int periodInMillis)
      Constructs a throttle limiting throughput to maxTransactions per period.
      Parameters:
      maxTransactions - Transaction count threshold.
      periodInMillis - Time window, expressed in milliseconds.
    • ThroughputControl

      public ThroughputControl(int[] maxTransactions, int[] periodInMillis)
      Constructs a throttle with multiple parallel rate-limit windows.
      Parameters:
      maxTransactions - An array with transaction count thresholds.
      periodInMillis - An array of time windows, expressed in milliseconds.
  • Method Details

    • control

      public long control()
      This method should be called on every transaction. It will pause the thread for a while when the threshold is reached in order to control the process throughput.
      Returns:
      Returns sleep time in milliseconds when threshold is reached. Otherwise, zero.
    • toString

      public String toString()
      Overrides:
      toString in class Object