BigFatBaby
BigFatBaby

Reputation: 1510

How can i interrupt a live Job in Play! framework

I have tried looking, without any luck, for some kind of function to kill/interrupt a working Job in the Play! framework.

Am i missing something? or did Play! actually not add this functionality?

Upvotes: 3

Views: 1049

Answers (1)

Tilman Schweitzer
Tilman Schweitzer

Reputation: 1487

There is nothing like the stop method in the Java Thread class, which is deprecated for good reasons. The clean way is to have something like an interrupted boolean. If you extend play.jobs.Job you can easily add own interrupt methods the change the jobs state. You can do something like:

ComputeBigPrime primeJob = new ComputeBigPrime();
Promise<Integer> promisedPrime = primeJob.now();

// Timeout for interrupt
await("10s");
primeJob.interrupt();

// Wait until it's really finished to get the result
Integer prime = await(promisedPrime);

render(prime);

If you want to do the interrupt between more than one request, you can use the play.cache.Cache and set an unique id (e.g. a UUID) with the information, if the thread is interrupted or not.

Of course this does not work, if you job is hanging in some other API call and you can not use a loop the check the interrupted boolean, but if you really need to do a hard stop like in Thread, you can try to use a Thread inside you controller or job. As its all just Java it should work.

Upvotes: 1

Related Questions