lundi 28 décembre 2020

Random for-loop in Java?

I have 25 batch jobs that are executed constantly, that is, when number 25 is finished, 1 is immediately started.

These batch jobs are started using an URL that contains the value 1 to 25. Basically, I use a for loop from 1 to 25 where I, in each round, call en URL with the current value of i, http://batchjobserver/1, http://batchjobserver/2 and so on.

The problem is that some of these batch jobs are a bit unstable and sometimes crashes which causes the for-loop to restart at 1. As a consequence, batch job 1 is run every time the loop is initiated while 25 runs much less frequently.

I like my current solution because it is so simple (in pseudo code)

for (i=1; i < 26; i++) {
   getURL ("http://batchjob/" + Integer.toString(i));
}

However, I would like I to be a random number between 1 and 25 so that, in case something crashes, all the batch jobs, in the long run, are run approximately the same number of times.

Is there some nice hack/algorithm that allows me to achieve this?

Other requirements:

  • The number 25 changes frequently
  • This is not an absolut requirement but it would be nice one batch job wasn't run again until all other all other jobs have been attempted once. This doesn't mean that they have to "wait" 25 loops before they can run again, instead - if job 8 is executed in the 25th loop (the last loop of the first "set" of loops), the 26th loop (the first loop in the second set of loops) can be 8 as well.

Randomness has another advantage: it is desirable if the execution of these jobs looks a bit manual.




Aucun commentaire:

Enregistrer un commentaire