I am trying to create a java class that will connect to an online service, lookup a value and then update this value in my Database. It is a relatively simple task but since it is a large set of values i have the condition of executing this task in chunks of data (lets say 100).
So this ServiceSyncer class should(?) execute the following pseudo-code:
while (select Objects where timestamp < today, limit 100 ) returns items {
foreach Object {
try {
connect to Service for Object.attribute
extract attribute value
If attribute value has changed {
update Object {
set attribute value
}
}
} catch exception {
log exception
}
update timestamp = now()
}
}
This loop will update all objects in chunks of 100 until all objects have been looked up. Objects that failed during lookup will remain failed until next time the syncer will execute again.
What would be conceptually a good approach on solving this problem?
**The above pseudo code looks less obsolete when trying to implement it.
Something like that will work, but i wonder if there is a better way
JAVA
boolean keepProcessing = true;
int startIndex = 0;
int endIndex = 0;
while (keepProcessing) {
endIndex = startIndex + 100;
if (endIndex > publist.size() - 1) {
endIndex = publist.size() - 1;
keepProcessing = false;
}
List<ObjectJPA> currentBatchList = publist.subList(startIndex, endIndex);
for (int i = 0; i < currentBatchList.size(); i++) {
// do staff
}
repository.save(currentBatchList);
}