use of org.drools.serialization.protobuf.ProtobufMarshaller in project drools by kiegroup.
the class SerializationHelper method getSerialisedStatefulKnowledgeSession.
public static StatefulKnowledgeSession getSerialisedStatefulKnowledgeSession(final KieSession ksession, final KieBase kbase, final boolean dispose) throws IOException, ClassNotFoundException {
final ProtobufMarshaller marshaller = (ProtobufMarshaller) MarshallerFactory.newMarshaller(kbase, (ObjectMarshallingStrategy[]) ksession.getEnvironment().get(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES));
final long time = ksession.getSessionClock().getCurrentTime();
// make sure globas are in the environment of the session
ksession.getEnvironment().set(EnvironmentName.GLOBALS, ksession.getGlobals());
// Serialize object
final byte[] b1;
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
marshaller.marshall(bos, ksession, time);
b1 = bos.toByteArray();
}
// Deserialize object
final StatefulKnowledgeSession ksession2;
try (final ByteArrayInputStream bais = new ByteArrayInputStream(b1)) {
ksession2 = marshaller.unmarshall(bais, ksession.getSessionConfiguration(), ksession.getEnvironment());
}
if (dispose) {
ksession.dispose();
}
return ksession2;
}
Aggregations