Search in sources :

Example 1 with ServerLifecycleListener

use of io.dropwizard.lifecycle.ServerLifecycleListener 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)

Example 2 with ServerLifecycleListener

use of io.dropwizard.lifecycle.ServerLifecycleListener in project irontest by zheng-wang.

the class IronTestApplication method run.

@Override
public void run(IronTestConfiguration configuration, Environment environment) throws IOException {
    final JdbiFactory jdbiFactory = new JdbiFactory();
    final Jdbi systemDBJdbi = jdbiFactory.build(environment, configuration.getSystemDatabase(), "systemDatabase");
    // compare system database version with uber jar version to see whether an upgrade is needed
    Integer versionTableCount = systemDBJdbi.withHandle(handle -> handle.createQuery("select count(*) from information_schema.tables where table_name = 'VERSION'").mapTo(Integer.class).findOnly());
    if (versionTableCount == 1) {
        // VERSION table exists in the system database (i.e. we are not starting a brand new Iron Test build)
        if (!checkVersion(systemDBJdbi)) {
            System.out.println("Press Enter to exit.");
            System.in.read();
            System.exit(0);
        }
    }
    // by 'new SSLContextBuilder().loadTrustMaterial').
    if (new File(configuration.getSslTrustStorePath()).exists()) {
        System.setProperty("javax.net.ssl.trustStore", configuration.getSslTrustStorePath());
        System.setProperty("javax.net.ssl.trustStorePassword", configuration.getSslTrustStorePassword());
    }
    // start WireMock server (in the same JVM)
    WireMockServer wireMockServer = new WireMockServer(options().extensions(new ResponseTemplateTransformer(true)).port(Integer.parseInt(configuration.getWireMock().get("port"))).maxRequestJournalEntries(Integer.parseInt(configuration.getWireMock().get("maxRequestJournalEntries"))).notifier(new WireMockFileNotifier()));
    wireMockServer.start();
    createSystemResources(configuration, environment, systemDBJdbi, wireMockServer);
    createSampleResources(configuration, environment);
    environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {

        @Override
        public void serverStarted(Server server) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Iron Test started with UI address http://localhost:" + +getLocalPort(server) + "/ui");
            System.out.println();
        }
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) JdbiFactory(io.dropwizard.jdbi3.JdbiFactory) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) Server(org.eclipse.jetty.server.Server) ServerLifecycleListener(io.dropwizard.lifecycle.ServerLifecycleListener) File(java.io.File) ResponseTemplateTransformer(com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer)

Example 3 with ServerLifecycleListener

use of io.dropwizard.lifecycle.ServerLifecycleListener in project registry by hortonworks.

the class RegistryApplication method registerHA.

private void registerHA(HAConfiguration haConfiguration, Environment environment) throws Exception {
    if (haConfiguration != null) {
        environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {

            @Override
            public void serverStarted(Server server) {
                String serverUrl = server.getURI().toString();
                LOG.info("Received callback as server is started with server URL:[{}]", server);
                LOG.info("HA configuration: [{}]", haConfiguration);
                String className = haConfiguration.getClassName();
                LeadershipParticipant leadershipParticipant = null;
                try {
                    leadershipParticipant = (LeadershipParticipant) Class.forName(className).newInstance();
                } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                    throw new RuntimeException(e);
                }
                leadershipParticipant.init(haConfiguration.getConfig(), serverUrl);
                leadershipParticipantRef.set(leadershipParticipant);
                LOG.info("Registering for leadership with participant [{}]", leadershipParticipant);
                try {
                    leadershipParticipantRef.get().participateForLeadership();
                } catch (Exception e) {
                    LOG.error("Error occurred while participating for leadership with serverUrl [{}]", serverUrl, e);
                    throw new RuntimeException(e);
                }
                LOG.info("Registered for leadership with participant [{}]", leadershipParticipant);
            }
        });
    } else {
        leadershipParticipantRef.set(LocalLeader.getInstance());
        LOG.info("No HA configuration exists, using [{}]", leadershipParticipantRef);
    }
}
Also used : LeadershipParticipant(com.hortonworks.registries.common.ha.LeadershipParticipant) Server(org.eclipse.jetty.server.Server) ServerLifecycleListener(io.dropwizard.lifecycle.ServerLifecycleListener)

Aggregations

ServerLifecycleListener (io.dropwizard.lifecycle.ServerLifecycleListener)3 Server (org.eclipse.jetty.server.Server)3 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)1 ResponseTemplateTransformer (com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer)1 LeadershipParticipant (com.hortonworks.registries.common.ha.LeadershipParticipant)1 JdbiFactory (io.dropwizard.jdbi3.JdbiFactory)1 Task (io.dropwizard.servlets.tasks.Task)1 File (java.io.File)1 HashMap (java.util.HashMap)1 Jdbi (org.jdbi.v3.core.Jdbi)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