use of org.jbpm.persistence.api.ProcessPersistenceContextManager in project jbpm by kiegroup.
the class JPASignalManager method signalEvent.
public void signalEvent(String type, Object event) {
String actualSignalType = type.replaceFirst(ASYNC_SIGNAL_PREFIX, "");
ProcessPersistenceContextManager contextManager = (ProcessPersistenceContextManager) getKnowledgeRuntime().getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
ProcessPersistenceContext context = contextManager.getProcessPersistenceContext();
List<Long> processInstancesToSignalList = context.getProcessInstancesWaitingForEvent(actualSignalType);
// handle signal asynchronously
if (type.startsWith(ASYNC_SIGNAL_PREFIX)) {
RuntimeManager runtimeManager = ((RuntimeManager) getKnowledgeRuntime().getEnvironment().get("RuntimeManager"));
ExecutorService executorService = (ExecutorService) getKnowledgeRuntime().getEnvironment().get("ExecutorService");
if (runtimeManager != null && executorService != null) {
for (Long processInstanceId : processInstancesToSignalList) {
CommandContext ctx = new CommandContext();
ctx.setData("deploymentId", runtimeManager.getIdentifier());
ctx.setData("processInstanceId", processInstanceId);
ctx.setData("Signal", actualSignalType);
ctx.setData("Event", event);
executorService.scheduleRequest(AsyncSignalEventCommand.class.getName(), ctx);
}
return;
} else {
logger.warn("Signal should be sent asynchronously but there is no executor service available, continuing sync...");
}
}
for (long id : processInstancesToSignalList) {
try {
getKnowledgeRuntime().getProcessInstance(id);
} catch (IllegalStateException e) {
// IllegalStateException can be thrown when using RuntimeManager
// and invalid ksession was used for given context
} catch (RuntimeException e) {
logger.warn("Exception when loading process instance for signal '{}', instance with id {} will not be signaled", e.getMessage(), id);
}
}
super.signalEvent(actualSignalType, event);
}
use of org.jbpm.persistence.api.ProcessPersistenceContextManager in project jbpm by kiegroup.
the class IntermediateEventTest method testEventTypesLifeCycle.
@Test
@RequirePersistence
public void testEventTypesLifeCycle() throws Exception {
// JBPM-4246
KieBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchSignalBetweenUserTasks.bpmn2");
EntityManagerFactory separateEmf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
Environment env = createEnvironment(separateEmf);
ksession = createKnowledgeSession(kbase, null, env);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new DoNothingWorkItemHandler());
ksession.startProcess("BPMN2-IntermediateCatchSignalBetweenUserTasks");
int signalListSize = ksession.execute(new ExecutableCommand<Integer>() {
public Integer execute(Context context) {
SingleSessionCommandService commandService = (SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
InternalKnowledgeRuntime kruntime = (InternalKnowledgeRuntime) commandService.getKieSession();
ProcessPersistenceContextManager contextManager = (ProcessPersistenceContextManager) kruntime.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
ProcessPersistenceContext pcontext = contextManager.getProcessPersistenceContext();
List<Long> processInstancesToSignalList = pcontext.getProcessInstancesWaitingForEvent("MySignal");
return processInstancesToSignalList.size();
}
});
// Process instance is not waiting for signal
assertThat(signalListSize).isEqualTo(0);
ksession.getWorkItemManager().completeWorkItem(1, null);
signalListSize = ksession.execute(new ExecutableCommand<Integer>() {
public Integer execute(Context context) {
SingleSessionCommandService commandService = (SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
InternalKnowledgeRuntime kruntime = (InternalKnowledgeRuntime) commandService.getKieSession();
ProcessPersistenceContextManager contextManager = (ProcessPersistenceContextManager) kruntime.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
ProcessPersistenceContext pcontext = contextManager.getProcessPersistenceContext();
List<Long> processInstancesToSignalList = pcontext.getProcessInstancesWaitingForEvent("MySignal");
return processInstancesToSignalList.size();
}
});
// Process instance is waiting for signal now
assertThat(signalListSize).isEqualTo(1);
ksession.signalEvent("MySignal", null);
signalListSize = ksession.execute(new ExecutableCommand<Integer>() {
public Integer execute(Context context) {
SingleSessionCommandService commandService = (SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
InternalKnowledgeRuntime kruntime = (InternalKnowledgeRuntime) commandService.getKieSession();
ProcessPersistenceContextManager contextManager = (ProcessPersistenceContextManager) kruntime.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
ProcessPersistenceContext pcontext = contextManager.getProcessPersistenceContext();
List<Long> processInstancesToSignalList = pcontext.getProcessInstancesWaitingForEvent("MySignal");
return processInstancesToSignalList.size();
}
});
// Process instance is not waiting for signal
assertThat(signalListSize).isEqualTo(0);
ksession.getWorkItemManager().completeWorkItem(2, null);
ksession.dispose();
ksession = null;
separateEmf.close();
}
use of org.jbpm.persistence.api.ProcessPersistenceContextManager in project jbpm by kiegroup.
the class JPAProcessInstanceManager method addProcessInstance.
public void addProcessInstance(ProcessInstance processInstance, CorrelationKey correlationKey) {
ProcessInstanceInfo processInstanceInfo = new ProcessInstanceInfo(processInstance, this.kruntime.getEnvironment());
ProcessPersistenceContext context = ((ProcessPersistenceContextManager) this.kruntime.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER)).getProcessPersistenceContext();
processInstanceInfo = (ProcessInstanceInfo) context.persist(processInstanceInfo);
((org.jbpm.process.instance.ProcessInstance) processInstance).setId(processInstanceInfo.getId());
processInstanceInfo.updateLastReadDate();
// generate correlation key if not given which is same as process instance id to keep uniqueness
if (correlationKey == null) {
correlationKey = new CorrelationKeyInfo();
((CorrelationKeyInfo) correlationKey).addProperty(new CorrelationPropertyInfo(null, processInstanceInfo.getId().toString()));
((org.jbpm.process.instance.ProcessInstance) processInstance).getMetaData().put("CorrelationKey", correlationKey);
}
CorrelationKeyInfo correlationKeyInfo = (CorrelationKeyInfo) correlationKey;
correlationKeyInfo.setProcessInstanceId(processInstanceInfo.getId());
context.persist(correlationKeyInfo);
internalAddProcessInstance(processInstance);
EventManagerProvider.getInstance().get().create(new ProcessInstanceView(processInstance));
}
use of org.jbpm.persistence.api.ProcessPersistenceContextManager in project jbpm by kiegroup.
the class JPAProcessInstanceManager method removeProcessInstance.
public void removeProcessInstance(ProcessInstance processInstance) {
ProcessPersistenceContext context = ((ProcessPersistenceContextManager) this.kruntime.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER)).getProcessPersistenceContext();
ProcessInstanceInfo processInstanceInfo = (ProcessInstanceInfo) context.findProcessInstanceInfo(processInstance.getId());
if (processInstanceInfo != null) {
context.remove(processInstanceInfo);
}
internalRemoveProcessInstance(processInstance);
EventManagerProvider.getInstance().get().delete(new ProcessInstanceView(processInstance));
}
use of org.jbpm.persistence.api.ProcessPersistenceContextManager in project jbpm by kiegroup.
the class JPAProcessInstanceManager method getProcessInstance.
public ProcessInstance getProcessInstance(long id, boolean readOnly) {
InternalRuntimeManager manager = (InternalRuntimeManager) kruntime.getEnvironment().get(EnvironmentName.RUNTIME_MANAGER);
if (manager != null) {
manager.validate((KieSession) kruntime, ProcessInstanceIdContext.get(id));
}
TransactionManager txm = (TransactionManager) this.kruntime.getEnvironment().get(EnvironmentName.TRANSACTION_MANAGER);
org.jbpm.process.instance.ProcessInstance processInstance = null;
processInstance = (org.jbpm.process.instance.ProcessInstance) this.processInstances.get(id);
if (processInstance != null) {
if (((WorkflowProcessInstanceImpl) processInstance).isPersisted() && !readOnly) {
ProcessPersistenceContextManager ppcm = (ProcessPersistenceContextManager) this.kruntime.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
ppcm.beginCommandScopedEntityManager();
ProcessPersistenceContext context = ppcm.getProcessPersistenceContext();
ProcessInstanceInfo processInstanceInfo = (ProcessInstanceInfo) context.findProcessInstanceInfo(id);
if (processInstanceInfo == null) {
return null;
}
TransactionManagerHelper.addToUpdatableSet(txm, processInstanceInfo);
processInstanceInfo.updateLastReadDate();
EventManagerProvider.getInstance().get().update(new ProcessInstanceView(processInstance));
}
return processInstance;
}
// Make sure that the cmd scoped entity manager has started
ProcessPersistenceContextManager ppcm = (ProcessPersistenceContextManager) this.kruntime.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
ppcm.beginCommandScopedEntityManager();
ProcessPersistenceContext context = ppcm.getProcessPersistenceContext();
ProcessInstanceInfo processInstanceInfo = (ProcessInstanceInfo) context.findProcessInstanceInfo(id);
if (processInstanceInfo == null) {
return null;
}
processInstance = (org.jbpm.process.instance.ProcessInstance) processInstanceInfo.getProcessInstance(kruntime, this.kruntime.getEnvironment(), readOnly);
if (!readOnly) {
processInstanceInfo.updateLastReadDate();
TransactionManagerHelper.addToUpdatableSet(txm, processInstanceInfo);
EventManagerProvider.getInstance().get().update(new ProcessInstanceView(processInstance));
}
if (((ProcessInstanceImpl) processInstance).getProcessXml() == null) {
Process process = kruntime.getKieBase().getProcess(processInstance.getProcessId());
if (process == null) {
throw new IllegalArgumentException("Could not find process " + processInstance.getProcessId());
}
processInstance.setProcess(process);
}
if (processInstance.getKnowledgeRuntime() == null) {
Long parentProcessInstanceId = (Long) ((ProcessInstanceImpl) processInstance).getMetaData().get("ParentProcessInstanceId");
if (parentProcessInstanceId != null) {
kruntime.getProcessInstance(parentProcessInstanceId);
}
processInstance.setKnowledgeRuntime(kruntime);
((ProcessInstanceImpl) processInstance).reconnect();
if (readOnly) {
internalRemoveProcessInstance(processInstance);
}
}
return processInstance;
}
Aggregations