Search in sources :

Example 1 with SundialInitializerListener

use of org.knowm.sundial.ee.SundialInitializerListener in project dropwizard-sundial by knowm.

the class SundialBundle method run.

@Override
public void run(T configuration, final Environment environment) throws Exception {
    SundialConfiguration sundialConfiguration = getSundialConfiguration(configuration);
    // this sets up Sundial with all the default values
    environment.servlets().addServletListeners(new SundialInitializerListener());
    // here we can override the defaults
    if (sundialConfiguration.getThreadPoolSize() != null) {
        environment.servlets().setInitParameter("thread-pool-size", sundialConfiguration.getThreadPoolSize());
    }
    if (sundialConfiguration.getPerformShutdown() != null) {
        environment.servlets().setInitParameter("shutdown-on-unload", sundialConfiguration.getPerformShutdown());
    }
    if (sundialConfiguration.getWaitOnShutdown() != null) {
        environment.servlets().setInitParameter("wait-on-shutdown", sundialConfiguration.getWaitOnShutdown());
    }
    if (sundialConfiguration.getStartDelay() != null) {
        environment.servlets().setInitParameter("start-delay-seconds", sundialConfiguration.getStartDelay());
    }
    if (sundialConfiguration.getStartOnLoad() != null) {
        environment.servlets().setInitParameter("start-scheduler-on-load", sundialConfiguration.getStartOnLoad());
    }
    if (sundialConfiguration.getGlobalLockOnLoad() != null) {
        environment.servlets().setInitParameter("global-lock-on-load", sundialConfiguration.getGlobalLockOnLoad());
    }
    if (sundialConfiguration.getGlobalLockOnLoad() != null) {
        environment.servlets().setInitParameter("global-lock-on-load", sundialConfiguration.getGlobalLockOnLoad());
    }
    if (sundialConfiguration.getAnnotatedJobsPackageName() != null) {
        environment.servlets().setInitParameter("annotated-jobs-package-name", sundialConfiguration.getAnnotatedJobsPackageName());
    }
    Set<String> enabledTasks = sundialConfiguration.getTasks();
    Map<String, Task> availableTasks = new HashMap<String, Task>();
    for (Class<? extends Task> taskClass : Arrays.asList(UnlockSundialSchedulerTask.class, LockSundialSchedulerTask.class, RemoveJobTriggerTask.class, AddCronJobTriggerTask.class, StartJobTask.class, StopJobTask.class, RemoveJobTask.class, AddJobTask.class)) {
        Task task = taskClass.newInstance();
        availableTasks.put(task.getName(), task);
    }
    if (enabledTasks == null) {
        enabledTasks = availableTasks.keySet();
    }
    if (!availableTasks.keySet().containsAll(enabledTasks)) {
        enabledTasks.removeAll(availableTasks.keySet());
        throw new IllegalArgumentException(String.format("Unknown tasks: %s. Available tasks: %s", enabledTasks, availableTasks.keySet()));
    }
    for (String task : enabledTasks) {
        environment.admin().addTask(availableTasks.get(task));
    }
    environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {

        @Override
        public void serverStarted(Server server) {
            final ListenerManager listenerManager;
            try {
                listenerManager = SundialJobScheduler.getScheduler().getListenerManager();
            } catch (SchedulerException e) {
                throw new RuntimeException(e);
            }
            listenerManager.addTriggerListener(new MetricsReporter(environment.metrics()));
        }
    });
}
Also used : UnlockSundialSchedulerTask(org.knowm.dropwizard.sundial.tasks.UnlockSundialSchedulerTask) AddJobTask(org.knowm.dropwizard.sundial.tasks.AddJobTask) StopJobTask(org.knowm.dropwizard.sundial.tasks.StopJobTask) AddCronJobTriggerTask(org.knowm.dropwizard.sundial.tasks.AddCronJobTriggerTask) RemoveJobTriggerTask(org.knowm.dropwizard.sundial.tasks.RemoveJobTriggerTask) RemoveJobTask(org.knowm.dropwizard.sundial.tasks.RemoveJobTask) LockSundialSchedulerTask(org.knowm.dropwizard.sundial.tasks.LockSundialSchedulerTask) Task(io.dropwizard.servlets.tasks.Task) StartJobTask(org.knowm.dropwizard.sundial.tasks.StartJobTask) SchedulerException(org.quartz.exceptions.SchedulerException) Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) ServerLifecycleListener(io.dropwizard.lifecycle.ServerLifecycleListener) SundialInitializerListener(org.knowm.sundial.ee.SundialInitializerListener) ListenerManager(org.quartz.listeners.ListenerManager)

Aggregations

ServerLifecycleListener (io.dropwizard.lifecycle.ServerLifecycleListener)1 Task (io.dropwizard.servlets.tasks.Task)1 HashMap (java.util.HashMap)1 Server (org.eclipse.jetty.server.Server)1 AddCronJobTriggerTask (org.knowm.dropwizard.sundial.tasks.AddCronJobTriggerTask)1 AddJobTask (org.knowm.dropwizard.sundial.tasks.AddJobTask)1 LockSundialSchedulerTask (org.knowm.dropwizard.sundial.tasks.LockSundialSchedulerTask)1 RemoveJobTask (org.knowm.dropwizard.sundial.tasks.RemoveJobTask)1 RemoveJobTriggerTask (org.knowm.dropwizard.sundial.tasks.RemoveJobTriggerTask)1 StartJobTask (org.knowm.dropwizard.sundial.tasks.StartJobTask)1 StopJobTask (org.knowm.dropwizard.sundial.tasks.StopJobTask)1 UnlockSundialSchedulerTask (org.knowm.dropwizard.sundial.tasks.UnlockSundialSchedulerTask)1 SundialInitializerListener (org.knowm.sundial.ee.SundialInitializerListener)1 SchedulerException (org.quartz.exceptions.SchedulerException)1 ListenerManager (org.quartz.listeners.ListenerManager)1