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