Search in sources :

Example 46 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class JbpmJUnitTestCase method createKnowledgeSession.

protected KieSession createKnowledgeSession() {
    manager = RuntimeManagerFactory.Factory.get().newSingletonRuntimeManager(environment);
    RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
    KieSession result = runtime.getKieSession();
    if (sessionPersistence) {
        logService = new JPAAuditLogService(environment.getEnvironment());
    } else {
        logger = new WorkingMemoryInMemoryLogger((StatefulKnowledgeSession) result);
    }
    // knowledgeSessionSetLocal.get().add(result);
    return result;
}
Also used : WorkingMemoryInMemoryLogger(org.drools.core.audit.WorkingMemoryInMemoryLogger) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) JPAAuditLogService(org.jbpm.process.audit.JPAAuditLogService) KieSession(org.kie.api.runtime.KieSession)

Example 47 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class ProcessMain method main.

public static final void main(String[] args) throws Exception {
    JBPMHelper.startUp();
    // load up the knowledge base
    KieBase kbase = readKnowledgeBase();
    StatefulKnowledgeSession ksession = JBPMHelper.newStatefulKnowledgeSession(kbase);
    // start a new process instance
    ksession.startProcess("com.sample.bpmn.hello");
    logger.info("Process started ...");
}
Also used : KieBase(org.kie.api.KieBase) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession)

Example 48 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class SerializedTimerRollbackTest method testSerizliableTestsWithExternalRollback.

@Test
public void testSerizliableTestsWithExternalRollback() {
    try {
        createRuntimeManager("org/jbpm/test/functional/timer/HumanTaskWithBoundaryTimer.bpmn");
        RuntimeEngine runtimeEngine = getRuntimeEngine();
        KieSession ksession = runtimeEngine.getKieSession();
        TaskService taskService = runtimeEngine.getTaskService();
        logger.debug("Created knowledge session");
        TransactionManager tm = com.arjuna.ats.jta.TransactionManager.transactionManager();
        List<Long> committedProcessInstanceIds = new ArrayList<Long>();
        for (int i = 0; i < 10; i++) {
            tm.begin();
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("test", "john");
            logger.debug("Creating process instance: {}", i);
            ProcessInstance pi = ksession.startProcess("PROCESS_1", params);
            if (i % 2 == 0) {
                committedProcessInstanceIds.add(pi.getId());
                tm.commit();
            } else {
                tm.rollback();
            }
        }
        Connection c = getDs().getConnection();
        Statement st = c.createStatement();
        ResultSet rs = st.executeQuery("select rulesbytearray from sessioninfo");
        rs.next();
        Blob b = rs.getBlob("rulesbytearray");
        assertNotNull(b);
        KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        ProtobufMarshaller marshaller = new ProtobufMarshaller(builder.newKieBase(), new MarshallingConfigurationImpl());
        StatefulKnowledgeSession session = marshaller.unmarshall(b.getBinaryStream());
        assertNotNull(session);
        TimerManager timerManager = ((InternalProcessRuntime) ((InternalKnowledgeRuntime) session).getProcessRuntime()).getTimerManager();
        assertNotNull(timerManager);
        Collection<TimerInstance> timers = timerManager.getTimers();
        assertNotNull(timers);
        assertEquals(5, timers.size());
        for (TimerInstance timerInstance : timers) {
            assertTrue(committedProcessInstanceIds.contains(timerInstance.getProcessInstanceId()));
            ksession.abortProcessInstance(timerInstance.getProcessInstanceId());
        }
        List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
        assertEquals(0, tasks.size());
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception thrown");
    }
}
Also used : ProtobufMarshaller(org.drools.core.marshalling.impl.ProtobufMarshaller) HashMap(java.util.HashMap) TimerInstance(org.jbpm.process.instance.timer.TimerInstance) ArrayList(java.util.ArrayList) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ResultSet(java.sql.ResultSet) KieSession(org.kie.api.runtime.KieSession) MarshallingConfigurationImpl(org.drools.core.marshalling.impl.MarshallingConfigurationImpl) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) Blob(java.sql.Blob) TaskService(org.kie.api.task.TaskService) Statement(java.sql.Statement) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Connection(java.sql.Connection) TimerManager(org.jbpm.process.instance.timer.TimerManager) TransactionManager(javax.transaction.TransactionManager) TaskSummary(org.kie.api.task.model.TaskSummary) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) Test(org.junit.Test)

Example 49 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class ProcessMarshallingTest method testMarshallingProcessInstanceWithTimer.

@Test
@Ignore
public void testMarshallingProcessInstanceWithTimer() throws Exception {
    String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "  xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "  xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "  type=\"RuleFlow\" name=\"ruleflow\" id=\"com.sample.ruleflow\" package-name=\"com.sample\" >\n" + "\n" + "    <header>\n" + "    </header>\n" + "\n" + "    <nodes>\n" + "      <start id=\"1\" name=\"Start\" />\n" + "      <timerNode id=\"4\" name=\"Timer\" delay=\"200\" />\n" + "      <end id=\"3\" name=\"End\" />\n" + "    </nodes>\n" + "\n" + "    <connections>\n" + "      <connection from=\"1\" to=\"4\" />\n" + "      <connection from=\"4\" to=\"3\" />\n" + "    </connections>\n" + "\n" + "</process>\n";
    builder.addProcessFromXml(new StringReader(process));
    final KieSession session = createKieSession(builder.getPackages());
    session.startProcess("com.sample.ruleflow", null);
    assertEquals(1, session.getProcessInstances().size());
    session.halt();
    final StatefulKnowledgeSession session2 = getSerialisedStatefulKnowledgeSession(session);
    int sleeps = 3;
    int procInstsAlive = session2.getProcessInstances().size();
    while (procInstsAlive > 0 && sleeps > 0) {
        Thread.sleep(1000);
        --sleeps;
        procInstsAlive = session2.getProcessInstances().size();
    }
    assertEquals(0, session2.getProcessInstances().size());
    session2.halt();
}
Also used : JbpmSerializationHelper.getSerialisedStatefulKnowledgeSession(org.jbpm.integrationtests.JbpmSerializationHelper.getSerialisedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 50 with StatefulKnowledgeSession

use of org.kie.internal.runtime.StatefulKnowledgeSession in project jbpm by kiegroup.

the class ProcessMarshallingTest method testTimerOnUnmarshalledSession.

@Test
public void testTimerOnUnmarshalledSession() throws Exception {
    String process = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<process xmlns=\"http://drools.org/drools-5.0/process\"\n" + "  xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "  xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\"\n" + "  type=\"RuleFlow\" name=\"ruleflow\" id=\"com.sample.ruleflow\" package-name=\"com.sample\" >\n" + "\n" + "    <header>\n" + "    </header>\n" + "\n" + "    <nodes>\n" + "      <start id=\"1\" name=\"Start\" />\n" + "      <timerNode id=\"4\" name=\"Timer\" delay=\"1000\" />\n" + "      <end id=\"3\" name=\"End\" />\n" + "    </nodes>\n" + "\n" + "    <connections>\n" + "      <connection from=\"1\" to=\"4\" />\n" + "      <connection from=\"4\" to=\"3\" />\n" + "    </connections>\n" + "\n" + "</process>\n";
    builder.addProcessFromXml(new StringReader(process));
    KieSession session = createKieSession(builder.getPackages());
    session.startProcess("com.sample.ruleflow", null);
    // serialize session
    Marshaller marshaller = MarshallerFactory.newMarshaller(session.getKieBase());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshall(baos, session);
    byte[] b1 = baos.toByteArray();
    baos.close();
    // hope that timer hasn't fired yet?
    assertEquals(1, session.getProcessInstances().size());
    // dispose of session
    session.dispose();
    // deserialize session
    ByteArrayInputStream bais = new ByteArrayInputStream(b1);
    StatefulKnowledgeSession session2 = (StatefulKnowledgeSession) marshaller.unmarshall(bais);
    // make sure time job runs
    int sleeps = 3;
    int procInstsAlive = session2.getProcessInstances().size();
    while (procInstsAlive > 0 && sleeps > 0) {
        Thread.yield();
        Thread.sleep(1000);
        --sleeps;
        procInstsAlive = session2.getProcessInstances().size();
    }
    // verify
    assertEquals(0, session2.getProcessInstances().size());
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) JbpmSerializationHelper.getSerialisedStatefulKnowledgeSession(org.jbpm.integrationtests.JbpmSerializationHelper.getSerialisedStatefulKnowledgeSession) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)114 Test (org.junit.Test)79 KieBase (org.kie.api.KieBase)52 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)40 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)30 ArrayList (java.util.ArrayList)29 CommandBasedStatefulKnowledgeSession (org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession)22 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)20 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)18 HashMap (java.util.HashMap)16 Environment (org.kie.api.runtime.Environment)12 KieSession (org.kie.api.runtime.KieSession)11 WorkflowProcessInstance (org.kie.api.runtime.process.WorkflowProcessInstance)11 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)10 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 WorkItem (org.kie.api.runtime.process.WorkItem)9 TestWorkItemHandler (org.jbpm.bpmn2.objects.TestWorkItemHandler)8 TestWorkItemHandler (org.jbpm.persistence.session.objects.TestWorkItemHandler)8 Resource (org.kie.api.io.Resource)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7