Search in sources :

Example 46 with InternalRuntimeManager

use of org.kie.internal.runtime.manager.InternalRuntimeManager in project jbpm by kiegroup.

the class FormProviderServiceImpl method getMarshallerContext.

protected ContentMarshallerContext getMarshallerContext(String deploymentId, String processId) {
    DeployedUnit deployedUnit = deploymentService.getDeployedUnit(deploymentId);
    if (deployedUnit == null) {
        return new ContentMarshallerContext();
    }
    InternalRuntimeManager manager = (InternalRuntimeManager) deployedUnit.getRuntimeManager();
    return new ContentMarshallerContext(manager.getEnvironment().getEnvironment(), manager.getEnvironment().getClassLoader());
}
Also used : InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) DeployedUnit(org.jbpm.services.api.model.DeployedUnit) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext)

Example 47 with InternalRuntimeManager

use of org.kie.internal.runtime.manager.InternalRuntimeManager in project jbpm by kiegroup.

the class RuntimeManagerWithDescriptorTest method testDeployWithDefaultDeploymentDescriptor.

@Test
public void testDeployWithDefaultDeploymentDescriptor() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.jbpm.test.dd", "kjar-with-dd", "1.0.0");
    String processString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-ScriptTask.bpmn2"), "UTF-8");
    Map<String, String> resources = new HashMap<String, String>();
    resources.put("src/main/resources/BPMN2-ScriptTask.bpmn2", processString);
    InternalKieModule kJar1 = createKieJar(ks, releaseId, resources);
    installKjar(releaseId, kJar1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder(releaseId).userGroupCallback(userGroupCallback).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    assertNotNull(manager);
    InternalRuntimeManager internalManager = (InternalRuntimeManager) manager;
    DeploymentDescriptor descriptor = internalManager.getDeploymentDescriptor();
    assertNotNull(descriptor);
    RegisterableItemsFactory factory = internalManager.getEnvironment().getRegisterableItemsFactory();
    assertNotNull(factory);
    assertTrue(factory instanceof InternalRegisterableItemsFactory);
    assertNotNull(((InternalRegisterableItemsFactory) factory).getRuntimeManager());
    String descriptorFromKjar = descriptor.toXml();
    DeploymentDescriptorManager ddManager = new DeploymentDescriptorManager();
    String defaultDescriptor = ddManager.getDefaultDescriptor().toXml();
    assertEquals(defaultDescriptor, descriptorFromKjar);
}
Also used : RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) HashMap(java.util.HashMap) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) InternalRegisterableItemsFactory(org.kie.internal.runtime.manager.InternalRegisterableItemsFactory) InternalRegisterableItemsFactory(org.kie.internal.runtime.manager.InternalRegisterableItemsFactory) RegisterableItemsFactory(org.kie.api.runtime.manager.RegisterableItemsFactory) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 48 with InternalRuntimeManager

use of org.kie.internal.runtime.manager.InternalRuntimeManager in project jbpm by kiegroup.

the class RuntimeManagerWithDescriptorTest method testDeployWithCustomDeploymentDescriptor.

@Test
public void testDeployWithCustomDeploymentDescriptor() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.jbpm.test.dd", "-kjar-with-dd", "1.0.0");
    DeploymentDescriptor customDescriptor = new DeploymentDescriptorImpl("org.jbpm.persistence.jpa");
    customDescriptor.getBuilder().runtimeStrategy(RuntimeStrategy.PER_REQUEST).addGlobal(new NamedObjectModel("service", "java.util.ArrayList"));
    String processString = IOUtils.toString(this.getClass().getResourceAsStream("/BPMN2-ScriptTask.bpmn2"), "UTF-8");
    Map<String, String> resources = new HashMap<String, String>();
    resources.put("src/main/resources/BPMN2-ScriptTask.bpmn2", processString);
    resources.put("src/main/resources/" + DeploymentDescriptor.META_INF_LOCATION, customDescriptor.toXml());
    String drl = "package org.jbpm; global java.util.List service; " + "	rule \"Start Hello1\"" + "	  when" + "	  then" + "	    System.out.println(\"Hello\");" + "	end";
    resources.put("src/main/resources/simple.drl", drl);
    InternalKieModule kJar1 = createKieJar(ks, releaseId, resources);
    installKjar(releaseId, kJar1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder(releaseId).userGroupCallback(userGroupCallback).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    assertNotNull(manager);
    InternalRuntimeManager internalManager = (InternalRuntimeManager) manager;
    RegisterableItemsFactory factory = internalManager.getEnvironment().getRegisterableItemsFactory();
    assertNotNull(factory);
    assertTrue(factory instanceof InternalRegisterableItemsFactory);
    assertNotNull(((InternalRegisterableItemsFactory) factory).getRuntimeManager());
    DeploymentDescriptor descriptor = internalManager.getDeploymentDescriptor();
    assertNotNull(descriptor);
    assertEquals("org.jbpm.persistence.jpa", descriptor.getPersistenceUnit());
    assertEquals("org.jbpm.persistence.jpa", descriptor.getAuditPersistenceUnit());
    assertEquals(AuditMode.JPA, descriptor.getAuditMode());
    assertEquals(PersistenceMode.JPA, descriptor.getPersistenceMode());
    assertEquals(RuntimeStrategy.PER_REQUEST, descriptor.getRuntimeStrategy());
    assertEquals(0, descriptor.getMarshallingStrategies().size());
    assertEquals(0, descriptor.getConfiguration().size());
    assertEquals(0, descriptor.getEnvironmentEntries().size());
    assertEquals(0, descriptor.getEventListeners().size());
    assertEquals(1, descriptor.getGlobals().size());
    assertEquals(0, descriptor.getTaskEventListeners().size());
    assertEquals(0, descriptor.getWorkItemHandlers().size());
    assertEquals(0, descriptor.getRequiredRoles().size());
    RuntimeEngine engine = manager.getRuntimeEngine(EmptyContext.get());
    assertNotNull(engine);
    Object service = engine.getKieSession().getGlobal("service");
    assertNotNull(service);
    assertTrue(service instanceof ArrayList);
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) HashMap(java.util.HashMap) DeploymentDescriptor(org.kie.internal.runtime.conf.DeploymentDescriptor) ArrayList(java.util.ArrayList) InternalRegisterableItemsFactory(org.kie.internal.runtime.manager.InternalRegisterableItemsFactory) RegisterableItemsFactory(org.kie.api.runtime.manager.RegisterableItemsFactory) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) NamedObjectModel(org.kie.internal.runtime.conf.NamedObjectModel) InternalRegisterableItemsFactory(org.kie.internal.runtime.manager.InternalRegisterableItemsFactory) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 49 with InternalRuntimeManager

use of org.kie.internal.runtime.manager.InternalRuntimeManager in project jbpm by kiegroup.

the class SingletonRuntimeManagerTest method testTimerStartWithDeactivate.

@Test(timeout = 10000)
public void testTimerStartWithDeactivate() {
    final NodeLeftCountDownProcessEventListener countDownListener = new NodeLeftCountDownProcessEventListener("Hello", 1);
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-TimerStart.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public List<ProcessEventListener> getProcessEventListeners(RuntimeEngine runtime) {
            List<ProcessEventListener> listeners = super.getProcessEventListeners(runtime);
            listeners.add(countDownListener);
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    assertNotNull(manager);
    countDownListener.waitTillCompleted();
    RuntimeEngine runtime1 = manager.getRuntimeEngine(EmptyContext.get());
    List<? extends ProcessInstanceLog> logs = runtime1.getAuditService().findProcessInstances();
    assertEquals(1, logs.size());
    manager.disposeRuntimeEngine(runtime1);
    ((InternalRuntimeManager) manager).deactivate();
    countDownListener.reset(1);
    countDownListener.waitTillCompleted(2000);
    runtime1 = manager.getRuntimeEngine(EmptyContext.get());
    logs = runtime1.getAuditService().findProcessInstances();
    assertEquals(1, logs.size());
    manager.disposeRuntimeEngine(runtime1);
    ((InternalRuntimeManager) manager).activate();
    countDownListener.reset(1);
    countDownListener.waitTillCompleted();
    runtime1 = manager.getRuntimeEngine(EmptyContext.get());
    logs = runtime1.getAuditService().findProcessInstances();
    assertEquals(2, logs.size());
    manager.disposeRuntimeEngine(runtime1);
}
Also used : NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) ProcessEventListener(org.kie.api.event.process.ProcessEventListener) DefaultProcessEventListener(org.kie.api.event.process.DefaultProcessEventListener) NodeLeftCountDownProcessEventListener(org.jbpm.test.listener.NodeLeftCountDownProcessEventListener) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 50 with InternalRuntimeManager

use of org.kie.internal.runtime.manager.InternalRuntimeManager in project jbpm by kiegroup.

the class MigrationManager method migrate.

/**
 * Performs migration with node mapping (if non null).
 * @param nodeMapping node instance mapping that is composed of unique ids of source node mapped to target node
 * @return returns migration report describing complete migration process.
 */
public MigrationReport migrate(Map<String, String> nodeMapping) {
    validate();
    KieSession current = null;
    KieSession tobe = null;
    TransactionManager txm = null;
    boolean transactionOwner = false;
    InternalRuntimeManager currentManager = (InternalRuntimeManager) RuntimeManagerRegistry.get().getManager(migrationSpec.getDeploymentId());
    InternalRuntimeManager toBeManager = (InternalRuntimeManager) RuntimeManagerRegistry.get().getManager(migrationSpec.getToDeploymentId());
    Map<Long, List<TimerInstance>> timerMigrated = null;
    try {
        // collect and cancel any active timers before migration
        timerMigrated = cancelActiveTimersBeforeMigration(currentManager);
        // start transaction to secure consistency of the migration
        txm = TransactionManagerFactory.get().newTransactionManager(currentManager.getEnvironment().getEnvironment());
        transactionOwner = txm.begin();
        org.kie.api.definition.process.Process toBeProcess = toBeManager.getEnvironment().getKieBase().getProcess(migrationSpec.getToProcessId());
        String auditPu = currentManager.getDeploymentDescriptor().getAuditPersistenceUnit();
        EntityManagerFactory emf = EntityManagerFactoryManager.get().getOrCreate(auditPu);
        EntityManager em = emf.createEntityManager();
        try {
            // update variable instance log information with new deployment id and process id
            Query varLogQuery = em.createQuery("update VariableInstanceLog set externalId = :depId, processId = :procId where processInstanceId = :procInstanceId");
            varLogQuery.setParameter("depId", migrationSpec.getToDeploymentId()).setParameter("procId", migrationSpec.getToProcessId()).setParameter("procInstanceId", migrationSpec.getProcessInstanceId());
            int varsUpdated = varLogQuery.executeUpdate();
            report.addEntry(Type.INFO, "Variable instances updated = " + varsUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
            // update node instance log information with new deployment id and process id
            Query nodeLogQuery = em.createQuery("update NodeInstanceLog set externalId = :depId, processId = :procId where processInstanceId = :procInstanceId");
            nodeLogQuery.setParameter("depId", migrationSpec.getToDeploymentId()).setParameter("procId", migrationSpec.getToProcessId()).setParameter("procInstanceId", migrationSpec.getProcessInstanceId());
            int nodesUpdated = nodeLogQuery.executeUpdate();
            report.addEntry(Type.INFO, "Node instances updated = " + nodesUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
            // update process instance log with new deployment and process id
            Query pInstanceLogQuery = em.createQuery("update ProcessInstanceLog set externalId = :depId, processId = :procId, processName = :procName, processVersion= :procVersion where processInstanceId = :procInstanceId");
            pInstanceLogQuery.setParameter("depId", migrationSpec.getToDeploymentId()).setParameter("procId", migrationSpec.getToProcessId()).setParameter("procName", toBeProcess.getName()).setParameter("procVersion", toBeProcess.getVersion()).setParameter("procInstanceId", migrationSpec.getProcessInstanceId());
            int pInstancesUpdated = pInstanceLogQuery.executeUpdate();
            report.addEntry(Type.INFO, "Process instances updated = " + pInstancesUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
            try {
                // update task audit instance log with new deployment and process id
                Query taskVarLogQuery = em.createQuery("update TaskVariableImpl set processId = :procId where processInstanceId = :procInstanceId");
                taskVarLogQuery.setParameter("procId", migrationSpec.getToProcessId()).setParameter("procInstanceId", migrationSpec.getProcessInstanceId());
                int taskVarUpdated = taskVarLogQuery.executeUpdate();
                report.addEntry(Type.INFO, "Task variables updated = " + taskVarUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
            } catch (Throwable e) {
                logger.warn("Unexpected error during migration", e);
                report.addEntry(Type.WARN, "Cannot update task variables (added in version 6.3) due to " + e.getMessage());
            }
            // update task audit instance log with new deployment and process id
            Query auditTaskLogQuery = em.createQuery("update AuditTaskImpl set deploymentId = :depId, processId = :procId where processInstanceId = :procInstanceId");
            auditTaskLogQuery.setParameter("depId", migrationSpec.getToDeploymentId()).setParameter("procId", migrationSpec.getToProcessId()).setParameter("procInstanceId", migrationSpec.getProcessInstanceId());
            int auditTaskUpdated = auditTaskLogQuery.executeUpdate();
            report.addEntry(Type.INFO, "Task audit updated = " + auditTaskUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
            // update task  instance log with new deployment and process id
            Query taskLogQuery = em.createQuery("update TaskImpl set deploymentId = :depId, processId = :procId where processInstanceId = :procInstanceId");
            taskLogQuery.setParameter("depId", migrationSpec.getToDeploymentId()).setParameter("procId", migrationSpec.getToProcessId()).setParameter("procInstanceId", migrationSpec.getProcessInstanceId());
            int taskUpdated = taskLogQuery.executeUpdate();
            report.addEntry(Type.INFO, "Tasks updated = " + taskUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
            try {
                // update context mapping info with new deployment
                Query contextInfoQuery = em.createQuery("update ContextMappingInfo set ownerId = :depId where contextId = :procInstanceId");
                contextInfoQuery.setParameter("depId", migrationSpec.getToDeploymentId()).setParameter("procInstanceId", migrationSpec.getProcessInstanceId().toString());
                int contextInfoUpdated = contextInfoQuery.executeUpdate();
                report.addEntry(Type.INFO, "Context info updated = " + contextInfoUpdated + " for process instance id " + migrationSpec.getProcessInstanceId());
            } catch (Throwable e) {
                logger.warn("Unexpected error during migration", e);
                report.addEntry(Type.WARN, "Cannot update context mapping owner (added in version 6.2) due to " + e.getMessage());
            }
            current = JPAKnowledgeService.newStatefulKnowledgeSession(currentManager.getEnvironment().getKieBase(), null, currentManager.getEnvironment().getEnvironment());
            tobe = JPAKnowledgeService.newStatefulKnowledgeSession(toBeManager.getEnvironment().getKieBase(), null, toBeManager.getEnvironment().getEnvironment());
            upgradeProcessInstance(current, tobe, migrationSpec.getProcessInstanceId(), migrationSpec.getToProcessId(), nodeMapping, em, toBeManager.getIdentifier());
            if (!timerMigrated.isEmpty()) {
                rescheduleTimersAfterMigration(toBeManager, timerMigrated);
            }
            em.flush();
        } finally {
            em.clear();
            em.close();
        }
        txm.commit(transactionOwner);
        report.addEntry(Type.INFO, "Migration of process instance (" + migrationSpec.getProcessInstanceId() + ") completed successfully to process " + migrationSpec.getToProcessId());
        report.setSuccessful(true);
        report.setEndDate(new Date());
    } catch (Throwable e) {
        txm.rollback(transactionOwner);
        logger.error("Unexpected error during migration", e);
        // put back timers (if there are any) in case of rollback
        if (timerMigrated != null && !timerMigrated.isEmpty()) {
            rescheduleTimersAfterMigration(currentManager, timerMigrated);
        }
        report.addEntry(Type.ERROR, "Migration of process instance (" + migrationSpec.getProcessInstanceId() + ") failed due to " + e.getMessage());
    } finally {
        if (current != null) {
            try {
                current.destroy();
            } catch (SessionNotFoundException e) {
            // in case of rollback session might not exist
            }
        }
        if (tobe != null) {
            try {
                tobe.destroy();
            } catch (SessionNotFoundException e) {
            // in case of rollback session might not exist
            }
        }
    }
    return report;
}
Also used : InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) Query(javax.persistence.Query) Date(java.util.Date) EntityManager(javax.persistence.EntityManager) TransactionManager(org.drools.persistence.api.TransactionManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) SessionNotFoundException(org.drools.persistence.api.SessionNotFoundException)

Aggregations

InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)51 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)29 Test (org.junit.Test)23 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)23 DeployedUnit (org.jbpm.services.api.model.DeployedUnit)17 HashMap (java.util.HashMap)14 KieSession (org.kie.api.runtime.KieSession)14 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)14 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)12 ArrayList (java.util.ArrayList)9 AbstractKieServicesBaseTest (org.jbpm.kie.test.util.AbstractKieServicesBaseTest)9 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)9 DeploymentDescriptor (org.kie.internal.runtime.conf.DeploymentDescriptor)8 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)7 DeploymentNotFoundException (org.jbpm.services.api.DeploymentNotFoundException)7 KieServices (org.kie.api.KieServices)7 ReleaseId (org.kie.api.builder.ReleaseId)7 TaskService (org.kie.api.task.TaskService)7 InternalTaskService (org.kie.internal.task.api.InternalTaskService)7 NamedObjectModel (org.kie.internal.runtime.conf.NamedObjectModel)6