use of org.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.
the class RuntimeManagerFactoryImpl method newPerCaseRuntimeManager.
public RuntimeManager newPerCaseRuntimeManager(RuntimeEnvironment environment, String identifier) {
SessionFactory factory = getSessionFactory(environment, identifier);
TaskServiceFactory taskServiceFactory = getTaskServiceFactory(environment);
RuntimeManager manager = new PerCaseRuntimeManager(environment, factory, taskServiceFactory, identifier);
initTimerService(environment, manager);
((AbstractRuntimeManager) manager).init();
return manager;
}
use of org.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.
the class DefaultRegisterableItemsFactory method getParametersMap.
protected Map<String, Object> getParametersMap(RuntimeEngine runtime) {
RuntimeManager manager = ((RuntimeEngineImpl) runtime).getManager();
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("ksession", runtime.getKieSession());
try {
parameters.put("taskService", runtime.getTaskService());
} catch (UnsupportedOperationException e) {
// in case task service was not configured
}
parameters.put("runtimeManager", manager);
parameters.put("classLoader", getRuntimeManager().getEnvironment().getClassLoader());
parameters.put("entityManagerFactory", runtime.getKieSession().getEnvironment().get(EnvironmentName.ENTITY_MANAGER_FACTORY));
parameters.put("kieContainer", getRuntimeManager().getKieContainer());
return parameters;
}
use of org.kie.api.runtime.manager.RuntimeManager 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.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.
the class JMSSignalReceiver method onMessage.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void onMessage(Message message) {
if (message instanceof BytesMessage) {
String deploymentId;
Long processInstanceId;
String signal;
Long workItemId;
Object data;
BytesMessage bytesMessage = (BytesMessage) message;
RuntimeManager runtimeManager = null;
RuntimeEngine engine = null;
try {
deploymentId = (String) bytesMessage.getObjectProperty("KIE_SignalDeploymentId");
if (deploymentId == null) {
deploymentId = (String) bytesMessage.getObjectProperty("KIE_DeploymentId");
}
signal = (String) bytesMessage.getObjectProperty("KIE_Signal");
processInstanceId = (Long) bytesMessage.getObjectProperty("KIE_SignalProcessInstanceId");
workItemId = (Long) bytesMessage.getObjectProperty("KIE_SignalWorkItemId");
logger.debug("Deployment id '{}', signal '{}', processInstanceId '{}', workItemId '{}'", deploymentId, signal, processInstanceId, workItemId);
Collection<String> availableRuntimeManagers = matchDeployments(deploymentId, RuntimeManagerRegistry.get().getRegisteredIdentifiers());
for (String matchedDeploymentId : availableRuntimeManagers) {
try {
runtimeManager = RuntimeManagerRegistry.get().getManager(matchedDeploymentId);
if (runtimeManager == null) {
throw new IllegalStateException("There is no runtime manager for deployment " + matchedDeploymentId);
}
logger.debug("RuntimeManager found for deployment id {}, reading message content with custom class loader of the deployment", matchedDeploymentId);
data = readData(bytesMessage, ((InternalRuntimeManager) runtimeManager).getEnvironment().getClassLoader());
logger.debug("Data read successfully with output {}", data);
engine = runtimeManager.getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId));
// perform operation either signal or complete work item
if (workItemId != null) {
Map<String, Object> results = new HashMap<String, Object>();
if (data != null) {
if (data instanceof Map) {
results.putAll((Map) data);
} else {
results.put("Data", data);
}
}
logger.debug("About to complete work item with id {} and data {}", workItemId, results);
engine.getKieSession().getWorkItemManager().completeWorkItem(workItemId, results);
logger.debug("Successfully completed work item with id {}", workItemId);
} else if (signal != null) {
if (processInstanceId != null) {
logger.debug("About to signal process instance with id {} and event data {} with signal {}", processInstanceId, data, signal);
engine.getKieSession().signalEvent(signal, data, processInstanceId);
} else {
logger.debug("About to broadcast signal {} and event data {}", signal, data);
runtimeManager.signalEvent(signal, data);
}
logger.debug("Signal completed successfully for signal {} with data {}", signal, data);
} else {
logger.warn("No signal or workitem id is given, skipping this message");
}
} catch (Exception e) {
logger.error("Unexpected exception while signaling: {}", e.getMessage(), e);
} finally {
if (runtimeManager != null && engine != null) {
runtimeManager.disposeRuntimeEngine(engine);
}
}
}
} catch (Exception e) {
logger.error("Unexpected exception while processing signal JMS message: {}", e.getMessage(), e);
}
}
}
use of org.kie.api.runtime.manager.RuntimeManager in project jbpm by kiegroup.
the class PerRequestRuntimeManagerTest method testMultiplePerRequestManagerFromSingleThread.
@Test
public void testMultiplePerRequestManagerFromSingleThread() {
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-IntermediateCatchEventSignalWithRef.bpmn2"), ResourceType.BPMN2).get();
manager = RuntimeManagerFactory.Factory.get().newPerRequestRuntimeManager(environment, "first");
assertNotNull(manager);
RuntimeEnvironment environment2 = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2).get();
RuntimeManager manager2 = RuntimeManagerFactory.Factory.get().newPerRequestRuntimeManager(environment2, "second");
assertNotNull(manager2);
// start first process instance with first manager
RuntimeEngine runtime1 = manager.getRuntimeEngine(EmptyContext.get());
KieSession ksession1 = runtime1.getKieSession();
assertNotNull(ksession1);
ProcessInstance processInstance = ksession1.startProcess("IntermediateCatchEventWithRef");
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
// start another process instance of the same process just owned by another manager
RuntimeEngine runtime2 = manager2.getRuntimeEngine(EmptyContext.get());
KieSession ksession2 = runtime2.getKieSession();
assertNotNull(ksession2);
ProcessInstance processInstance2 = ksession2.startProcess("UserTask");
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance2.getState());
manager.disposeRuntimeEngine(runtime1);
manager.disposeRuntimeEngine(runtime2);
// close manager which will close session maintained by the manager
manager.close();
manager2.close();
}
Aggregations