You can use the Managed interface. In the snippet below, I use ScheduledExecutorService to do the tasks, but you can use Quartz instead if you want. I prefer to work with ScheduledExecutorService as it is simpler and simpler ...
The first step is to register the managed service.
environment.lifecycle().manage(new JobExecutionService());
The second step is to write it down.
public class JobExecutionService implements Managed { private final ScheduledExecutorService service = Executors.newScheduledThreadPool(2); @Override public void start() throws Exception { System.out.println("Starting jobs"); service.scheduleAtFixedRate(new HelloWorldJob(), 1, 1, TimeUnit.SECONDS); } @Override public void stop() throws Exception { System.out.println("Shutting down"); service.shutdown(); } }
and the work itself
public class HelloWorldJob implements Runnable { @Override public void run() { System.out.println(System.currentTimeMillis()); } }
As mentioned in the comment below, if you use Runnable , you can Thread.getState() . See Get a list of all topics currently running in Java . You may need some intermediate parts depending on how you connect the application.
source share