Skip to content

Commit

Permalink
JBPM-4553 - Variable persistence strategy is invoked too many times
Browse files Browse the repository at this point in the history
  • Loading branch information
mswiderski committed Feb 13, 2015
1 parent 2cecbdf commit 0b24df4
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 300 deletions.
Expand Up @@ -36,6 +36,12 @@
public class ProtobufProcessMarshaller
implements
ProcessMarshaller {

private static boolean persistWorkItemVars = Boolean.parseBoolean(System.getProperty("org.jbpm.wi.variable.persist", "true"));
// mainly for testability as the setting is global
public static void setWorkItemVarsPersistence(boolean turnOn) {
persistWorkItemVars = turnOn;
}

public void writeProcessInstances(MarshallerWriteContext context) throws IOException {
ProtobufMessages.ProcessData.Builder _pdata = (ProtobufMessages.ProcessData.Builder) context.parameterObject;
Expand Down Expand Up @@ -335,7 +341,7 @@ public void init(MarshallerReaderContext context) {
@Override
public void writeWorkItem(MarshallerWriteContext context, org.drools.core.process.instance.WorkItem workItem) {
try {
JBPMMessages.WorkItem _workItem = writeWorkItem(context, workItem, true);
JBPMMessages.WorkItem _workItem = writeWorkItem(context, workItem, persistWorkItemVars);
PersisterHelper.writeToStreamWithHeader( context, _workItem );
} catch (IOException e) {
throw new IllegalArgumentException( "IOException while storing work item instance "
Expand All @@ -349,7 +355,7 @@ public org.drools.core.process.instance.WorkItem readWorkItem(MarshallerReaderCo
ExtensionRegistry registry = PersisterHelper.buildRegistry(context, null);
Header _header = PersisterHelper.readFromStreamWithHeaderPreloaded(context, registry);
JBPMMessages.WorkItem _workItem = JBPMMessages.WorkItem.parseFrom(_header.getPayload(), registry);
return (org.drools.core.process.instance.WorkItem) readWorkItem(context, _workItem, true);
return (org.drools.core.process.instance.WorkItem) readWorkItem(context, _workItem, persistWorkItemVars);
} catch (IOException e) {
throw new IllegalArgumentException( "IOException while fetching work item instance : " + e.getMessage(), e );
} catch (ClassNotFoundException e) {
Expand Down
Expand Up @@ -142,7 +142,7 @@
</properties>
</persistence-unit>

<persistence-unit name="org.jbpm.persistence.patient.example">
<persistence-unit name="org.jbpm.persistence.patient.example" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.jbpm.test.persistence.objects.Patient</class>
<class>org.jbpm.test.persistence.objects.MedicalRecord</class>
Expand All @@ -162,6 +162,8 @@
<property name="hibernate.connection.url" value="jdbc:h2:mem:mydb" />
<property name="hibernate.connection.username" value="sa" />
<property name="hibernate.connection.password" value="sasa" />

<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.BitronixJtaPlatform" />
</properties>
</persistence-unit>

Expand Down
Expand Up @@ -8,17 +8,25 @@
import java.util.List;
import java.util.Map;

import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.transaction.UserTransaction;

import org.drools.core.marshalling.impl.ClassObjectMarshallingStrategyAcceptor;
import org.drools.core.marshalling.impl.SerializablePlaceholderResolverStrategy;
import org.drools.persistence.jpa.marshaller.JPAPlaceholderResolverStrategy;
import org.jbpm.marshalling.impl.ProcessInstanceResolverStrategy;
import org.jbpm.services.task.utils.ContentMarshallerHelper;
import org.jbpm.test.JbpmJUnitBaseTestCase;
import org.jbpm.test.persistence.objects.MedicalRecord;
import org.jbpm.test.persistence.objects.Patient;
import org.jbpm.test.persistence.objects.RecordRow;
import org.junit.Assert;
import org.junit.Test;
import org.kie.api.marshalling.ObjectMarshallingStrategy;
import org.kie.api.runtime.EnvironmentName;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.process.ProcessInstance;
Expand Down Expand Up @@ -46,11 +54,15 @@ public void simplePatientMedicalRecordTest() throws Exception {
MedicalRecord medicalRecord = new MedicalRecord("Last Three Years Medical Hisotry", salaboy);

emfDomain = Persistence.createEntityManagerFactory("org.jbpm.persistence.patient.example");
EntityManager em = emfDomain.createEntityManager();
addEnvironmentEntry(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES,
new ObjectMarshallingStrategy[] {
new ProcessInstanceResolverStrategy(),
new JPAPlaceholderResolverStrategy(emfDomain),
new SerializablePlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });


em.getTransaction().begin();
em.persist(medicalRecord);
em.getTransaction().commit();
EntityManager em = emfDomain.createEntityManager();


createRuntimeManager("patient-appointment.bpmn");
RuntimeEngine runtimeEngine = getRuntimeEngine();
Expand Down Expand Up @@ -85,12 +97,10 @@ public void simplePatientMedicalRecordTest() throws Exception {
MedicalRecord taskMedicalRecord = getTaskContent(runtimeEngine, frontDeskTasks.get(0));
Assert.assertNotNull(taskMedicalRecord.getId());
taskMedicalRecord.setDescription("Initial Description of the Medical Record");

em.getTransaction().begin();
em.merge(taskMedicalRecord);
em.getTransaction().commit();

taskService.complete(frontDeskTasks.get(0).getId(), "frontDesk", null);

Map<String, Object> output = new HashMap<String, Object>();
output.put("output1", taskMedicalRecord);
taskService.complete(frontDeskTasks.get(0).getId(), "frontDesk", output);

//Now doctor has 1 task
doctorTasks = taskService.getTasksAssignedAsPotentialOwner("doctor", "en-UK");
Expand All @@ -100,16 +110,26 @@ public void simplePatientMedicalRecordTest() throws Exception {
managerTasks = taskService.getTasksAssignedAsPotentialOwner("manager", "en-UK");
Assert.assertTrue(managerTasks.isEmpty());

// modify the entity from outside
taskMedicalRecord.setDescription("Initial Description of the Medical Record - Updated");

UserTransaction ut = InitialContext.doLookup("java:comp/UserTransaction");
ut.begin();
em.merge(taskMedicalRecord);
ut.commit();

taskMedicalRecord = getTaskContent(runtimeEngine, doctorTasks.get(0));

Assert.assertNotNull(taskMedicalRecord.getId());
taskMedicalRecord.setDescription("Initial Description of the Medical Record - Updated");

taskService.start(doctorTasks.get(0).getId(), "doctor");
//Check that we have the Modified Document
taskMedicalRecord = em.find(MedicalRecord.class, taskMedicalRecord.getId());

Assert.assertEquals("Initial Description of the Medical Record", taskMedicalRecord.getDescription());
Assert.assertEquals("Initial Description of the Medical Record - Updated", taskMedicalRecord.getDescription());


em.getTransaction().begin();
taskMedicalRecord.setDescription("Medical Record Validated by Doctor");
List<RecordRow> rows = new ArrayList<RecordRow>();
RecordRow recordRow = new RecordRow("CODE-999", "Just a regular Cold");
Expand All @@ -118,24 +138,26 @@ public void simplePatientMedicalRecordTest() throws Exception {
taskMedicalRecord.setRows(rows);
taskMedicalRecord.setPriority(1);

em.getTransaction().commit();
output = new HashMap<String, Object>();
output.put("output2", taskMedicalRecord);

taskService.complete(doctorTasks.get(0).getId(), "doctor", null);
taskService.complete(doctorTasks.get(0).getId(), "doctor", output);

// tasks for manager
managerTasks = taskService.getTasksAssignedAsPotentialOwner("manager", "en-UK");
Assert.assertEquals(1, managerTasks.size());
taskService.start(managerTasks.get(0).getId(), "manager");

em.getTransaction().begin();
Patient patient = taskMedicalRecord.getPatient();
patient.setNextAppointment(new Date());


em.getTransaction().commit();
output = new HashMap<String, Object>();
output.put("output3", taskMedicalRecord);

// ksession.getWorkItemManager().registerWorkItemHandler("Human Task", htHandler);

taskService.complete(managerTasks.get(0).getId(), "manager", null);
taskService.complete(managerTasks.get(0).getId(), "manager", output);

// since persisted process instance is completed it should be null
process = ksession.getProcessInstance(process.getId());
Expand Down

0 comments on commit 0b24df4

Please sign in to comment.