Search in sources :

Example 1 with SessionInfo

use of org.drools.persistence.info.SessionInfo in project jbpm by kiegroup.

the class PersistenceRuntimeManagerTest method testPerRequestManagerDestorySession.

@SuppressWarnings("unchecked")
@Test
public void testPerRequestManagerDestorySession() {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2).get();
    EntityManager em = emf.createEntityManager();
    List<SessionInfo> sessions = em.createQuery("from SessionInfo").getResultList();
    assertEquals(0, sessions.size());
    manager = RuntimeManagerFactory.Factory.get().newPerRequestRuntimeManager(environment);
    assertNotNull(manager);
    sessions = em.createQuery("from SessionInfo").getResultList();
    assertEquals(0, sessions.size());
    // ksession for process instance #1
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    runtime.getKieSession();
    sessions = em.createQuery("from SessionInfo").getResultList();
    assertEquals(1, sessions.size());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    ProcessInstance pi1 = ksession.startProcess("UserTask");
    // both processes started
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    manager.disposeRuntimeEngine(runtime);
    sessions = em.createQuery("from SessionInfo").getResultList();
    assertEquals(0, sessions.size());
    runtime = manager.getRuntimeEngine(EmptyContext.get());
    ksession = runtime.getKieSession();
    sessions = em.createQuery("from SessionInfo").getResultList();
    assertEquals(1, sessions.size());
    ksession.getWorkItemManager().completeWorkItem(1, null);
    manager.disposeRuntimeEngine(runtime);
    sessions = em.createQuery("from SessionInfo").getResultList();
    assertEquals(0, sessions.size());
    manager.close();
    emf.close();
}
Also used : EntityManager(javax.persistence.EntityManager) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) EntityManagerFactory(javax.persistence.EntityManagerFactory) SessionInfo(org.drools.persistence.info.SessionInfo) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 2 with SessionInfo

use of org.drools.persistence.info.SessionInfo in project drools by kiegroup.

the class JpaPersistenceContext method remove.

public void remove(PersistentSession session) {
    if (!em.contains(session)) {
        SessionInfo s = em.getReference(SessionInfo.class, session.getId());
        em.remove(s);
    } else {
        em.remove(session);
    }
    TransactionManagerHelper.removeFromUpdatableSet(txm, session);
    em.flush();
}
Also used : SessionInfo(org.drools.persistence.info.SessionInfo)

Example 3 with SessionInfo

use of org.drools.persistence.info.SessionInfo in project drools by kiegroup.

the class PersistableRunner method initNewKnowledgeSession.

protected void initNewKnowledgeSession(KieBase kbase, KieSessionConfiguration conf) {
    this.sessionInfo = new SessionInfo();
    // create session but bypass command service
    this.ksession = kbase.newKieSession(conf, this.env);
    initKieSessionMBeans(this.ksession);
    this.marshallingHelper = new SessionMarshallingHelper(this.ksession, conf);
    MarshallingConfigurationImpl config = (MarshallingConfigurationImpl) this.marshallingHelper.getMarshaller().getMarshallingConfiguration();
    config.setMarshallProcessInstances(false);
    config.setMarshallWorkItems(false);
    this.sessionInfo.setJPASessionMashallingHelper(this.marshallingHelper);
    ((InternalKnowledgeRuntime) this.ksession).setEndOperationListener(new EndOperationListenerImpl(this.txm, this.sessionInfo));
    this.runner = new TransactionInterceptor();
    TimerJobFactoryManager timerJobFactoryManager = ((InternalKnowledgeRuntime) ksession).getTimerService().getTimerJobFactoryManager();
    if (timerJobFactoryManager instanceof CommandServiceTimerJobFactoryManager) {
        ((CommandServiceTimerJobFactoryManager) timerJobFactoryManager).setRunner(this);
    }
}
Also used : InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) MarshallingConfigurationImpl(org.drools.serialization.protobuf.marshalling.MarshallingConfigurationImpl) CommandServiceTimerJobFactoryManager(org.drools.core.time.impl.CommandServiceTimerJobFactoryManager) TimerJobFactoryManager(org.drools.core.time.impl.TimerJobFactoryManager) CommandServiceTimerJobFactoryManager(org.drools.core.time.impl.CommandServiceTimerJobFactoryManager) SessionInfo(org.drools.persistence.info.SessionInfo)

Example 4 with SessionInfo

use of org.drools.persistence.info.SessionInfo in project drools by kiegroup.

the class JpaPersistenceContext method findSession.

public PersistentSession findSession(Long id) {
    SessionInfo sessionInfo = null;
    if (this.pessimisticLocking) {
        sessionInfo = this.em.find(SessionInfo.class, id, lockMode);
        TransactionManagerHelper.addToUpdatableSet(txm, sessionInfo);
        return sessionInfo;
    }
    sessionInfo = this.em.find(SessionInfo.class, id);
    TransactionManagerHelper.addToUpdatableSet(txm, sessionInfo);
    return sessionInfo;
}
Also used : SessionInfo(org.drools.persistence.info.SessionInfo)

Example 5 with SessionInfo

use of org.drools.persistence.info.SessionInfo in project jbpm by kiegroup.

the class PersistenceRuntimeManagerTest method testPerProcessInstanceManagerDestorySession.

@SuppressWarnings("unchecked")
@Test
public void testPerProcessInstanceManagerDestorySession() {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultBuilder().userGroupCallback(userGroupCallback).entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2).get();
    EntityManager em = emf.createEntityManager();
    List<SessionInfo> sessions = em.createQuery("from SessionInfo").getResultList();
    assertEquals(0, sessions.size());
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    sessions = em.createQuery("from SessionInfo").getResultList();
    assertEquals(0, sessions.size());
    // ksession for process instance #1
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    runtime.getKieSession();
    sessions = em.createQuery("from SessionInfo").getResultList();
    assertEquals(1, sessions.size());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    long ksession1Id = ksession.getIdentifier();
    assertTrue(ksession1Id == 2);
    ProcessInstance pi1 = ksession.startProcess("UserTask");
    // both processes started
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    manager.disposeRuntimeEngine(runtime);
    runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi1.getId()));
    ksession = runtime.getKieSession();
    assertEquals(ksession1Id, ksession.getId());
    sessions = em.createQuery("from SessionInfo").getResultList();
    assertEquals(1, sessions.size());
    ksession.getWorkItemManager().completeWorkItem(1, null);
    manager.disposeRuntimeEngine(runtime);
    // since process is completed now session should not be there any more
    try {
        manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi1.getId())).getKieSession();
        fail("Session for this (" + pi1.getId() + ") process instance is no more accessible");
    } catch (RuntimeException e) {
    }
    sessions = em.createQuery("from SessionInfo").getResultList();
    assertEquals(0, sessions.size());
    manager.close();
    emf.close();
}
Also used : EntityManager(javax.persistence.EntityManager) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) EntityManagerFactory(javax.persistence.EntityManagerFactory) SessionInfo(org.drools.persistence.info.SessionInfo) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

SessionInfo (org.drools.persistence.info.SessionInfo)5 EntityManager (javax.persistence.EntityManager)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)2 Test (org.junit.Test)2 KieSession (org.kie.api.runtime.KieSession)2 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)2 RuntimeEnvironment (org.kie.api.runtime.manager.RuntimeEnvironment)2 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)2 InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)1 CommandServiceTimerJobFactoryManager (org.drools.core.time.impl.CommandServiceTimerJobFactoryManager)1 TimerJobFactoryManager (org.drools.core.time.impl.TimerJobFactoryManager)1 MarshallingConfigurationImpl (org.drools.serialization.protobuf.marshalling.MarshallingConfigurationImpl)1