use of org.kie.api.runtime.manager.RuntimeManager 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.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.
the class WorkflowProcessInstanceImpl method setState.
public void setState(final int state, String outcome) {
super.setState(state, outcome);
// TODO move most of this to ProcessInstanceImpl
if (state == ProcessInstance.STATE_COMPLETED || state == ProcessInstance.STATE_ABORTED) {
if (this.slaCompliance == SLA_PENDING) {
if (System.currentTimeMillis() > slaDueDate.getTime()) {
// completion of the process instance is after expected SLA due date, mark it accordingly
this.slaCompliance = SLA_VIOLATED;
} else {
this.slaCompliance = state == ProcessInstance.STATE_COMPLETED ? SLA_MET : SLA_ABORTED;
}
}
InternalKnowledgeRuntime kruntime = getKnowledgeRuntime();
InternalProcessRuntime processRuntime = (InternalProcessRuntime) kruntime.getProcessRuntime();
processRuntime.getProcessEventSupport().fireBeforeProcessCompleted(this, kruntime);
// deactivate all node instances of this process instance
while (!nodeInstances.isEmpty()) {
NodeInstance nodeInstance = nodeInstances.get(0);
((org.jbpm.workflow.instance.NodeInstance) nodeInstance).cancel();
}
if (this.slaTimerId > -1) {
processRuntime.getTimerManager().cancelTimer(this.slaTimerId);
logger.debug("SLA Timer {} has been canceled", this.slaTimerId);
}
removeEventListeners();
processRuntime.getProcessInstanceManager().removeProcessInstance(this);
processRuntime.getProcessEventSupport().fireAfterProcessCompleted(this, kruntime);
if (isSignalCompletion()) {
RuntimeManager manager = (RuntimeManager) kruntime.getEnvironment().get(EnvironmentName.RUNTIME_MANAGER);
if (getParentProcessInstanceId() > 0 && manager != null) {
try {
org.kie.api.runtime.manager.Context<?> context = ProcessInstanceIdContext.get(getParentProcessInstanceId());
String caseId = (String) kruntime.getEnvironment().get(EnvironmentName.CASE_ID);
if (caseId != null) {
context = CaseContext.get(caseId);
}
RuntimeEngine runtime = manager.getRuntimeEngine(context);
KieRuntime managedkruntime = (KieRuntime) runtime.getKieSession();
managedkruntime.signalEvent("processInstanceCompleted:" + getId(), this);
} catch (SessionNotFoundException e) {
// in case no session is found for parent process let's skip signal for process instance completion
}
} else {
processRuntime.getSignalManager().signalEvent("processInstanceCompleted:" + getId(), this);
}
}
}
}
use of org.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.
the class EvaluationExample method main.
public static final void main(String[] args) {
try {
RuntimeManager manager = getRuntimeManager("evaluation/Evaluation.bpmn");
RuntimeEngine runtime = manager.getRuntimeEngine(null);
KieSession ksession = runtime.getKieSession();
// start a new process instance
Map<String, Object> params = new HashMap<String, Object>();
params.put("employee", "krisv");
params.put("reason", "Yearly performance evaluation");
ksession.startProcess("com.sample.evaluation", params);
// complete Self Evaluation
TaskService taskService = runtime.getTaskService();
List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("krisv", "en-UK");
TaskSummary task = tasks.get(0);
System.out.println("'krisv' completing task " + task.getName() + ": " + task.getDescription());
taskService.start(task.getId(), "krisv");
Map<String, Object> results = new HashMap<String, Object>();
results.put("performance", "exceeding");
taskService.complete(task.getId(), "krisv", results);
// john from HR
tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
task = tasks.get(0);
System.out.println("'john' completing task " + task.getName() + ": " + task.getDescription());
taskService.start(task.getId(), "john");
results = new HashMap<String, Object>();
results.put("performance", "acceptable");
taskService.complete(task.getId(), "john", results);
// mary from PM
tasks = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
task = tasks.get(0);
System.out.println("'mary' completing task " + task.getName() + ": " + task.getDescription());
taskService.start(task.getId(), "mary");
results = new HashMap<String, Object>();
results.put("performance", "outstanding");
taskService.complete(task.getId(), "mary", results);
System.out.println("Process instance completed");
manager.disposeRuntimeEngine(runtime);
} catch (Throwable t) {
t.printStackTrace();
}
System.exit(0);
}
use of org.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.
the class MultipleInstanceExample method main.
public static final void main(String[] args) {
try {
RuntimeManager manager = getRuntimeManager("multipleinstance/multipleinstance.bpmn");
RuntimeEngine runtime = manager.getRuntimeEngine(null);
KieSession ksession = runtime.getKieSession();
// start a new process instance
Map<String, Object> params = new HashMap<String, Object>();
List<String> list = new ArrayList<String>();
list.add("krisv");
list.add("john doe");
list.add("superman");
params.put("list", list);
ksession.startProcess("com.sample.multipleinstance", params);
TaskService taskService = runtime.getTaskService();
List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("sales-rep", "en-UK");
for (TaskSummary task : tasks) {
System.out.println("Sales-rep executing task " + task.getName() + "(" + task.getId() + ": " + task.getDescription() + ")");
taskService.start(task.getId(), "sales-rep");
taskService.complete(task.getId(), "sales-rep", null);
}
manager.disposeRuntimeEngine(runtime);
} catch (Throwable t) {
t.printStackTrace();
}
System.exit(0);
}
use of org.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.
the class AbstractRuntimeEJBServicesTest method cleanup.
@After
@Override
public void cleanup() {
List<Long> pids = archive.getPids();
List<Long> all = (List<Long>) ((ArrayList<Long>) pids).clone();
for (Long pid : all) {
ProcessInstanceDesc pi = runtimeDataService.getProcessInstanceById(pid);
if (pi == null || pi.getState() != ProcessInstance.STATE_ACTIVE) {
pids.remove(pid);
}
}
if (!pids.isEmpty()) {
processService.abortProcessInstances(pids);
}
pids.clear();
cleanupSingletonSessionId();
List<DeploymentUnit> units = archive.getUnits();
if (units != null && !units.isEmpty()) {
for (DeploymentUnit unit : units) {
// clear audit logs
RuntimeManager manager = deploymentService.getRuntimeManager(unit.getIdentifier());
RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
engine.getAuditService().clear();
deploymentService.undeploy(unit);
}
units.clear();
}
kieJar = null;
}
Aggregations