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());
}
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);
}
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);
}
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);
}
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;
}
Aggregations