Search code examples
javaspringspring-bootscheduled-tasksfrequency

Spring schedule "Busyness"


so i'm programming a simulation of a CameraDetector system for traffic. and the incoming images (called Messages) have an ID, a Licenseplate and a timestamp (LocalDateTime.Now)

The problem is that i have to program busyness. As you know, traffic has "busyness" between 5 to 6PM and 8-9 AM. I have to simulate this busyness by generating more Messages at those times than on regular times.

How can i do this with Spring?

**To clarify the messages need to be sent more frequently during busy hours. the timestamps need to be unchanged the moment they are generated. This is to simulate a real workload on the thread(s).

Documentation references and/or code are really helpfull,

the MessageGenerateMethod

  @Override
  public CameraMessage generate() {
      randomLicensePlate = String.format("%d-%s%s%s-%d%d%d", r.nextInt(9) + 1, rndChar(), 
                                    rndChar(), rndChar(),
      r.nextInt(9) + 1, r.nextInt(9) + 1, r.nextInt(9) + 1);

      return new CameraMessage(randomIdBound, randomLicensePlate.toString(), 
                                                     LocalDateTime.now());
}

Solution

  • Make an int called frequency. The frequency should be higher during peak hours and lower during non-peak hours. Once every minute (on your system clock), recalibrate your frequency via an algebraic function -- just an elementary mathematics function f(t).

    Use Spring Scheduling (fixed rate = K) to schedule a message cascade event every minute which sends frequency messages, and then recalibrates freqency = f(now()). Repeat.

    The cascading function probably doesnt make sense to shoot all n messages at once -- maybe space them out uniformly or randomly