Search in sources :

Example 1 with ProtobufMarshaller

use of org.drools.core.marshalling.impl.ProtobufMarshaller in project drools by kiegroup.

the class SerializationHelper method getSerialisedStatefulKnowledgeSession.

public static StatefulKnowledgeSession getSerialisedStatefulKnowledgeSession(KieSession ksession, KieBase kbase, boolean dispose, boolean testRoundTrip) throws Exception {
    ProtobufMarshaller marshaller = (ProtobufMarshaller) MarshallerFactory.newMarshaller(kbase, (ObjectMarshallingStrategy[]) ksession.getEnvironment().get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES));
    long time = ksession.<SessionClock>getSessionClock().getCurrentTime();
    // make sure globas are in the environment of the session
    ksession.getEnvironment().set(EnvironmentName.GLOBALS, ksession.getGlobals());
    // Serialize object
    final byte[] b1;
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        marshaller.marshall(bos, ksession, time);
        b1 = bos.toByteArray();
        bos.close();
    }
    // Deserialize object
    StatefulKnowledgeSession ksession2;
    {
        ByteArrayInputStream bais = new ByteArrayInputStream(b1);
        ksession2 = marshaller.unmarshall(bais, ksession.getSessionConfiguration(), ksession.getEnvironment());
        bais.close();
    }
    if (testRoundTrip) {
        // for now, we can ensure the IDs will match because queries are creating untraceable fact handles at the moment
        // int previous_id = ((StatefulKnowledgeSessionImpl)ksession).session.getFactHandleFactory().getId();
        // long previous_recency = ((StatefulKnowledgeSessionImpl)ksession).session.getFactHandleFactory().getRecency();
        // int current_id = ((StatefulKnowledgeSessionImpl)ksession2).session.getFactHandleFactory().getId();
        // long current_recency = ((StatefulKnowledgeSessionImpl)ksession2).session.getFactHandleFactory().getRecency();
        // ((StatefulKnowledgeSessionImpl)ksession2).session.getFactHandleFactory().clear( previous_id, previous_recency );
        // Reserialize and check that byte arrays are the same
        final byte[] b2;
        {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            marshaller.marshall(bos, ksession2, time);
            b2 = bos.toByteArray();
            bos.close();
        }
        // bytes should be the same.
        if (!areByteArraysEqual(b1, b2)) {
        // throw new IllegalArgumentException( "byte streams for serialisation test are not equal" );
        }
    // ((StatefulKnowledgeSessionImpl) ksession2).session.getFactHandleFactory().clear( current_id, current_recency );
    // ((StatefulKnowledgeSessionImpl) ksession2).session.setGlobalResolver( ((StatefulKnowledgeSessionImpl) ksession).session.getGlobalResolver() );
    }
    if (dispose) {
        ksession.dispose();
    }
    return ksession2;
}
Also used : ProtobufMarshaller(org.drools.core.marshalling.impl.ProtobufMarshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 2 with ProtobufMarshaller

use of org.drools.core.marshalling.impl.ProtobufMarshaller 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 3 with ProtobufMarshaller

use of org.drools.core.marshalling.impl.ProtobufMarshaller in project jbpm by kiegroup.

the class SerializedTimerRollbackTest method testSerizliableTestsWithEngineRollback.

@Test
public void testSerizliableTestsWithEngineRollback() {
    try {
        createRuntimeManager("org/jbpm/test/functional/timer/HumanTaskWithBoundaryTimer.bpmn");
        RuntimeEngine runtimeEngine = getRuntimeEngine();
        KieSession ksession = runtimeEngine.getKieSession();
        logger.debug("Created knowledge session");
        TaskService taskService = runtimeEngine.getTaskService();
        logger.debug("Task service created");
        List<Long> committedProcessInstanceIds = new ArrayList<Long>();
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                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);
                committedProcessInstanceIds.add(pi.getId());
            } else {
                try {
                    Map<String, Object> params = new HashMap<String, Object>();
                    // set test variable to null so engine will rollback
                    params.put("test", null);
                    logger.debug("Creating process instance: {}", i);
                    ksession.startProcess("PROCESS_1", params);
                } catch (Exception e) {
                    logger.debug("Process rolled back");
                }
            }
        }
        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) TaskSummary(org.kie.api.task.model.TaskSummary) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) Test(org.junit.Test)

Aggregations

ProtobufMarshaller (org.drools.core.marshalling.impl.ProtobufMarshaller)3 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)3 Blob (java.sql.Blob)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 MarshallingConfigurationImpl (org.drools.core.marshalling.impl.MarshallingConfigurationImpl)2 InternalProcessRuntime (org.jbpm.process.instance.InternalProcessRuntime)2 TimerInstance (org.jbpm.process.instance.timer.TimerInstance)2 TimerManager (org.jbpm.process.instance.timer.TimerManager)2 Test (org.junit.Test)2 KieSession (org.kie.api.runtime.KieSession)2 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)2 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)2 TaskService (org.kie.api.task.TaskService)2 TaskSummary (org.kie.api.task.model.TaskSummary)2 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1