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

java:删除实现IsScheduledJob的类中的当前计划作业,java,ischedulingservice,Java,Ischedulingservice,在计划作业本身的执行过程中,我想阻止它一次又一次地执行,如果没有我在第一次创建作业时收到的字符串,我怎么能这样做呢 public class UfkJob implements IScheduledJob { public void execute(ISchedulingService service) { if (...) { /* here i want to remove the current running job */ } } 我使用以下命令在外部执行作业:

在计划作业本身的执行过程中,我想阻止它一次又一次地执行,如果没有我在第一次创建作业时收到的字符串,我怎么能这样做呢

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

我刚刚意识到我最初回答的问题是错误的,为了子孙后代,我将把它留在下面。再次假设我正在寻找正确的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(整数 间隔,IsScheduledJob作业,int (延迟)为定期工作安排工作 将在 指定的延迟

关键是“定期执行”。听起来你要找的是:

addScheduledOnceJob(长时间增量, IsScheduledJob(计划作业)为计划作业 未来的单一执行

所以你的电话是:

service.addScheduledOnceJob(5000, new UfkJob());
它将在方法调用后5秒执行一次UfkJob