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;
}
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");
}
}
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");
}
}
Aggregations