Search in sources :

Example 1 with RunnableService

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);
        }
    }
}
Also used : RunnableService(org.structr.api.service.RunnableService) DatabaseService(org.structr.api.DatabaseService) NodeService(org.structr.core.graph.NodeService) ExecutorService(java.util.concurrent.ExecutorService) Service(org.structr.api.service.Service) RunnableService(org.structr.api.service.RunnableService) SchemaService(org.structr.schema.SchemaService)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)1 DatabaseService (org.structr.api.DatabaseService)1 RunnableService (org.structr.api.service.RunnableService)1 Service (org.structr.api.service.Service)1 NodeService (org.structr.core.graph.NodeService)1 SchemaService (org.structr.schema.SchemaService)1