Search in sources :

Example 1 with KieServerExtension

use of org.kie.server.services.api.KieServerExtension in project droolsjbpm-integration by kiegroup.

the class KieServerImpl method disposeContainer.

public ServiceResponse<Void> disposeContainer(String containerId) {
    List<Message> messages = new CopyOnWriteArrayList<Message>();
    try {
        KieContainerInstanceImpl kci = context.unregisterContainer(containerId);
        if (kci != null) {
            synchronized (kci) {
                eventSupport.fireBeforeContainerStopped(this, kci);
                // just in case
                kci.setStatus(KieContainerStatus.DISPOSING);
                if (kci.getKieContainer() != null) {
                    org.kie.api.builder.ReleaseId releaseId = kci.getKieContainer().getReleaseId();
                    List<KieServerExtension> disposedExtensions = new ArrayList<KieServerExtension>();
                    try {
                        // first attempt to dispose container on all extensions
                        logger.debug("Container {} (for release id {}) shutdown: In Progress", containerId, kci.getResource().getReleaseId());
                        // process server extensions
                        List<KieServerExtension> extensions = context.getServerExtensions();
                        for (KieServerExtension extension : extensions) {
                            extension.disposeContainer(containerId, kci, new HashMap<String, Object>());
                            logger.debug("Container {} (for release id {}) {} shutdown: DONE", containerId, kci.getResource().getReleaseId(), extension);
                            disposedExtensions.add(extension);
                        }
                    } catch (Exception e) {
                        logger.warn("Dispose of container {} failed, putting it back to started state by recreating container on {}", containerId, disposedExtensions);
                        // since the dispose fail rollback must take place to put it back to running state
                        KieModuleMetaData metadata = buildKieModuleMetadata(kci.getKieContainer().getReleaseId());
                        Map<String, Object> parameters = getContainerParameters(metadata, messages);
                        for (KieServerExtension extension : disposedExtensions) {
                            extension.createContainer(containerId, kci, parameters);
                            logger.debug("Container {} (for release id {}) {} restart: DONE", containerId, kci.getResource().getReleaseId(), extension);
                        }
                        kci.setStatus(KieContainerStatus.STARTED);
                        context.registerContainer(containerId, kci);
                        logger.info("Container {} (for release id {}) STARTED after failed dispose", containerId, kci.getResource().getReleaseId());
                        messages.add(new Message(Severity.WARN, "Error disposing container '" + containerId + "' due to " + e.getMessage() + ", container is running"));
                        return new ServiceResponse<Void>(ResponseType.FAILURE, "Container " + containerId + " failed to dispose, exception was raised: " + e.getClass().getName() + ": " + e.getMessage());
                    }
                    if (kci.getScanner() != null && kci.getScanner().getStatus() != Status.SHUTDOWN) {
                        kci.stopScanner();
                    }
                    InternalKieContainer kieContainer = kci.getKieContainer();
                    // helps reduce concurrent access issues
                    kci.setKieContainer(null);
                    // this may fail, but we already removed the container from the registry
                    kieContainer.dispose();
                    ks.getRepository().removeKieModule(releaseId);
                    logger.info("Container {} (for release id {}) successfully stopped", containerId, kci.getResource().getReleaseId());
                    // store the current state of the server
                    storeServerState(currentState -> {
                        List<KieContainerResource> containers = new ArrayList<>();
                        currentState.getContainers().forEach(containerResource -> {
                            if (!containerId.equals(containerResource.getContainerId())) {
                                containers.add(containerResource);
                            }
                        });
                        currentState.setContainers(new HashSet<KieContainerResource>(containers));
                    });
                    messages.add(new Message(Severity.INFO, "Container " + containerId + " successfully stopped."));
                    eventSupport.fireAfterContainerStopped(this, kci);
                    notifyStatusToControllers();
                    return new ServiceResponse<Void>(ServiceResponse.ResponseType.SUCCESS, "Container " + containerId + " successfully disposed.");
                } else {
                    messages.add(new Message(Severity.INFO, "Container " + containerId + " was not instantiated."));
                    return new ServiceResponse<Void>(ServiceResponse.ResponseType.SUCCESS, "Container " + containerId + " was not instantiated.");
                }
            }
        } else {
            messages.add(new Message(Severity.INFO, "Container " + containerId + " was not instantiated."));
            return new ServiceResponse<Void>(ServiceResponse.ResponseType.SUCCESS, "Container " + containerId + " was not instantiated.");
        }
    } catch (Exception e) {
        messages.add(new Message(Severity.ERROR, "Error disposing container '" + containerId + "' due to " + e.getMessage()));
        logger.error("Error disposing Container '" + containerId + "'", e);
        return new ServiceResponse<Void>(ServiceResponse.ResponseType.FAILURE, "Error disposing container " + containerId + ": " + e.getClass().getName() + ": " + e.getMessage());
    } finally {
        this.containerMessages.put(containerId, messages);
    }
}
Also used : Message(org.kie.server.api.model.Message) KieServerExtension(org.kie.server.services.api.KieServerExtension) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) KieModuleMetaData(org.kie.scanner.KieModuleMetaData) ServiceResponse(org.kie.server.api.model.ServiceResponse) InternalKieContainer(org.drools.core.impl.InternalKieContainer) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KieContainerResource(org.kie.server.api.model.KieContainerResource) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with KieServerExtension

use of org.kie.server.services.api.KieServerExtension in project droolsjbpm-integration by kiegroup.

the class TaskAssigningRuntimeKieServerExtension method init.

@Override
public void init(KieServerImpl kieServer, KieServerRegistry registry) {
    this.registry = registry;
    // JAXB and JSON required.
    registry.getExtraClasses().add(LocalDateTimeValue.class);
    KieServerExtension jbpmExtension = registry.getServerExtension(JbpmKieServerExtension.EXTENSION_NAME);
    if (jbpmExtension == null) {
        throw new KieServicesException(MISSING_REQUIRED_JBPM_EXTENSION_ERROR);
    }
    configureServices(kieServer, registry);
    services.add(taskAssigningRuntimeServiceBase);
    try {
        registerQueries();
    } catch (Exception e) {
        throw new KieServicesException(String.format(QUERIES_INITIALIZATION_ERROR, e.getMessage()), e);
    }
    initialized = true;
}
Also used : JbpmKieServerExtension(org.kie.server.services.jbpm.JbpmKieServerExtension) KieServerExtension(org.kie.server.services.api.KieServerExtension) KieServicesException(org.kie.server.api.exception.KieServicesException) KieServicesException(org.kie.server.api.exception.KieServicesException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with KieServerExtension

use of org.kie.server.services.api.KieServerExtension in project droolsjbpm-integration by kiegroup.

the class TaskAssigningRuntimeKieServerExtension method configureServices.

private void configureServices(KieServerImpl kieServer, KieServerRegistry registry) {
    KieServerExtension jbpmExtension = registry.getServerExtension(JbpmKieServerExtension.EXTENSION_NAME);
    List<Object> jbpmServices = jbpmExtension.getServices();
    UserTaskService userTaskService = null;
    for (Object object : jbpmServices) {
        // in case given service is null (meaning was not configured) continue with next one
        if (object != null) {
            if (UserTaskService.class.isAssignableFrom(object.getClass())) {
                userTaskService = (UserTaskService) object;
            } else if (QueryService.class.isAssignableFrom(object.getClass())) {
                queryService = (QueryService) object;
            }
        }
    }
    taskAssigningRuntimeServiceBase = new TaskAssigningRuntimeServiceBase(kieServer, registry, userTaskService, queryService);
}
Also used : QueryService(org.jbpm.services.api.query.QueryService) JbpmKieServerExtension(org.kie.server.services.jbpm.JbpmKieServerExtension) KieServerExtension(org.kie.server.services.api.KieServerExtension) UserTaskService(org.jbpm.services.api.UserTaskService)

Example 4 with KieServerExtension

use of org.kie.server.services.api.KieServerExtension in project droolsjbpm-integration by kiegroup.

the class TaskAssigningPlanningKieServerExtension method init.

/**
 * Invoked by the KieServer initialization procedure for performing current extension initialization.
 */
@Override
public void init(KieServerImpl kieServer, KieServerRegistry registry) {
    LOGGER.debug("Initializing " + EXTENSION_NAME + " extension.");
    this.kieServer = kieServer;
    this.registry = registry;
    KieServerExtension jbpmExtension = registry.getServerExtension(JbpmKieServerExtension.EXTENSION_NAME);
    if (jbpmExtension != null) {
        String msg = String.format(UNDESIRED_EXTENSIONS_RUNNING_ERROR, JbpmKieServerExtension.EXTENSION_NAME);
        LOGGER.warn(msg);
        kieServer.addServerMessage(new Message(Severity.WARN, addExtensionMessagePrefix(msg)));
    }
    initRuntimeClient();
    try {
        validateAndSetSolverConfiguration();
    } catch (TaskAssigningValidationException e) {
        throw new KieServicesException(String.format(SOLVER_CONFIGURATION_ERROR, e.getMessage()), e);
    }
    try {
        validateAndSetUserSystemServiceConfiguration();
    } catch (TaskAssigningValidationException e) {
        throw new KieServicesException(String.format(USER_SYSTEM_CONFIGURATION_ERROR, e.getMessage()), e);
    }
    this.executorService = Executors.newScheduledThreadPool(4);
    try {
        this.taskAssigningService = createTaskAssigningService(readAndValidateTaskAssigningServiceConfig());
        this.services.add(taskAssigningService);
    } catch (TaskAssigningValidationException e) {
        throw new KieServicesException(String.format(String.format(TASK_ASSIGNING_SERVICE_CONFIGURATION_ERROR, e.getMessage()), e));
    }
    this.initialized = true;
}
Also used : Message(org.kie.server.api.model.Message) JbpmKieServerExtension(org.kie.server.services.jbpm.JbpmKieServerExtension) KieServerExtension(org.kie.server.services.api.KieServerExtension) KieServicesException(org.kie.server.api.exception.KieServicesException)

Example 5 with KieServerExtension

use of org.kie.server.services.api.KieServerExtension in project droolsjbpm-integration by kiegroup.

the class JBPMClusterKieServerExtension method init.

@Override
public void init(KieServerImpl kieServer, KieServerRegistry registry) {
    this.registry = registry;
    KieServerExtension jbpmExtension = registry.getServerExtension("jBPM");
    if (jbpmExtension == null) {
        initialized = false;
        logger.warn("jBPM extension not found, jBPM Cluster cannot work without jBPM extension, disabling itself");
        return;
    }
    configureServices();
    // this implements fail over for jobs
    ExecutorServiceImpl service = (ExecutorServiceImpl) jbpmExecutorService;
    ClusteredJobFailOverListener clusteredJobFailOverListener = new ClusteredJobFailOverListener(clusterService, service);
    service.addAsyncJobListener(clusteredJobFailOverListener);
    clusterService.addClusterListener(clusteredJobFailOverListener);
    initialized = true;
}
Also used : ExecutorServiceImpl(org.jbpm.executor.impl.ExecutorServiceImpl) KieServerExtension(org.kie.server.services.api.KieServerExtension)

Aggregations

KieServerExtension (org.kie.server.services.api.KieServerExtension)43 Message (org.kie.server.api.model.Message)10 ArrayList (java.util.ArrayList)9 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)9 ReleaseId (org.kie.server.api.model.ReleaseId)8 Test (org.junit.Test)6 KieModuleMetaData (org.kie.scanner.KieModuleMetaData)6 KieContainerResource (org.kie.server.api.model.KieContainerResource)6 ServiceResponse (org.kie.server.api.model.ServiceResponse)6 Matchers.anyString (org.mockito.Matchers.anyString)6 JbpmKieServerExtension (org.kie.server.services.jbpm.JbpmKieServerExtension)5 DeploymentService (org.jbpm.services.api.DeploymentService)4 ProcessService (org.jbpm.services.api.ProcessService)4 KieContainerInstance (org.kie.server.services.api.KieContainerInstance)4 KieServerRegistry (org.kie.server.services.api.KieServerRegistry)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ExecutorServiceImpl (org.jbpm.executor.impl.ExecutorServiceImpl)3 RuntimeDataService (org.jbpm.services.api.RuntimeDataService)3