玩! - 动态结束作业

发布于 2024-12-07 11:41:02 字数 512 浏览 1 评论 0原文

因此,假设我从控制器异步启动一项作业,然后渲染一些模板。

MyJob job = new MyJob();
job.doJob();

render();

MyJob 看起来像:

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
    }

}

从 UI 中,我执行一些操作,然后触发对控制器中另一个路由的请求,这将结束作业。我不想在客户端处理复杂、连续的 DAO 操作,那么解决这个问题的最佳方法是什么?我有一个 EC2 弹性缓存设置,因此主要问题是为作业分配 ID。

 job.endJob(id); ?

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); ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑咖 2024-12-14 11:41:02

如果您查看 JobsPlugin 类 -它使用ScheduledThreadPoolExecutor执行器来维护作业列表。

这个类有一个 remove方法,你可以尝试使用。

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.

舂唻埖巳落 2024-12-14 11:41:02

我不知道有什么方法可以轻松停止作业。由于 Play 是无状态的,因此最好的选择可能是在数据库中添加一些标志,作业可以检查该标志以决定停止,但对我来说这看起来不是一个很好的解决方案。

您是否在 Play 中检查过Continuations?我相信它们可能更适合您的场景。

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.

夏九 2024-12-14 11:41:02
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);
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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文