Question

I am implementing guava into a module inside myApp, I am having difficulty in understanding when the AbstractScheduledService works, my goal is make a service that runs from time to time, but I want to by code, set to run in different time intervals. So my question is: How can I update the time interval in the delay between Iterations?

My Code so far

public class Service extends AbstractScheduledService {


public String id;
private int timeInterval;
private TimeUnit unit;
public boolean keepAsking;

/**
 *
 * @param newTimeInterval
 * @param newTimeUnit
 */
public void pauseAndUpdate(int newTimeInterval, TimeUnit newTimeUnit) throws Exception {
    this.shutDown();
    timeInterval = newTimeInterval;
    unit = newTimeUnit;
    keepAsking = true;
    this.startAndWait();
}

protected Scheduler scheduler() {
    System.out.println("Scheduling : "+ timeInterval + unit);
    return Scheduler.newFixedRateSchedule(0, timeInterval, unit);
}

public Service(int timeInterval,TimeUnit unit){
    super();
    id = "XPTO";
    this.timeInterval = timeInterval;
    this.unit = unit;
    keepAsking = true;
}

public void pauseAndUpdate(int newTimeInterval) throws Exception {

    pauseAndUpdate(newTimeInterval,this.unit);

}

@Override
protected void runOneIteration() throws Exception {
    System.out.println("Running");
    if(!keepAsking) {
        stop();
    }
}
Était-ce utile?

La solution

It sounds like you'll want to change your scheduler() method to return a CustomScheduler. After each iteration, the service will ask the scheduler for the delay before the next iteration.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top