I need to start a service which extends the service AbstractScheduledService
in Guava. I want to start the service in such a way that runOneIteration()
method is executed as soon as the service is started by running it's start method.
I don't want to use startAsync()
method as that would return asynchronously.
If I use awaitRunning()
, the documentation specifies that it will only return after the service reaches RUNNING state. But does that guarantee that runOneIteration()
is called before returning? If not, is there any way I can guarantee that runOneIteration()
is called at least once before returning from starting the service.
Here is one approach:
If I Override the startUp()
method and call runOneIteration()
in it, is this going to work?
@Override
public void startUp() {
runOneIteration();
}
Here is a dirty hack I used to solve this issue.
public void start() {
runOneIteration();
startAsync();
}
@Override
protected Scheduler scheduler() {
return Scheduler.newFixedRateSchedule(0, 10, TimeUnit.SECONDS);
}
I call the start()
method on this service.
serviceName.start();
This ensures that runOneIteration
is executed once when serviceName.start()
is called.