Spring Controller, how to break out of a loop based on a new HttpRequest?

644 Views Asked by At

I have a Spring @Controller. Inside the controller there is a method that after a specific request enters a while loop, which checks for new data and breaks only when data is found:

while(System.currentTimeMillis() < end && controllersDataStore.getAutoupdate() == eventAutoUpdate ) {

        gridData = (GenericGrid) dsEventLogService.retrieveNewEventGridRows(gridHelper);
        if(gridData.getAaData().size()==0){
            try {
                Thread.sleep(POLLING_INTERVAL);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        else{
            break;
        }
    }

I need to break out of the loop in certain cases even if data is not found, after a specific user action. So I need from a new HttpRequest, to be able to break from the previous while loop.

Also I need this to be per user, so one user can not interact with another user actions.

A controller is a singleton so it has common properties in all sessions. This means that we can not store a state in a global property and then check inside the loop for a change so it can break, because this will not be a per session property.

Is there any way to do that besides having the controller to have a session scope? (Which I am not even sure if it will work).

Thank you in advance, I will appreciate any tip.

1

There are 1 best solutions below

3
On

Store a keepRunning value in a db config table and after n seconds check this too.

A separate request in a different controller can update the state variable independently from this controller.