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()));
}
});
}
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();
}
});
}
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);
}
}
Aggregations