Ste
Ste

Reputation: 419

How to extend AbstractScheduledService.Scheduler

I am trying to extend AbstractScheduledService.Scheduler so that I can use a schedule that stores initial delay and period in instance variable.

I am trying with the following

public class ScannerScheduler extends AbstractScheduledService.Scheduler {

    @Override
    public final Future<?> schedule(AbstractService as, 
        ScheduledExecutorService ses, Runnable task) {
        Executors.newSingleThreadScheduledExecutor()
                 .scheduleAtFixedRate(task, 0, 1, TimeUnit.DAYS);
    }
}

But the compiler still complains with:

ScannerScheduler is not abstract and does not override abstract method schedule(AbstractService,ScheduledExecutorService,Runnable) in Scheduler

What am I missing?

Upvotes: 0

Views: 1257

Answers (2)

Stephen C
Stephen C

Reputation: 718886

REVISED

The Scheduler class has a private constructor, which means that you cannot extend it without modifying the Guava library code.

You therefore need to take the alternative approach suggested by the javadocs.

If more flexibility is needed then consider subclassing CustomScheduler.


(The compilation error is a bit misleading in this case ... but the bottom line is that the extend approach will not work.)

Upvotes: 4

Tobb
Tobb

Reputation: 12205

The method Future<?> schedule(AbstractService service, ScheduledExecutorService executor, Runnable runnable) of AbstractScheduledService is final, and thus cannot be overridden.

See line 393 here: https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/util/concurrent/AbstractScheduledService.java?r=d0f4023bd9027b21d9ef3f8077651ad0d2c2275d

From the code there, it seems that the same method signature in the class Scheduler can be overridden, don't know if that helps you though:

new Scheduler() {
    @Override
    public Future<?> schedule(AbstractService service, ScheduledExecutorService executor,
        Runnable task) {
      return executor.scheduleWithFixedDelay(task, initialDelay, delay, unit);
    } 
  };

Upvotes: 0

Related Questions