Original program code to modify:
/this method is called periodically and it takes data from db, performs some ooperations on it, and puts the results in a file which is sent at the ends of all processing via ftp/
void pullData(){
while(!stopped){
rows= getRowsFromDb();
for (row: rows){
newRow= process(row);
rowsContainer.add(newRow)
}
writeToFile(rowsContainer);
sendFileSomewhere();
}
}
Now this code has to be modified to allow the following commands to be issued to it: process only 1 row; skip 1 row (more commands to come later). To do this the application is put in suspended mode and it will not process rows until it get one of the 2 commands I outlined above. Also, it can be resumed from suspended mode and then it’s back to normal processing.
The question is not how to do this, but what is the right way to do it.
(1) Someone suggested boolean flags for each operation. This is a pretty bad idea. I recommend people avoid flags for many reasons (some may have been discussed on stack overflow as well).
(2) Another idea was to use a bool flag for isSuspended and enums for the commands. This is better than (1), but not a lot because app needs to check which command was the last one and if it is in suspended mode and act accoringly.
(3) I was thinking of breaking up the application and use the Strategy pattern. Each command would get it’s own method in the application. This is a bit more complicated, but to me it seems a lot cleaner.
What would you suggest (you don’t need to choose from the options I listed, I want to hear how you handled a similar situation)?
I would use an ExecutorService with tasks like
Note: Your loops have to honour interrupts or they won’t stop just because the thread has been interrupted. You can use