So, let's say I start a job from a controller asynchronously and then render some template.
MyJob job = new MyJob();
job.doJob();
render();
MyJob looks like:
import play.jobs.*;
public class MyJob extends Job {
public void doJob() {
// execute some application logic here until I say to quit via a controller method
}
}
From the UI, I do some actions, and I trigger a request to another route in the controller which would end the job. I don't want to have complicated, continuous DAO actions handled on the client side, so what is the best way to go about this? I have an EC2 elastic cache setup, so the main problem is assigning an ID开发者_运维知识库 to a job.
job.endJob(id); ?
If you look at JobsPlugin class - it uses ScheduledThreadPoolExecutor executor
to maintain a list of jobs.
This class has a remove method, which you can try to use.
I'm not aware of a way to easily stop a Job. As Play is stateless, probably your best bet would be to have some flag in the database that the job can check to decide to stop, but doesn't look a great solution to me.
Have you checked Continuations in Play? I believe they may suit your scenario better.
private static ScheduledFuture<?> future;
private static final TestJob testJob = new TestJob();
future = JobsPlugin.executor.scheduleWithFixedDelay(testJob, 1, 1, TimeUnit.SECONDS);
future.cancel(true);
future = JobsPlugin.executor.scheduleWithFixedDelay(testJob, 5, 5, TimeUnit.SECONDS);
精彩评论