In my case I need to run some scheduled tasks (e.g. every minute) doing some checks in DB and if needed some subtasks. This should be no DB health-check!
DW documentation says:
"It should be noted that Environment has built-in factory methods for ExecutorService and ScheduledExecutorService instances which are managed. See LifecycleEnvironment#executorService and LifecycleEnvironment#scheduledExecutorService for details."
Does anyone knows how to implement this in DW? Trying to play around with DW code possibilities, I found this:
String nameFormat = "?What should this string contain?";
ScheduledExecutorServiceBuilder sesBuilder = environment.lifecycle().scheduledExecutorService(nameFormat);
ScheduledExecutorService ses = sesBuilder.build();
Runnable alarmTask = new AlarmTask();
ses.scheduleWithFixedDelay(alarmTask, 0, 5, TimeUnit.SECONDS);
Is this the correct way in DW to do this? BTW a runnable dummy:
private static final class AlarmTask implements Runnable {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
@Override public void run() {
++fCount;
cal = Calendar.getInstance();
System.out.println(fCount + "x BEEP:" + dateFormat.format(cal.getTime()));
}
private int fCount;
}
Whats the purpose of the initial name and is it used somewhere? Hope someone can help.
With dropwizard-sundial, you can easily integrate the lightweight multi-threaded Java job scheduling library, Sundial, and add Jobs either using a SimpleTrigger
or a CronTrigger
. In your case, you'd start out with a class defining your job logic with a SimpleTrigger
annotation:
@SimpleTrigger(repeatInterval = 60, timeUnit = TimeUnit.SECONDS)
public class SampleJob extends com.xeiam.sundial.Job {
@Override
public void doRun() throws JobInterruptException {
// Do something interesting...
}
}
In your Dropwizard app's yaml
file, you'll need to define what package dropwizard-sundial
should search for annotated job classes. Following is an example config with the annotated-jobs-package-name
config param along with several other optional params for fine-tuning the scheduler:
sundial:
thread-pool-size: 10
shutdown-on-unload: true
wait-on-shutdown: false
start-delay-seconds: 0
start-scheduler-on-load: true
global-lock-on-load: false
annotated-jobs-package-name: com.foo.bar.jobs
Additionally, you can control the scheduler asynchronously via Curl while the app is running to do things like locking and unlocking the scheduler, starting, stopping, adding, removing jobs and triggers. Here are a few examples:
curl -X POST http://localhost:9090/admin/tasks/locksundialscheduler
curl -X POST http://localhost:9090/admin/tasks/unlocksundialscheduler
curl -X POST "http://localhost:9090/admin/tasks/startjob?JOB_NAME=MyJob"
I'm doing pretty much the same thing in a Dropwizard app to run a job periodically. There are projects such as dropwizard-jobs and dropwizard-quartz, but this seemed to work fine for my simple needs.
The ScheduledExecutorServiceBuilder
passes the nameFormat
to the ThreadFactoryBuilder
as a pattern for naming the threads. The docs for that might be helpful to you: https://guava.dev/releases/snapshot/api/docs/com/google/common/util/concurrent/ThreadFactoryBuilder.html#setNameFormat(java.lang.String)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With