Skip to content

Instantly share code, notes, and snippets.

@lundvall
Created September 14, 2015 11:29
Show Gist options
  • Save lundvall/8b2fd24dbcc68415a397 to your computer and use it in GitHub Desktop.
Save lundvall/8b2fd24dbcc68415a397 to your computer and use it in GitHub Desktop.
Ratpack scheduled service
import ratpack.exec.ExecController;
import ratpack.server.Service;
import ratpack.server.StartEvent;
import ratpack.server.StopEvent;
import java.util.concurrent.TimeUnit;
public class ScheduledService implements Service, Runnable {
public String getName() {
return getClass().getSimpleName();
}
public void onStart(StartEvent event) throws Exception {
ExecController.require().getExecutor()
.scheduleAtFixedRate(this, 0, 5, TimeUnit.SECONDS);
}
public void onStop(StopEvent event) throws Exception {
System.out.println("stop:" + getName());
}
public void run() {
ExecController.require().fork().start(e -> {
System.out.println("running: " + getName());
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment