开发者

java: remove current scheduled job in a class that implements IScheduledJob

开发者 https://www.devze.com 2022-12-29 06:33 出处:网络
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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消