Refine search
@Override public Void execute(org.kie.api.runtime.Context context) { TransactionManager tm = (TransactionManager) initialKsession.getEnvironment().get(EnvironmentName.TRANSACTION_MANAGER); if (manager.hasEnvironmentEntry("IS_JTA_TRANSACTION", false)) { if (initialKsession instanceof CommandBasedStatefulKnowledgeSession) { ExecutableRunner commandService = ((CommandBasedStatefulKnowledgeSession) initialKsession).getRunner(); ((SingleSessionCommandService) commandService).destroy(); } else { ((RegistryContext) context).lookup( KieSession.class ).destroy(); && tm.getStatus() != TransactionManager.STATUS_ROLLEDBACK && tm.getStatus() != TransactionManager.STATUS_COMMITTED) { TransactionManagerHelper.registerTransactionSyncInContainer(tm, new OrderedTransactionSynchronization(5, "PPIRM-"+initialKsession.getIdentifier()) {
@Test public void testInterceptor() { String str = ""; str += "package org.kie.test\n"; str += "global java.util.List list\n"; str += "rule rule1\n"; str += "when\n"; str += " Integer(intValue > 0)\n"; str += "then\n"; str += " list.add( 1 );\n"; str += " System.out.println(\"ADDED\");\n"; str += "end\n"; str += "\n"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem().write( "src/main/resources/r1.drl", str ); ks.newKieBuilder( kfs ).buildAll(); KieBase kbase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase(); KieSession ksession = InfinispanKnowledgeService.newStatefulKnowledgeSession( kbase, null, env ); PersistableRunner sscs = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner(); sscs.addInterceptor(new LoggingInterceptor()); sscs.addInterceptor(new FireAllRulesInterceptor()); sscs.addInterceptor(new LoggingInterceptor()); List<?> list = new ArrayList<Object>(); ksession.setGlobal( "list", list ); ksession.insert( 1 ); ksession.insert( 2 ); ksession.insert( 3 ); ksession.getWorkItemManager().completeWorkItem(0, null); assertEquals( 3, list.size() ); }
@Test @RequirePersistence public void testRuleTaskWithFactsWithPersistence() throws Exception { KieBase kbase = createKnowledgeBaseWithoutDumper("BPMN2-RuleTaskWithFact.bpmn2", "BPMN2-RuleTask3.drl"); ksession = createKnowledgeSession(kbase); ( (SingleSessionCommandService) ( (CommandBasedStatefulKnowledgeSession) ksession ).getRunner() ).getKieSession() .addEventListener(new TriggerRulesEventListener(ksession)); ksession.addEventListener(new DebugAgendaEventListener()); Map<String, Object> params = new HashMap<String, Object>(); params.put("x", "SomeString"); ProcessInstance processInstance = ksession.startProcess("RuleTask", params); assertProcessInstanceFinished(processInstance, ksession); params = new HashMap<String, Object>(); try { processInstance = ksession.startProcess("RuleTask", params); fail("Should fail"); } catch (Exception e) { e.printStackTrace(); } params = new HashMap<String, Object>(); params.put("x", "SomeString"); processInstance = ksession.startProcess("RuleTask", params); assertProcessInstanceFinished(processInstance, ksession); }
@Test @Ignore("beta4 phreak") public void testStartTimerCycleFromDiscDRL() throws Exception { KieBase kbase = createKnowledgeBaseFromDisc("rules-timer.drl"); StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); long sessionId = ksession.getIdentifier(); Environment env = ksession.getEnvironment(); final List<String> list = new ArrayList<String>(); ksession.setGlobal("list", list); ( (SingleSessionCommandService) ( (CommandBasedStatefulKnowledgeSession) ksession ).getRunner() ).getKieSession() .addEventListener(new TriggerRulesEventListener(ksession)); ksession.fireAllRules(); Thread.sleep(5000); assertEquals(2, list.size()); logger.info("dispose"); ksession.dispose(); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env); AuditLoggerFactory.newInstance(Type.JPA, ksession, null); final List<String> list2 = new ArrayList<String>(); ksession.setGlobal("list", list2); ( (SingleSessionCommandService) ( (CommandBasedStatefulKnowledgeSession) ksession ).getRunner() ).getKieSession() .addEventListener(new TriggerRulesEventListener(ksession)); ksession.fireAllRules(); Thread.sleep(6000); assertEquals(3, list2.size()); }
KieSession ksession = runtime.getKieSession(); ProcessInstance processInstance = ksession.startProcess("UserTaskWithRollback"); ExecutableRunner commandService = ((CommandBasedStatefulKnowledgeSession)ksession).getRunner(); assertEquals( PersistableRunner.class, commandService.getClass() );
private KieRuntime extractIfNeeded(KieRuntime ksession) { if (ksession instanceof CommandBasedStatefulKnowledgeSession) { return ((SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner()).getKieSession(); } return ksession; }
protected void addInterceptors(KieSession ksession) { PersistableRunner runner = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner(); runner.addInterceptor(new OptimisticLockRetryInterceptor()); // even though it's added always TransactionLockInterceptor is by default disabled so won't do anything runner.addInterceptor(new TransactionLockInterceptor(ksession.getEnvironment())); runner.addInterceptor(new ExecutionErrorHandlerInterceptor(ksession.getEnvironment())); }
public SynchronizedTaskService(KieSession ksession, InternalTaskService taskService) { if (ksession instanceof CommandBasedStatefulKnowledgeSession) { this.ksession = ((CommandBasedStatefulKnowledgeSession) ksession).getRunner(); } else { this.ksession = ksession; } this.taskService = taskService; }
public long getStatefulKnowledgeSessionId(StatefulKnowledgeSession ksession) { if ( ksession instanceof CommandBasedStatefulKnowledgeSession ) { SingleSessionCommandService commandService = (SingleSessionCommandService) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner(); return commandService.getSessionId(); } throw new IllegalArgumentException( "StatefulKnowledgeSession must be an a CommandBasedStatefulKnowledgeSession" ); }
public StatefulKnowledgeSession loadKieSession(int id, KieBase kbase, KieSessionConfiguration configuration, Environment environment) { if ( configuration == null ) { configuration = SessionConfiguration.newInstance(); } if ( environment == null ) { throw new IllegalArgumentException( "Environment cannot be null" ); } ExecutableRunner runner = (ExecutableRunner) buildCommandService( new Long( id), kbase, mergeConfig( configuration ), environment ); return new CommandBasedStatefulKnowledgeSession( runner ); }
@Test public void testRuleFlowGroupRollback() throws Exception { CommandBasedStatefulKnowledgeSession ksession = createSession(); List<String> list = new ArrayList<String>(); list.add("Test"); ksession.insert(list); ksession.execute(new ActivateRuleFlowCommand("ruleflow-group")); assertEquals(1, ksession.fireAllRules()); try { ksession.execute(new ExceptionCommand()); fail("Process must throw an exception"); } catch (Exception e) { e.printStackTrace(); } ksession.insert(list); ksession.execute(new ActivateRuleFlowCommand("ruleflow-group")); assertEquals(1, ksession.fireAllRules()); }
public ClassLoader getClassLoader(CommandExecutor exec) { ClassLoader cl = null; if (exec instanceof StatefulKnowledgeSessionImpl) { cl = ((StatefulKnowledgeSessionImpl)exec).getKnowledgeBase().getRootClassLoader(); } else if (exec instanceof StatelessKnowledgeSessionImpl) { cl = ((StatelessKnowledgeSessionImpl)exec).getKnowledgeBase().getRootClassLoader(); } else if (exec instanceof CommandBasedStatefulKnowledgeSession) { cl = ((InternalKnowledgeBase)((CommandBasedStatefulKnowledgeSession)exec).getKieBase()).getRootClassLoader(); } return cl; }
@Test @RequirePersistence public void testRuleTaskWithFactsWithPersistence() throws Exception { KieBase kbase = createKnowledgeBaseWithoutDumper("BPMN2-RuleTaskWithFact.bpmn2", "BPMN2-RuleTask3.drl"); ksession = createKnowledgeSession(kbase); ( (SingleSessionCommandService) ( (CommandBasedStatefulKnowledgeSession) ksession ).getRunner() ).getKieSession() .addEventListener(new TriggerRulesEventListener(ksession)); ksession.addEventListener(new DebugAgendaEventListener()); Map<String, Object> params = new HashMap<String, Object>(); params.put("x", "SomeString"); ProcessInstance processInstance = ksession.startProcess("RuleTask", params); assertProcessInstanceFinished(processInstance, ksession); params = new HashMap<String, Object>(); try { processInstance = ksession.startProcess("RuleTask", params); fail("Should fail"); } catch (Exception e) { e.printStackTrace(); } params = new HashMap<String, Object>(); params.put("x", "SomeString"); processInstance = ksession.startProcess("RuleTask", params); assertProcessInstanceFinished(processInstance, ksession); }
@Test @Ignore("beta4 phreak") public void testStartTimerCycleFromClasspathDRL() throws Exception { KieBase kbase = createKnowledgeBaseWithoutDumper("rules-timer.drl"); StatefulKnowledgeSession ksession = createKnowledgeSession(kbase); long sessionId = ksession.getIdentifier(); Environment env = ksession.getEnvironment(); final List<String> list = new ArrayList<String>(); ksession.setGlobal("list", list); ( (SingleSessionCommandService) ( (CommandBasedStatefulKnowledgeSession) ksession ).getRunner() ).getKieSession() .addEventListener(new TriggerRulesEventListener(ksession)); ksession.fireAllRules(); Thread.sleep(5000); assertEquals(2, list.size()); logger.info("dispose"); ksession.dispose(); ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionId, kbase, null, env); AuditLoggerFactory.newInstance(Type.JPA, ksession, null); final List<String> list2 = new ArrayList<String>(); ksession.setGlobal("list", list2); ( (SingleSessionCommandService) ( (CommandBasedStatefulKnowledgeSession) ksession ).getRunner() ).getKieSession() .addEventListener(new TriggerRulesEventListener(ksession)); ksession.fireAllRules(); Thread.sleep(5000); assertEquals(3, list2.size()); }
protected TimerManager getTimerManager(KieSession ksession) { KieSession internal = ksession; if (ksession instanceof CommandBasedStatefulKnowledgeSession) { internal = ( (SingleSessionCommandService) ( (CommandBasedStatefulKnowledgeSession) ksession ).getRunner() ).getKieSession(); } return ((InternalProcessRuntime) ((StatefulKnowledgeSessionImpl) internal).getProcessRuntime()).getTimerManager(); }
protected void addInterceptors(KieSession ksession) { PersistableRunner runner = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner(); runner.addInterceptor(new OptimisticLockRetryInterceptor()); // even though it's added always TransactionLockInterceptor is by default disabled so won't do anything runner.addInterceptor(new TransactionLockInterceptor(ksession.getEnvironment())); runner.addInterceptor(new ExecutionErrorHandlerInterceptor(ksession.getEnvironment())); }
subProcessNodeInstance); } else if (ksession instanceof CommandBasedStatefulKnowledgeSession) { ExecutableRunner commandService = ((CommandBasedStatefulKnowledgeSession) ksession).getRunner(); return commandService.execute(new ExecutableCommand<Long>() { private static final long serialVersionUID = 5L;
public StatefulKnowledgeSession newKieSession(KieBase kbase, KieSessionConfiguration configuration, Environment environment) { if ( configuration == null ) { configuration = SessionConfiguration.newInstance(); } if ( environment == null ) { throw new IllegalArgumentException( "Environment cannot be null" ); } ExecutableRunner runner = (ExecutableRunner) buildCommandService( kbase, mergeConfig( configuration ), environment ); return new CommandBasedStatefulKnowledgeSession( runner ); }