Search in sources :

Example 1 with ProcessPersistenceContextManager

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);
}
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 2 with ProcessPersistenceContextManager

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();
}
Also used : RegistryContext(org.drools.core.command.impl.RegistryContext) ProcessPersistenceContext(org.jbpm.persistence.api.ProcessPersistenceContext) Context(org.kie.api.runtime.Context) DoNothingWorkItemHandler(org.jbpm.process.instance.impl.demo.DoNothingWorkItemHandler) SingleSessionCommandService(org.drools.core.command.SingleSessionCommandService) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ProcessPersistenceContext(org.jbpm.persistence.api.ProcessPersistenceContext) InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) KieBase(org.kie.api.KieBase) EntityManagerFactory(javax.persistence.EntityManagerFactory) Environment(org.kie.api.runtime.Environment) ExecutableCommand(org.drools.core.command.impl.ExecutableCommand) List(java.util.List) ArrayList(java.util.ArrayList) ProcessPersistenceContextManager(org.jbpm.persistence.api.ProcessPersistenceContextManager) Test(org.junit.Test) RequirePersistence(org.jbpm.bpmn2.test.RequirePersistence)

Example 3 with ProcessPersistenceContextManager

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));
}
Also used : CorrelationPropertyInfo(org.jbpm.persistence.correlation.CorrelationPropertyInfo) CorrelationKeyInfo(org.jbpm.persistence.correlation.CorrelationKeyInfo) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkflowProcessInstance(org.kie.api.runtime.process.WorkflowProcessInstance) ProcessPersistenceContextManager(org.jbpm.persistence.api.ProcessPersistenceContextManager) ProcessPersistenceContext(org.jbpm.persistence.api.ProcessPersistenceContext) ProcessInstanceView(org.jbpm.persistence.api.integration.model.ProcessInstanceView)

Example 4 with ProcessPersistenceContextManager

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));
}
Also used : ProcessPersistenceContextManager(org.jbpm.persistence.api.ProcessPersistenceContextManager) ProcessPersistenceContext(org.jbpm.persistence.api.ProcessPersistenceContext) ProcessInstanceView(org.jbpm.persistence.api.integration.model.ProcessInstanceView)

Example 5 with ProcessPersistenceContextManager

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;
}
Also used : InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) ProcessInstanceImpl(org.jbpm.process.instance.impl.ProcessInstanceImpl) Process(org.kie.api.definition.process.Process) ProcessPersistenceContext(org.jbpm.persistence.api.ProcessPersistenceContext) ProcessInstanceView(org.jbpm.persistence.api.integration.model.ProcessInstanceView) TransactionManager(org.drools.persistence.api.TransactionManager) ProcessPersistenceContextManager(org.jbpm.persistence.api.ProcessPersistenceContextManager)

Aggregations

ProcessPersistenceContext (org.jbpm.persistence.api.ProcessPersistenceContext)6 ProcessPersistenceContextManager (org.jbpm.persistence.api.ProcessPersistenceContextManager)6 ProcessInstanceView (org.jbpm.persistence.api.integration.model.ProcessInstanceView)3 ArrayList (java.util.ArrayList)1 List (java.util.List)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 SingleSessionCommandService (org.drools.core.command.SingleSessionCommandService)1 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)1 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1 InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)1 TransactionManager (org.drools.persistence.api.TransactionManager)1 RequirePersistence (org.jbpm.bpmn2.test.RequirePersistence)1 CorrelationKeyInfo (org.jbpm.persistence.correlation.CorrelationKeyInfo)1 CorrelationPropertyInfo (org.jbpm.persistence.correlation.CorrelationPropertyInfo)1 AsyncSignalEventCommand (org.jbpm.process.core.async.AsyncSignalEventCommand)1 ProcessInstanceImpl (org.jbpm.process.instance.impl.ProcessInstanceImpl)1 DoNothingWorkItemHandler (org.jbpm.process.instance.impl.demo.DoNothingWorkItemHandler)1 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)1 Test (org.junit.Test)1