Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to abort controller action in play framework 2.1 when client cancel request or client socket is closed?

I am testing play framework 2.1 to check what happen when client cancel request or socket is closed from client side. I created this simple program:

package controllers;

import play.; import play.mvc.;

import views.html.*;

public class Application extends Controller {

public static Result index() { try{ for(int i=0;i<1000;i++){ Thread.sleep(10000); System.out.println(i+"\n"); } }catch(Exception e){ System.out.println("\nexcepción capturada"); } return ok(index.render("Your new application is ready.")); }

}

If I cancel request from client (google chrome) the loop isn't aborted. I think this could be a problem in real world application, not making a loop, but doing a "heavy" query to database.

like image 364
gavioto Avatar asked Dec 02 '25 12:12

gavioto


1 Answers

Since there was no answer I asked on the google group for play https://groups.google.com/forum/?hl=en#!topic/play-framework/POvFA3moXug and here is what James Roper from Typesafe said:

Play offers no such feature. However, there are other ways to achieve the same thing, for example, if you have a long running request, you could do it as a websockets request, which let's you easily detect when the client navigates to a different page.

The latest release as of today is play-2.2.0-RC1 (in case they add support later).

like image 97
Fabian Kessler Avatar answered Dec 05 '25 12:12

Fabian Kessler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!