Search in sources :

Example 1 with ProcessInstanceManager

use of org.jbpm.process.instance.ProcessInstanceManager in project jbpm by kiegroup.

the class ProcessInstanceResolverStrategy method retrieveKnowledgeRuntime.

/**
 * Retrieve the {@link ProcessInstanceManager} object from the ObjectOutput- or ObjectInputStream.
 * The stream object will secretly also either be a {@link MarshallerReaderContext} or a
 * {@link MarshallerWriteContext}.
 * </p>
 * The knowledge runtime object is useful in order to reconnect the process instance to the
 * process and the knowledge runtime object.
 * @param streamContext The marshaller stream/context.
 * @return A {@link InternalKnowledgeRuntime} object.
 */
public static InternalKnowledgeRuntime retrieveKnowledgeRuntime(Object streamContext) {
    InternalKnowledgeRuntime kruntime = null;
    if (streamContext instanceof MarshallerWriteContext) {
        MarshallerWriteContext context = (MarshallerWriteContext) streamContext;
        kruntime = ((InternalWorkingMemory) context.wm).getKnowledgeRuntime();
    } else if (streamContext instanceof MarshallerReaderContext) {
        MarshallerReaderContext context = (MarshallerReaderContext) streamContext;
        kruntime = ((InternalWorkingMemory) context.wm).getKnowledgeRuntime();
    } else {
        throw new UnsupportedOperationException("Unable to retrieve " + ProcessInstanceManager.class.getSimpleName() + " from " + streamContext.getClass().getName());
    }
    return kruntime;
}
Also used : InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ProcessInstanceManager(org.jbpm.process.instance.ProcessInstanceManager) MarshallerWriteContext(org.drools.core.marshalling.impl.MarshallerWriteContext) MarshallerReaderContext(org.drools.core.marshalling.impl.MarshallerReaderContext)

Example 2 with ProcessInstanceManager

use of org.jbpm.process.instance.ProcessInstanceManager in project jbpm by kiegroup.

the class ProcessInstanceResolverStrategy method retrieveProcessInstanceManager.

/**
 * Retrieve the {@link ProcessInstanceManager} object from the ObjectOutput- or ObjectInputStream.
 * The stream object will secretly also either be a {@link MarshallerReaderContext} or a
 * {@link MarshallerWriteContext}.
 * @param streamContext The marshaller stream/context.
 * @return A {@link ProcessInstanceManager} object.
 */
public static ProcessInstanceManager retrieveProcessInstanceManager(Object streamContext) {
    ProcessInstanceManager pim = null;
    if (streamContext instanceof MarshallerWriteContext) {
        MarshallerWriteContext context = (MarshallerWriteContext) streamContext;
        pim = ((ProcessRuntimeImpl) ((InternalWorkingMemory) context.wm).getProcessRuntime()).getProcessInstanceManager();
    } else if (streamContext instanceof MarshallerReaderContext) {
        MarshallerReaderContext context = (MarshallerReaderContext) streamContext;
        pim = ((ProcessRuntimeImpl) ((InternalWorkingMemory) context.wm).getProcessRuntime()).getProcessInstanceManager();
    } else {
        throw new UnsupportedOperationException("Unable to retrieve " + ProcessInstanceManager.class.getSimpleName() + " from " + streamContext.getClass().getName());
    }
    return pim;
}
Also used : ProcessInstanceManager(org.jbpm.process.instance.ProcessInstanceManager) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) MarshallerWriteContext(org.drools.core.marshalling.impl.MarshallerWriteContext) MarshallerReaderContext(org.drools.core.marshalling.impl.MarshallerReaderContext) ProcessRuntimeImpl(org.jbpm.process.instance.ProcessRuntimeImpl)

Example 3 with ProcessInstanceManager

use of org.jbpm.process.instance.ProcessInstanceManager in project jbpm by kiegroup.

the class ProcessInstanceResolverStrategyTest method testProcessInstanceResolverStrategy.

@Test
public void testProcessInstanceResolverStrategy() throws Exception {
    // Setup
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource(PROCESS_NAME, this.getClass()), ResourceType.DRF);
    KieBase kbase = kbuilder.newKieBase();
    KieSession ksession = kbase.newKieSession();
    ProcessInstance processInstance = ksession.createProcessInstance("process name", new HashMap<String, Object>());
    ksession.insert(processInstance);
    // strategy setup
    ProcessInstanceResolverStrategy strategy = new ProcessInstanceResolverStrategy();
    ObjectMarshallingStrategy[] strategies = { strategy, MarshallerFactory.newSerializeMarshallingStrategy() };
    // Test strategy.write
    org.kie.api.marshalling.MarshallingConfiguration marshallingConfig = new MarshallingConfigurationImpl(strategies, true, true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MarshallerWriteContext writerContext = new MarshallerWriteContext(baos, ((InternalKnowledgeBase) kbase), (InternalWorkingMemory) ((StatefulKnowledgeSessionImpl) ksession), RuleBaseNodes.getNodeMap(((InternalKnowledgeBase) kbase)), marshallingConfig.getObjectMarshallingStrategyStore(), marshallingConfig.isMarshallProcessInstances(), marshallingConfig.isMarshallWorkItems(), ksession.getEnvironment());
    strategy.write(writerContext, processInstance);
    baos.close();
    writerContext.close();
    byte[] bytes = baos.toByteArray();
    int numCorrectBytes = calculateNumBytesForLong(processInstance.getId());
    assertTrue("Expected " + numCorrectBytes + " bytes, not " + bytes.length, bytes.length == numCorrectBytes);
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    ObjectInputStream ois = new ObjectInputStream(bais);
    long serializedProcessInstanceId = ois.readLong();
    assertTrue("Expected " + processInstance.getId() + ", not " + serializedProcessInstanceId, processInstance.getId() == serializedProcessInstanceId);
    // Test other strategy stuff
    ProcessInstanceManager pim = ProcessInstanceResolverStrategy.retrieveProcessInstanceManager(writerContext);
    assertNotNull(pim);
    assertNotNull(ProcessInstanceResolverStrategy.retrieveKnowledgeRuntime(writerContext));
    assertTrue(processInstance == pim.getProcessInstance(serializedProcessInstanceId));
    // Test strategy.read
    bais = new ByteArrayInputStream(bytes);
    MarshallerReaderContext readerContext = new MarshallerReaderContext(bais, ((KnowledgeBaseImpl) kbase), RuleBaseNodes.getNodeMap(((KnowledgeBaseImpl) kbase)), marshallingConfig.getObjectMarshallingStrategyStore(), ProtobufMarshaller.TIMER_READERS, marshallingConfig.isMarshallProcessInstances(), marshallingConfig.isMarshallWorkItems(), EnvironmentFactory.newEnvironment());
    readerContext.wm = ((StatefulKnowledgeSessionImpl) ksession).getInternalWorkingMemory();
    Object procInstObject = strategy.read(readerContext);
    assertTrue(procInstObject != null && procInstObject instanceof ProcessInstance);
    assertTrue(processInstance == procInstObject);
}
Also used : ProcessInstanceResolverStrategy(org.jbpm.marshalling.impl.ProcessInstanceResolverStrategy) ProcessInstanceManager(org.jbpm.process.instance.ProcessInstanceManager) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieBase(org.kie.api.KieBase) MarshallerReaderContext(org.drools.core.marshalling.impl.MarshallerReaderContext) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) MarshallingConfigurationImpl(org.drools.core.marshalling.impl.MarshallingConfigurationImpl) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClassPathResource(org.drools.core.io.impl.ClassPathResource) ByteArrayInputStream(java.io.ByteArrayInputStream) MarshallerWriteContext(org.drools.core.marshalling.impl.MarshallerWriteContext) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 4 with ProcessInstanceManager

use of org.jbpm.process.instance.ProcessInstanceManager in project jbpm by kiegroup.

the class ProcessInstanceResolverStrategy method unmarshal.

public Object unmarshal(Context context, ObjectInputStream is, byte[] object, ClassLoader classloader) throws IOException, ClassNotFoundException {
    long processInstanceId = PersisterHelper.byteArrayToLong(object);
    ProcessInstanceManager pim = retrieveProcessInstanceManager(is);
    // load it as read only to avoid any updates to the data base
    ProcessInstance processInstance = pim.getProcessInstance(processInstanceId, true);
    if (processInstance == null) {
        RuleFlowProcessInstance result = new RuleFlowProcessInstance();
        result.setId(processInstanceId);
        result.internalSetState(ProcessInstance.STATE_COMPLETED);
        return result;
    } else {
        connectProcessInstanceToRuntimeAndProcess(processInstance, is);
        return processInstance;
    }
}
Also used : ProcessInstanceManager(org.jbpm.process.instance.ProcessInstanceManager) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Example 5 with ProcessInstanceManager

use of org.jbpm.process.instance.ProcessInstanceManager in project jbpm by kiegroup.

the class ProcessInstanceResolverStrategy method read.

public Object read(ObjectInputStream is) throws IOException, ClassNotFoundException {
    long processInstanceId = is.readLong();
    ProcessInstanceManager pim = retrieveProcessInstanceManager(is);
    ProcessInstance processInstance = pim.getProcessInstance(processInstanceId);
    if (processInstance == null) {
        RuleFlowProcessInstance result = new RuleFlowProcessInstance();
        result.setId(processInstanceId);
        result.internalSetState(ProcessInstance.STATE_COMPLETED);
        return result;
    } else {
        connectProcessInstanceToRuntimeAndProcess(processInstance, is);
        return processInstance;
    }
}
Also used : ProcessInstanceManager(org.jbpm.process.instance.ProcessInstanceManager) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) ProcessInstance(org.kie.api.runtime.process.ProcessInstance)

Aggregations

ProcessInstanceManager (org.jbpm.process.instance.ProcessInstanceManager)6 MarshallerReaderContext (org.drools.core.marshalling.impl.MarshallerReaderContext)3 MarshallerWriteContext (org.drools.core.marshalling.impl.MarshallerWriteContext)3 RuleFlowProcessInstance (org.jbpm.ruleflow.instance.RuleFlowProcessInstance)3 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)3 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 ProcessRuntimeImpl (org.jbpm.process.instance.ProcessRuntimeImpl)2 KieSession (org.kie.api.runtime.KieSession)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)1 ClassPathResource (org.drools.core.io.impl.ClassPathResource)1 MarshallingConfigurationImpl (org.drools.core.marshalling.impl.MarshallingConfigurationImpl)1 ProcessInstanceResolverStrategy (org.jbpm.marshalling.impl.ProcessInstanceResolverStrategy)1 DefaultProcessInstanceManager (org.jbpm.process.instance.impl.DefaultProcessInstanceManager)1 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)1 Test (org.junit.Test)1