Search in sources :

Example 1 with ProtobufMarshaller

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;
}
Also used : ProtobufMarshaller(org.drools.serialization.protobuf.ProtobufMarshaller) ByteArrayInputStream(java.io.ByteArrayInputStream) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ProtobufMarshaller (org.drools.serialization.protobuf.ProtobufMarshaller)1 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)1