Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropwizard ScheduledExecutorService

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.

like image 297
user3280180 Avatar asked Oct 23 '14 10:10

user3280180


2 Answers

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"
like image 30
herrtim Avatar answered Sep 18 '22 22:09

herrtim


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)

like image 140
krisr Avatar answered Sep 19 '22 22:09

krisr