use of org.structr.api.service.RunnableService in project structr by structr.
the class Services method startService.
public void startService(final Class serviceClass) {
int retryCount = 10;
int retryDelay = 30;
boolean waitAndRetry = true;
boolean isVital = false;
logger.info("Creating {}..", serviceClass.getSimpleName());
try {
final Service service = (Service) serviceClass.newInstance();
if (licenseManager != null && !licenseManager.isValid(service)) {
logger.error("Configured service {} is not part of the currently licensed Structr Edition.", serviceClass.getSimpleName());
return;
}
isVital = service.isVital();
retryCount = service.getRetryCount();
retryDelay = service.getRetryDelay();
while (waitAndRetry && retryCount-- > 0) {
waitAndRetry = service.waitAndRetry();
try {
if (service.initialize(this)) {
if (service instanceof RunnableService) {
RunnableService runnableService = (RunnableService) service;
if (runnableService.runOnStartup()) {
// start RunnableService and cache it
runnableService.startService();
}
}
if (service.isRunning()) {
// cache service instance
serviceCache.put(serviceClass, service);
}
// initialization callback
service.initialized();
// abort wait and retry loop
waitAndRetry = false;
} else if (isVital && !waitAndRetry) {
checkVitalService(serviceClass, null);
}
} catch (Throwable t) {
logger.warn("Service {} failed to start: {}", serviceClass.getSimpleName(), t.getMessage());
if (isVital && !waitAndRetry) {
checkVitalService(serviceClass, t);
}
}
if (waitAndRetry) {
if (retryCount > 0) {
logger.warn("Retrying in {} seconds..", (retryDelay * 1000));
Thread.sleep(retryDelay * 1000);
} else {
if (isVital) {
checkVitalService(serviceClass, null);
}
}
}
}
} catch (Throwable t) {
if (isVital) {
checkVitalService(serviceClass, t);
}
}
}
Aggregations