java:删除实现 IScheduledJob 的类中的当前计划作业

发布于 2024-08-31 03:11:31 字数 485 浏览 2 评论 0原文

在执行计划作业本身时,我想阻止它一次又一次地执行,如果没有我首先创建作业时收到的字符串,我该如何做到这一点?

public class UfkJob implements IScheduledJob {

 public void execute(ISchedulingService service) {
   if (...) {
   /* here i want to remove the current running job */
  }
}

我使用以下命令在外部执行该作业:

 ISchedulingService service = (ISchedulingService) getScope().getContext().getBean(ISchedulingService.BEAN_NAME);

service.addScheduledJobAfterDelay(5000,new UfkJob(),200);

In the execution of the scheduled job itself i want to stop it from being executed again and again, how can i do so without having the string that i received when i created the job in the first place ?

public class UfkJob implements IScheduledJob {

 public void execute(ISchedulingService service) {
   if (...) {
   /* here i want to remove the current running job */
  }
}

I executed the job outside by using the commands:

 ISchedulingService service = (ISchedulingService) getScope().getContext().getBean(ISchedulingService.BEAN_NAME);

service.addScheduledJobAfterDelay(5000,new UfkJob(),200);

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

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

发布评论

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

评论(1

冬天旳寂寞 2024-09-07 03:11:32

我刚刚意识到我认为我最初回答了错误的问题,为了后代,我将把它留在下面。再次假设我正在寻找正确的 API。添加一个字段到 UFkJob:

public class UfkJob implements IScheduledJob {

  String jobName = null;

 public void execute(ISchedulingService service) {
   if (... && jobName != null) {
   /* here i want to remove the current running job */
   ISchedulingService service = (ISchedulingService) getScope().getContext().getBean(ISchedulingService.BEAN_NAME);
   service.removeScheduledJob(jobName);
  }
  public void setJobName(String name){
    this.jobName = name;
  }
}

然后当你安排工作时:

ISchedulingService service = (ISchedulingService) getScope().getContext().getBean(ISchedulingService.BEAN_NAME);
UfkJob job = new UfkJob();
job.setJobName(service.addScheduledJobAfterDelay(5000, job, 200));

或者你总是可以拥有工作安排本身:

public class UfkJob implements IScheduledJob {

  String jobName;
  ISchedulingService service;

  public UfkJob(ISchedulingService service){
    this.service = service;
    this.jobName = service.addScheduledJobAfterDelay(5000, this, 200);
  }
  public void execute(ISchedulingService service) {
    if (...) {
      service.removeScheduledJob(jobName);
    }
  }

}

//Your calling code
...
new UfkJob((ISchedulingService) getScope().getContext().getBean(ISchedulingService.BEAN_NAME));

----- 下面是我原来的答案,我相信是错误的问题 ----

我不确定是否我正在查看正确库的 API 文档,但是您的方法调用:

service.addScheduledJobAfterDelay(5000,new UfkJob(),200);

定义为:

addScheduledJobAfterDelay(int
间隔,IScheduledJob 作业,int
延迟)定期安排作业
执行将在之后开始
指定延迟。

关键是“定期执行”。听起来您正在寻找的是:

addScheduledOnceJob(long timeDelta,
IScheduledJob 作业)安排作业
将来单次执行。

因此,您的调用将是:

service.addScheduledOnceJob(5000, new UfkJob());

它将在方法调用后 5 秒执行一次 UfkJob。

I just realized I think I answered the wrong question initially, I will leave it below for the sake of posterity. Again an assumption I am looking at the right API. Add a field to UFkJob:

public class UfkJob implements IScheduledJob {

  String jobName = null;

 public void execute(ISchedulingService service) {
   if (... && jobName != null) {
   /* here i want to remove the current running job */
   ISchedulingService service = (ISchedulingService) getScope().getContext().getBean(ISchedulingService.BEAN_NAME);
   service.removeScheduledJob(jobName);
  }
  public void setJobName(String name){
    this.jobName = name;
  }
}

and then when you schedule the job:

ISchedulingService service = (ISchedulingService) getScope().getContext().getBean(ISchedulingService.BEAN_NAME);
UfkJob job = new UfkJob();
job.setJobName(service.addScheduledJobAfterDelay(5000, job, 200));

or you could always have the job schedule itself:

public class UfkJob implements IScheduledJob {

  String jobName;
  ISchedulingService service;

  public UfkJob(ISchedulingService service){
    this.service = service;
    this.jobName = service.addScheduledJobAfterDelay(5000, this, 200);
  }
  public void execute(ISchedulingService service) {
    if (...) {
      service.removeScheduledJob(jobName);
    }
  }

}

//Your calling code
...
new UfkJob((ISchedulingService) getScope().getContext().getBean(ISchedulingService.BEAN_NAME));

----- My original answer below, I believe for the wrong question ----

I'm not sure if I am looking at the API docs for the right library, however your method call:

service.addScheduledJobAfterDelay(5000,new UfkJob(),200);

is defined as:

addScheduledJobAfterDelay(int
interval, IScheduledJob job, int
delay) Schedule a job for periodic
execution which will start after the
specifed delay.

The key being "periodic execution". It sounds like what you are looking for is:

addScheduledOnceJob(long timeDelta,
IScheduledJob job) Schedule a job for
single execution in the future.

So your call would be:

service.addScheduledOnceJob(5000, new UfkJob());

Which would execute the UfkJob a single time 5 seconds after the method call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文