Search in sources :

Example 1 with PerCaseRuntimeManager

use of org.jbpm.runtime.manager.impl.PerCaseRuntimeManager in project jbpm by kiegroup.

the class CancelCaseCommand method execute.

@Override
public Void execute(Context context) {
    CorrelationKey correlationKey = correlationKeyFactory.newCorrelationKey(caseId);
    Collection<ProcessInstanceDesc> caseProcesses = runtimeDataService.getProcessInstancesByCorrelationKey(correlationKey, new QueryContext(0, 1000));
    if (caseProcesses.isEmpty()) {
        throw new CaseNotFoundException("Case with id " + caseId + " was not found");
    }
    List<Long> processInstanceIds = caseProcesses.stream().filter(pi -> pi.getState().equals(ProcessInstance.STATE_ACTIVE)).sorted((ProcessInstanceDesc o1, ProcessInstanceDesc o2) -> {
        return Long.valueOf(o2.getParentId()).compareTo(Long.valueOf(o1.getParentId()));
    }).map(pi -> pi.getId()).collect(toList());
    CaseEventSupport caseEventSupport = getCaseEventSupport(context);
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    CaseFileInstance caseFile = getCaseFile(ksession, caseId);
    caseEventSupport.fireBeforeCaseCancelled(caseId, caseFile, processInstanceIds);
    logger.debug("Case {} consists of following process instances (ids) {}", caseId, processInstanceIds);
    processService.abortProcessInstances(processInstanceIds);
    caseEventSupport.fireAfterCaseCancelled(caseId, caseFile, processInstanceIds);
    if (destroy) {
        RuntimeManager runtimeManager = getRuntimeManager(context);
        if (runtimeManager instanceof PerCaseRuntimeManager) {
            caseEventSupport.fireBeforeCaseDestroyed(caseId, caseFile, processInstanceIds);
            logger.debug("Case {} aborted, destroying case data including per case runtime engine (including working memory)", caseId);
            ((PerCaseRuntimeManager) runtimeManager).destroyCase(CaseContext.get(caseId));
            caseEventSupport.fireAfterCaseDestroyed(caseId, caseFile, processInstanceIds);
        }
    }
    return null;
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) RegistryContext(org.drools.core.command.impl.RegistryContext) Logger(org.slf4j.Logger) ProcessService(org.jbpm.services.api.ProcessService) CorrelationKey(org.kie.internal.process.CorrelationKey) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) QueryContext(org.kie.api.runtime.query.QueryContext) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Context(org.kie.api.runtime.Context) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) RuntimeDataService(org.jbpm.services.api.RuntimeDataService) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) KieInternalServices(org.kie.internal.KieInternalServices) KieSession(org.kie.api.runtime.KieSession) CorrelationKeyFactory(org.kie.internal.process.CorrelationKeyFactory) CaseContext(org.kie.internal.runtime.manager.context.CaseContext) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) ProcessInstanceDesc(org.jbpm.services.api.model.ProcessInstanceDesc) RegistryContext(org.drools.core.command.impl.RegistryContext) QueryContext(org.kie.api.runtime.query.QueryContext) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CorrelationKey(org.kie.internal.process.CorrelationKey) KieSession(org.kie.api.runtime.KieSession)

Example 2 with PerCaseRuntimeManager

use of org.jbpm.runtime.manager.impl.PerCaseRuntimeManager in project jbpm by kiegroup.

the class StartCaseWorkItemHandler method executeWorkItem.

@Override
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    String deploymentId = (String) workItem.getParameter(DEPLOYMENT_ID);
    if (deploymentId == null) {
        deploymentId = ((org.drools.core.process.instance.WorkItem) workItem).getDeploymentId();
    }
    RuntimeManager targetRuntimeManager = RuntimeManagerRegistry.get().getManager(deploymentId);
    if (targetRuntimeManager == null || !(targetRuntimeManager instanceof PerCaseRuntimeManager)) {
        throw new IllegalArgumentException("Requested target deployment does not exist or is not per case strategy");
    }
    String caseDefinitionId = (String) workItem.getParameter(CASE_DEFINITION_ID);
    if (caseDefinitionId == null || caseDefinitionId.trim().isEmpty()) {
        throw new IllegalArgumentException(CASE_DEFINITION_ID + " is a required parameter for StartCaseWorkItemHandler");
    }
    Map<String, Object> caseFileData = new HashMap<>();
    Map<String, List<String>> accessRestrictions = new HashMap<>();
    Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
    parseParameters(workItem, caseFileData, roleAssignments, accessRestrictions);
    long processInstanceId = ((WorkItemImpl) workItem).getProcessInstanceId();
    long workItemId = workItem.getId();
    logger.debug("Parent process instance id {} and work item instance id {} for new case instance", processInstanceId, workItemId);
    CaseService caseService = (CaseService) ServiceRegistry.get().service(ServiceRegistry.CASE_SERVICE);
    CaseFileInstance subCaseFile = caseService.newCaseFileInstanceWithRestrictions(deploymentId, caseDefinitionId, caseFileData, roleAssignments, accessRestrictions);
    ((CaseFileInstanceImpl) subCaseFile).setParentInstanceId(processInstanceId);
    ((CaseFileInstanceImpl) subCaseFile).setParentWorkItemId(workItemId);
    String caseId = caseService.startCase(deploymentId, caseDefinitionId, subCaseFile);
    logger.debug("Case with id {} has been successfully started");
    boolean independent = Boolean.parseBoolean((String) workItem.getParameter(INDEPENDENT));
    if (independent) {
        Map<String, Object> results = new HashMap<>();
        results.put(CASE_ID, caseId);
        try {
            CaseFileInstance snapshot = caseService.getCaseFileInstance(caseId);
            results.putAll(snapshot.getData());
        } catch (CaseNotFoundException e) {
            // case is already completed
            logger.debug("Case is already completed, not possible to fetch case file data any more");
        }
        logger.debug("Completing directly (without waiting for case instance {} completion) work item with id {}", caseId, workItem.getId());
        ((CaseFileInstanceImpl) subCaseFile).setParentInstanceId(null);
        ((CaseFileInstanceImpl) subCaseFile).setParentWorkItemId(null);
        manager.completeWorkItem(workItem.getId(), results);
    } else {
        // save case id so the abort work item can abort/destroy the case instance
        ((WorkItemImpl) workItem).setParameter(CASE_ID, caseId);
        logger.debug("Waiting for case instance {} completion before completing work item with id {}", caseId, workItem.getId());
    }
}
Also used : PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) ArrayList(java.util.ArrayList) List(java.util.List) CaseService(org.jbpm.casemgmt.api.CaseService)

Example 3 with PerCaseRuntimeManager

use of org.jbpm.runtime.manager.impl.PerCaseRuntimeManager in project jbpm by kiegroup.

the class CaseConfigurationDeploymentListener method onDeploy.

@Override
public void onDeploy(DeploymentEvent event) {
    InternalRuntimeManager runtimeManager = (InternalRuntimeManager) event.getDeployedUnit().getRuntimeManager();
    if (runtimeManager instanceof PerCaseRuntimeManager) {
        List<CaseEventListener> caseEventListeners = getEventListenerFromDescriptor(runtimeManager);
        logger.debug("Adding following case event listeners {} for deployment {}", caseEventListeners, event.getDeploymentId());
        TransactionalCommandService commandService = transactionalCommandService;
        if (commandService == null) {
            commandService = new TransactionalCommandService(((SimpleRuntimeEnvironment) runtimeManager.getEnvironment()).getEmf());
        }
        CaseInstanceAuditEventListener auditEventListener = new CaseInstanceAuditEventListener(commandService);
        caseEventListeners.add(auditEventListener);
        caseEventListeners.add(new NotifyParentCaseEventListener(identityProvider));
        CaseEventSupport caseEventSupport = new CaseEventSupport(identityProvider, caseEventListeners);
        ((PerCaseRuntimeManager) runtimeManager).setCaseEventSupport(caseEventSupport);
        logger.debug("CaseEventSupport configured for deployment {}", event.getDeploymentId());
    }
}
Also used : SimpleRuntimeEnvironment(org.jbpm.runtime.manager.impl.SimpleRuntimeEnvironment) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) CaseInstanceAuditEventListener(org.jbpm.casemgmt.impl.audit.CaseInstanceAuditEventListener) TransactionalCommandService(org.jbpm.shared.services.impl.TransactionalCommandService) NotifyParentCaseEventListener(org.jbpm.casemgmt.impl.wih.NotifyParentCaseEventListener) CaseEventListener(org.jbpm.casemgmt.api.event.CaseEventListener) NotifyParentCaseEventListener(org.jbpm.casemgmt.impl.wih.NotifyParentCaseEventListener)

Example 4 with PerCaseRuntimeManager

use of org.jbpm.runtime.manager.impl.PerCaseRuntimeManager in project jbpm by kiegroup.

the class CaseConfigurationDeploymentListener method onUnDeploy.

@Override
public void onUnDeploy(DeploymentEvent event) {
    InternalRuntimeManager runtimeManager = (InternalRuntimeManager) event.getDeployedUnit().getRuntimeManager();
    if (runtimeManager instanceof PerCaseRuntimeManager) {
        CaseEventSupport caseEventSupport = (CaseEventSupport) ((PerCaseRuntimeManager) runtimeManager).getCaseEventSupport();
        if (caseEventSupport != null) {
            caseEventSupport.reset();
            logger.debug("CaseEventSupport disposed for deployment {}", event.getDeploymentId());
        }
    }
}
Also used : InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager)

Example 5 with PerCaseRuntimeManager

use of org.jbpm.runtime.manager.impl.PerCaseRuntimeManager in project jbpm by kiegroup.

the class InMemorySessionFactoryLeakTest method testInMemorySessionCleanup.

@Test
public void testInMemorySessionCleanup() {
    for (int i = 0; i < 20; i++) {
        Context<?> context = getContext();
        RuntimeEngine runtime = manager.getRuntimeEngine(context);
        KieSession ksession = runtime.getKieSession();
        ProcessInstance pi = ksession.startProcess("ScriptTask");
        assertEquals(ProcessInstance.STATE_COMPLETED, pi.getState());
        manager.disposeRuntimeEngine(runtime);
        if ("case".equals(strategy)) {
            ((PerCaseRuntimeManager) manager).destroyCase((CaseContext) context);
        }
    }
    InMemorySessionFactory factory = (InMemorySessionFactory) ((AbstractRuntimeManager) manager).getFactory();
    int expectedSessionsInFactory = getExpectedSessionsInFactory();
    assertEquals(expectedSessionsInFactory, factory.getSessions().size());
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) PerCaseRuntimeManager(org.jbpm.runtime.manager.impl.PerCaseRuntimeManager) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

PerCaseRuntimeManager (org.jbpm.runtime.manager.impl.PerCaseRuntimeManager)5 List (java.util.List)2 CaseNotFoundException (org.jbpm.casemgmt.api.CaseNotFoundException)2 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)2 KieSession (org.kie.api.runtime.KieSession)2 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)2 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)2 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Collectors.toList (java.util.stream.Collectors.toList)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)1 CaseService (org.jbpm.casemgmt.api.CaseService)1 CaseEventListener (org.jbpm.casemgmt.api.event.CaseEventListener)1 CaseInstanceAuditEventListener (org.jbpm.casemgmt.impl.audit.CaseInstanceAuditEventListener)1 CaseEventSupport (org.jbpm.casemgmt.impl.event.CaseEventSupport)1 CaseFileInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl)1 NotifyParentCaseEventListener (org.jbpm.casemgmt.impl.wih.NotifyParentCaseEventListener)1