use of org.drools.core.runtime.ChainableRunner in project drools by kiegroup.
the class JpaPersistentStatefulSessionTest method interceptorOnRollback.
private void interceptorOnRollback(final boolean withOOPath) throws Exception {
final String str = getSimpleRule(withOOPath);
final KieBase kbase = new KieHelper().addContent(str, ResourceType.DRL).build();
final KieSession ksession = KieServices.get().getStoreServices().newKieSession(kbase, null, env);
final PersistableRunner sscs = (PersistableRunner) ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
sscs.addInterceptor(new LoggingInterceptor());
sscs.addInterceptor(new FireAllRulesInterceptor());
sscs.addInterceptor(new LoggingInterceptor());
ChainableRunner runner = sscs.getChainableRunner();
assertThat(runner.getClass()).isEqualTo(LoggingInterceptor.class);
runner = (ChainableRunner) runner.getNext();
assertThat(runner.getClass()).isEqualTo(FireAllRulesInterceptor.class);
runner = (ChainableRunner) runner.getNext();
assertThat(runner.getClass()).isEqualTo(LoggingInterceptor.class);
final UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
ut.begin();
final List<?> list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.insert(1);
ut.rollback();
ksession.insert(3);
runner = sscs.getChainableRunner();
assertThat(runner.getClass()).isEqualTo(LoggingInterceptor.class);
runner = (ChainableRunner) runner.getNext();
assertThat(runner.getClass()).isEqualTo(FireAllRulesInterceptor.class);
runner = (ChainableRunner) runner.getNext();
assertThat(runner.getClass()).isEqualTo(LoggingInterceptor.class);
}
use of org.drools.core.runtime.ChainableRunner in project drools by kiegroup.
the class PersistableRunner method initExistingKnowledgeSession.
protected void initExistingKnowledgeSession(Long sessionId, KieBase kbase, KieSessionConfiguration conf, PersistenceContext persistenceContext) {
if (!doRollback && this.ksession != null) {
return;
// nothing to initialise
}
this.doRollback = false;
try {
// if locking is active, this will also lock the (found) SessionInfo instance
this.sessionInfo = (SessionInfo) persistenceContext.findSession(sessionId);
} catch (Exception e) {
throw new SessionNotFoundException("Could not find session data for id " + sessionId, e);
}
if (sessionInfo == null) {
throw new SessionNotFoundException("Could not find session data for id " + sessionId);
}
if (this.marshallingHelper == null) {
// this should only happen when this class is first constructed
this.marshallingHelper = new SessionMarshallingHelper(kbase, conf, env);
MarshallingConfigurationImpl config = (MarshallingConfigurationImpl) this.marshallingHelper.getMarshaller().getMarshallingConfiguration();
config.setMarshallProcessInstances(false);
config.setMarshallWorkItems(false);
}
this.sessionInfo.setJPASessionMashallingHelper(this.marshallingHelper);
// if this.ksession is null, it'll create a new one, else it'll use the existing one
this.ksession = this.marshallingHelper.loadSnapshot(this.sessionInfo.getData(), this.ksession, new JpaSessionInitializer(this));
// update the session id to be the same as the session info id
InternalKnowledgeRuntime kruntime = ((InternalKnowledgeRuntime) ksession);
kruntime.setIdentifier(this.sessionInfo.getId());
kruntime.setEndOperationListener(new EndOperationListenerImpl(this.txm, this.sessionInfo));
this.runner = new TransactionInterceptor();
// apply interceptors
Iterator<ChainableRunner> iterator = this.interceptors.descendingIterator();
while (iterator.hasNext()) {
addInterceptor(iterator.next(), false);
}
initKieSessionMBeans(this.ksession);
}
use of org.drools.core.runtime.ChainableRunner in project jbpm by kiegroup.
the class HumanTaskConfigurator method addOptimisticLockInterceptor.
@SuppressWarnings("unchecked")
protected void addOptimisticLockInterceptor() {
// add default interceptor if present
try {
Class<ChainableRunner> defaultInterceptorClass = (Class<ChainableRunner>) Class.forName(OPTIMISTIC_LOCK_INTERCEPTOR);
Constructor<ChainableRunner> constructor = defaultInterceptorClass.getConstructor(new Class[] {});
ChainableRunner defaultInterceptor = constructor.newInstance();
interceptor(7, defaultInterceptor);
} catch (Exception e) {
logger.warn("No optimistic lock interceptor found of type {} might be mssing drools-persistence-jpa module on classpath (error {}", OPTIMISTIC_LOCK_INTERCEPTOR, e.getMessage(), e);
}
}
use of org.drools.core.runtime.ChainableRunner in project jbpm by kiegroup.
the class SingletonRuntimeManagerTest method testInterceptorAfterRollback.
@Test
public void testInterceptorAfterRollback() throws Exception {
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTaskWithRollback.bpmn2"), ResourceType.BPMN2).get();
manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
KieSession ksession = runtime.getKieSession();
ProcessInstance processInstance = ksession.startProcess("UserTaskWithRollback");
ExecutableRunner commandService = ((CommandBasedStatefulKnowledgeSession) ksession).getRunner();
assertEquals(PersistableRunner.class, commandService.getClass());
ChainableRunner internalCommandService = ((PersistableRunner) commandService).getChainableRunner();
assertEquals(ExecutionErrorHandlerInterceptor.class, internalCommandService.getClass());
internalCommandService = (ChainableRunner) ((ExecutionErrorHandlerInterceptor) internalCommandService).getNext();
assertEquals(TransactionLockInterceptor.class, internalCommandService.getClass());
TaskService taskService = runtime.getTaskService();
List<Long> taskIds = taskService.getTasksByProcessInstanceId(processInstance.getId());
taskService.start(taskIds.get(0), "john");
HashMap<String, Object> result = new HashMap<String, Object>();
result.put("output1", "rollback");
try {
// rollback transaction
taskService.complete(taskIds.get(0), "john", result);
} catch (WorkflowRuntimeException e) {
// ignore
}
result = new HashMap<String, Object>();
result.put("output1", "ok");
// this time, execute normally
taskService.complete(taskIds.get(0), "john", result);
internalCommandService = ((PersistableRunner) commandService).getChainableRunner();
assertEquals(ExecutionErrorHandlerInterceptor.class, internalCommandService.getClass());
internalCommandService = (ChainableRunner) ((ExecutionErrorHandlerInterceptor) internalCommandService).getNext();
assertEquals(TransactionLockInterceptor.class, internalCommandService.getClass());
internalCommandService = (ChainableRunner) ((TransactionLockInterceptor) internalCommandService).getNext();
assertEquals(OptimisticLockRetryInterceptor.class, internalCommandService.getClass());
internalCommandService = (ChainableRunner) ((OptimisticLockRetryInterceptor) internalCommandService).getNext();
assertEquals("org.drools.persistence.PersistableRunner$TransactionInterceptor", internalCommandService.getClass().getName());
// close manager which will close session maintained by the manager
manager.close();
}
use of org.drools.core.runtime.ChainableRunner in project jbpm by kiegroup.
the class HumanTaskConfigurator method addTransactionLockInterceptor.
@SuppressWarnings("unchecked")
protected void addTransactionLockInterceptor() {
// add default interceptor if present
try {
Class<ChainableRunner> defaultInterceptorClass = (Class<ChainableRunner>) Class.forName(TX_LOCK_INTERCEPTOR);
Constructor<ChainableRunner> constructor = defaultInterceptorClass.getConstructor(new Class[] { Environment.class, String.class });
ChainableRunner defaultInterceptor = constructor.newInstance(this.environment, "task-service-tx-unlock");
interceptor(6, defaultInterceptor);
} catch (Exception e) {
logger.warn("No tx lock interceptor found of type {} might be mssing drools-persistence-jpa module on classpath (error {}", TX_LOCK_INTERCEPTOR, e.getMessage(), e);
}
}
Aggregations