2

In my Spring/Hibernate/MVC application I implement some long running functions. In this case I would like to inform the user about the status.

I wonder, if I can get certain log4j messages to the frontend, like:

DEBUG d.t.webapp.services.StageServiceImpl - Fetching result
DEBUG d.t.webapp.services.StageServiceImpl - loaded 1000 rows
DEBUG d.t.webapp.services.StageServiceImpl - loaded 1000 rows
DEBUG d.t.webapp.services.StageServiceImpl - loaded 1000 rows
DEBUG d.t.webapp.services.StageServiceImpl - loaded 994 rows

Those message should appear to the frontend step-by-step, as they are produced by the backend, with minimal delay. Is that possible or are there better solutions for this requirement?

tobi
  • 753
  • 1
  • 14
  • 25

1 Answers1

1

Or why not just use something like this:

1# Create a controller to provide you with last status of your worker

@RestController
@RequestMapping(value = "/worker")
public WorkerStatusProviderController {

    @Autowired
        WorkerStatusProvider workerStatusProvider;

    @RequestMapping(value = "/status/{id}", method = RequestMethod.GET)
    public new ResponseEntity<WorkerStatus> getStatus(@PathVariable("id") long workerId){

        //retrieve last worker status
        WorkerStatus lastStatus = workerStatusProvider.getLastStatus(workerId);

        //send worker status to front
        return new ResponseEntity<WorkerStatus>(lastStatus, HttpStatus.OK);
    }
}

2# In frontend use Ajax to call this controller every 2 or 3 seconds and display the last status to your user.

//call /worker/status/id every 2 seconds
setInterval(function(){
    $.get("/worker/status/100", function(workerStatus, status) {

        //display worker status to the user
        alert("workerStatus: " + workerStatus);

    });
}, 2000);
Mouad EL Fakir
  • 3,609
  • 2
  • 23
  • 37