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