¿Puede el controlador de Grails devolver dinámicamente una actualización a una vista antes de que finalice la acción?

Situación actual

I'm executing an external application (by using Runtime.getRuntime().exec... example aquí) and I'm listening on the Stream the app writes stdout to. That part is working as intended and I get results (in grails controller), linea por linea, as they are available and -- when the stream has no more content -- I render the result on the page.

Other aspect is initiation of that application from the gsp page, asynchronously. That is also already covered and the remoteFunction call is made (to the above mentioned controller method), the results are gathered and returned and they are correctly displayed in designated area on the page.

But, what is bugging me and what I actually want to achieve is:


to display the lines read from the stream mientras se leen, not after the whole stream finishes (as it is done now).

That means (something like, for example) calling render for each line in the Stream-reading loop:

while ((line = bri.readLine()) != null) {
  //render line here?

¿Se puede lograr esto?

Please explain :)

preguntado el 09 de marzo de 12 a las 13:03

Did you try your own example? -

Yes. The page gets the response only when the controller method exits. -

Are you sure you're using a big enough example? You could try explicitly calling flush every once in a while on the output stream. -

@TedNaleid The "simulator" is a DOS batch script echoing several lines with sleep 1 inbetween. The System.out (in the stream reader loop) prints the lines as they occur. The problem is that I can't get the same to display on the page, but only after the controller method exits (i.e. when the onComplete in remoteFunction is called back) -

1 Respuestas

I think there is no standard grails way to achieve your desired behavoir.

El g:remoteFunction ist still an tag that wraps the AJAX request for you. The request is bounded to the request response pattern - you can't get multiple responses for one request. Your action code won't be modified automatically.

You have to implement it on your own.

Intentaría lo siguiente:

  • Split your single remoteFunction call into multiple AJAX requests.
  • The first request initializes a nuevo hilo which listen to your external application.
  • The results of this application have to be stored in alcance de la sesión, so that ...
  • el siguiente periodical requests from the client can poll the current state of the execution.

respondido 11 mar '12, 13:03

Seems sensible. Will give it a try today and will let you know. - Saran

It works. Thanks @Aiolos for pointing me in the right direction :) - Saran

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.