public Void execute(Context context) { em.joinTransaction(); em.persist(mainObject); if (subObject != null) { KieSession ksession = ((KnowledgeCommandContext) context).getKieSession(); // THe following 3 lines are the important ones! (See below for an explanation) KnowledgeBase cleanKBase = KnowledgeBaseFactory.newKnowledgeBase(); cleanKBase.addKnowledgePackages( ((KnowledgeBase) ksession.getKieBase()).getKnowledgePackages()); StatefulKnowledgeSession commandKSession = JPAKnowledgeService.newStatefulKnowledgeSession( cleanKBase, null, initializeEnvironment()); /** * Here's what's going on: If the SingleSessionCommandService (SSCS) & JtaTransactionManager * (JTM) were _not_ aware of transactions, -> then inserting the mainObject _before_ having * inserted the subObject would cause the operation/persist to fail and the transaction to * fail. - This is because the mainObject contains a foreign key referring to the subObject. * * <p>However, the SSCS & JTM check whether or not they're owners of the transaction when * starting and when committing the transaction they use. -> So that when we insert the * mainObject here (via a _new_ CommandBasedStatefulKnowledgeSession), it does _not_ mess with * the transaction state and the operation succeeds. */ TransactionTestCommand transactionTestSubCommand = new TransactionTestCommand(this.subObject, getPersistenceEnvironment()); commandKSession.execute(transactionTestSubCommand); } return null; }
@Override public Collection<String> getAvailableSignals(String businessKey, long processInstanceId) { ProcessInstanceDesc piDesc = dataService.getProcessInstanceById(processInstanceId); KieSession ksession = domainService.getSessionById(piDesc.getSessionId()); ProcessInstance processInstance = ksession.getProcessInstance(processInstanceId); Collection<String> activeSignals = new ArrayList<String>(); if (processInstance != null) { ((ProcessInstanceImpl) processInstance) .setProcess(ksession.getKieBase().getProcess(processInstance.getProcessId())); Collection<NodeInstance> activeNodes = ((WorkflowProcessInstance) processInstance).getNodeInstances(); activeSignals.addAll(ProcessInstanceHelper.collectActiveSignals(activeNodes)); } return activeSignals; }