Search in sources :

Example 1 with ChainableRunner

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);
}
Also used : UserTransaction(javax.transaction.UserTransaction) ChainableRunner(org.drools.core.runtime.ChainableRunner) LoggingInterceptor(org.drools.core.command.impl.LoggingInterceptor) KieBase(org.kie.api.KieBase) FireAllRulesInterceptor(org.drools.core.command.impl.FireAllRulesInterceptor) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) PersistableRunner(org.drools.persistence.PersistableRunner)

Example 2 with ChainableRunner

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);
}
Also used : InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) MarshallingConfigurationImpl(org.drools.core.marshalling.impl.MarshallingConfigurationImpl) ChainableRunner(org.drools.core.runtime.ChainableRunner) SessionMarshallingHelper(org.drools.persistence.api.SessionMarshallingHelper) SessionNotFoundException(org.drools.persistence.api.SessionNotFoundException) SessionNotFoundException(org.drools.persistence.api.SessionNotFoundException)

Example 3 with ChainableRunner

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);
    }
}
Also used : ChainableRunner(org.drools.core.runtime.ChainableRunner)

Example 4 with ChainableRunner

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();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) HashMap(java.util.HashMap) TaskService(org.kie.api.task.TaskService) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ExecutableRunner(org.kie.api.runtime.ExecutableRunner) PersistableRunner(org.drools.persistence.PersistableRunner) ChainableRunner(org.drools.core.runtime.ChainableRunner) ExecutionErrorHandlerInterceptor(org.jbpm.runtime.manager.impl.error.ExecutionErrorHandlerInterceptor) OptimisticLockRetryInterceptor(org.drools.persistence.jpa.OptimisticLockRetryInterceptor) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) TransactionLockInterceptor(org.drools.persistence.jta.TransactionLockInterceptor) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 5 with ChainableRunner

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);
    }
}
Also used : ChainableRunner(org.drools.core.runtime.ChainableRunner)

Aggregations

ChainableRunner (org.drools.core.runtime.ChainableRunner)7 PersistableRunner (org.drools.persistence.PersistableRunner)2 KieSession (org.kie.api.runtime.KieSession)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 UserTransaction (javax.transaction.UserTransaction)1 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)1 FireAllRulesInterceptor (org.drools.core.command.impl.FireAllRulesInterceptor)1 LoggingInterceptor (org.drools.core.command.impl.LoggingInterceptor)1 InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)1 MarshallingConfigurationImpl (org.drools.core.marshalling.impl.MarshallingConfigurationImpl)1 SessionMarshallingHelper (org.drools.persistence.api.SessionMarshallingHelper)1 SessionNotFoundException (org.drools.persistence.api.SessionNotFoundException)1 OptimisticLockRetryInterceptor (org.drools.persistence.jpa.OptimisticLockRetryInterceptor)1 TransactionLockInterceptor (org.drools.persistence.jta.TransactionLockInterceptor)1 ExecutionErrorHandlerInterceptor (org.jbpm.runtime.manager.impl.error.ExecutionErrorHandlerInterceptor)1 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)1 WorkflowRuntimeException (org.jbpm.workflow.instance.WorkflowRuntimeException)1 Test (org.junit.Test)1 KieBase (org.kie.api.KieBase)1