Search in sources :

Example 41 with RuntimeManager

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;
}
Also used : InMemorySessionFactory(org.jbpm.runtime.manager.impl.factory.InMemorySessionFactory) SessionFactory(org.kie.internal.runtime.manager.SessionFactory) JPASessionFactory(org.jbpm.runtime.manager.impl.factory.JPASessionFactory) LocalTaskServiceFactory(org.jbpm.runtime.manager.impl.factory.LocalTaskServiceFactory) TaskServiceFactory(org.kie.internal.runtime.manager.TaskServiceFactory) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager)

Example 42 with RuntimeManager

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;
}
Also used : HashMap(java.util.HashMap) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager)

Example 43 with RuntimeManager

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);
}
Also used : CommandContext(org.kie.api.executor.CommandContext) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) ExecutorService(org.kie.api.executor.ExecutorService) ProcessPersistenceContextManager(org.jbpm.persistence.api.ProcessPersistenceContextManager) AsyncSignalEventCommand(org.jbpm.process.core.async.AsyncSignalEventCommand) ProcessPersistenceContext(org.jbpm.persistence.api.ProcessPersistenceContext)

Example 44 with RuntimeManager

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);
        }
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) HashMap(java.util.HashMap) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) BytesMessage(javax.jms.BytesMessage) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) JMSException(javax.jms.JMSException)

Example 45 with RuntimeManager

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();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)150 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)116 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)79 KieSession (org.kie.api.runtime.KieSession)55 TaskService (org.kie.api.task.TaskService)53 Test (org.junit.Test)51 HashMap (java.util.HashMap)49 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)46 InternalTaskService (org.kie.internal.task.api.InternalTaskService)44 DeployedUnit (org.jbpm.services.api.model.DeployedUnit)38 UserTaskService (org.jbpm.services.api.UserTaskService)36 UserTaskInstanceDesc (org.jbpm.services.api.model.UserTaskInstanceDesc)35 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)23 TaskSummary (org.kie.api.task.model.TaskSummary)21 ArrayList (java.util.ArrayList)19 KModuleDeploymentUnit (org.jbpm.kie.services.impl.KModuleDeploymentUnit)19 DeploymentNotFoundException (org.jbpm.services.api.DeploymentNotFoundException)18 TaskNotFoundException (org.jbpm.services.api.TaskNotFoundException)15 DeploymentUnit (org.jbpm.services.api.model.DeploymentUnit)15 PermissionDeniedException (org.jbpm.services.task.exception.PermissionDeniedException)15