use of io.automatiko.engine.api.uow.UnitOfWork in project automatiko-engine by automatiko-io.
the class PublishEventTest method testProcessWithMilestoneEvents.
@Test
public void testProcessWithMilestoneEvents() throws Exception {
Application app = generateCodeProcessesOnly("cases/milestones/SimpleMilestone.bpmn");
assertThat(app).isNotNull();
TestEventPublisher publisher = new TestEventPublisher();
app.unitOfWorkManager().eventManager().setService("http://myhost");
app.unitOfWorkManager().eventManager().addPublisher(publisher);
UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
uow.start();
Process<? extends Model> p = app.processes().processById("TestCase.SimpleMilestone_1_0");
ProcessInstance<?> processInstance = p.createInstance(p.createModel());
processInstance.start();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
uow.end();
List<DataEvent<?>> events = publisher.extract();
assertThat(events).isNotNull().hasSize(1);
DataEvent<?> event = events.get(0);
assertThat(event).isInstanceOf(ProcessInstanceDataEvent.class);
ProcessInstanceDataEvent processDataEvent = (ProcessInstanceDataEvent) event;
assertThat(processDataEvent.getSource()).isEqualTo("http://myhost/SimpleMilestone");
Set<MilestoneEventBody> milestones = ((ProcessInstanceDataEvent) event).getData().getMilestones();
assertThat(milestones).hasSize(2).extracting(e -> e.getName(), e -> e.getStatus()).containsExactlyInAnyOrder(tuple("AutoStartMilestone", Status.COMPLETED.name()), tuple("SimpleMilestone", Status.COMPLETED.name()));
}
use of io.automatiko.engine.api.uow.UnitOfWork in project automatiko-engine by automatiko-io.
the class UnitOfWorkExecutor method executeInUnitOfWork.
public static <T> T executeInUnitOfWork(UnitOfWorkManager uowManager, Supplier<T> supplier) {
T result = null;
UnitOfWork uow = uowManager.newUnitOfWork();
try {
uow.start();
result = supplier.get();
uow.end();
return result;
} catch (ProcessInstanceExecutionException e) {
uow.end();
throw e;
} catch (ConflictingVersionException e) {
LOGGER.warn("A conflict was identified for current unit of work with message '{}', aborting current unit of work and retrying", e.getMessage());
uow.abort();
return executeInUnitOfWork(uowManager, supplier);
} catch (Exception e) {
e.printStackTrace();
uow.abort();
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
} else {
throw new RuntimeException(e);
}
} finally {
// reset identity provider upon completion of the unit of work
IdentityProvider.set(null);
}
}
use of io.automatiko.engine.api.uow.UnitOfWork in project automatiko-engine by automatiko-io.
the class FileSystemProcessInstancesTest method testBasicFlowControlledByUnitOfWork.
@Test
void testBasicFlowControlledByUnitOfWork() {
UnitOfWorkManager uowManager = new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory());
ProcessConfig config = new StaticProcessConfig(new DefaultWorkItemHandlerConfig(), new DefaultProcessEventListenerConfig(), uowManager, null, new DefaultVariableInitializer(), new FileSystemProcessInstancesFactory());
BpmnProcess process = createProcess(config, "BPMN2-UserTask.bpmn2");
process.configure();
ProcessInstance<BpmnVariables> processInstance = process.createInstance(BpmnVariables.create(Collections.singletonMap("test", "test")));
UnitOfWork uow = uowManager.newUnitOfWork();
uow.start();
processInstance.start();
uow.end();
assertThat(processInstance.status()).isEqualTo(STATE_ACTIVE);
assertThat(processInstance.description()).isEqualTo("User Task");
assertThat(process.instances().values(1, 10)).hasSize(1);
FileSystemProcessInstances fileSystemBasedStorage = (FileSystemProcessInstances) process.instances();
verify(fileSystemBasedStorage, times(2)).create(any(), any());
verify(fileSystemBasedStorage, times(1)).setMetadata(any(), eq(FileSystemProcessInstances.PI_DESCRIPTION), eq("User Task"));
verify(fileSystemBasedStorage, times(1)).setMetadata(any(), eq(FileSystemProcessInstances.PI_STATUS), eq("1"));
String testVar = (String) processInstance.variables().get("test");
assertThat(testVar).isEqualTo("test");
assertThat(processInstance.description()).isEqualTo("User Task");
WorkItem workItem = processInstance.workItems(securityPolicy).get(0);
assertThat(workItem).isNotNull();
assertThat(workItem.getParameters().get("ActorId")).isEqualTo("john");
uow = uowManager.newUnitOfWork();
uow.start();
processInstance.completeWorkItem(workItem.getId(), null, securityPolicy);
uow.end();
assertThat(processInstance.status()).isEqualTo(STATE_COMPLETED);
fileSystemBasedStorage = (FileSystemProcessInstances) process.instances();
verify(fileSystemBasedStorage, times(1)).remove(any(), any());
assertThat(fileSystemBasedStorage.size()).isZero();
}
use of io.automatiko.engine.api.uow.UnitOfWork in project automatiko-engine by automatiko-io.
the class WorkflowThreadContextProvider method currentContext.
@Override
public ThreadContextSnapshot currentContext(Map<String, String> props) {
UnitOfWork capturedUnitOfWork = DefaultUnitOfWorkManager.getUnitOfWork();
IdentityProvider capturedIdentity = IdentityProvider.isSet() ? IdentityProvider.get() : null;
return () -> {
UnitOfWork currentUnitOfWork = DefaultUnitOfWorkManager.getUnitOfWork();
IdentityProvider currentIdentity = IdentityProvider.isSet() ? IdentityProvider.get() : null;
if (currentUnitOfWork != capturedUnitOfWork) {
DefaultUnitOfWorkManager.setUnitOfWork(capturedUnitOfWork);
}
if (currentIdentity != capturedIdentity) {
IdentityProvider.set(capturedIdentity);
}
return () -> {
DefaultUnitOfWorkManager.setUnitOfWork(currentUnitOfWork);
IdentityProvider.set(currentIdentity);
};
};
}
use of io.automatiko.engine.api.uow.UnitOfWork in project automatiko-engine by automatiko-io.
the class PublishEventTest method testServiceTaskProcessWithError.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testServiceTaskProcessWithError() throws Exception {
Application app = generateCodeProcessesOnly("servicetask/ServiceProcessDifferentOperations.bpmn2");
assertThat(app).isNotNull();
TestEventPublisher publisher = new TestEventPublisher();
app.unitOfWorkManager().eventManager().setService("http://myhost");
app.unitOfWorkManager().eventManager().addPublisher(publisher);
UnitOfWork uow = app.unitOfWorkManager().newUnitOfWork();
uow.start();
Process<? extends Model> p = app.processes().processById("ServiceProcessDifferentOperations_1_0");
Model m = p.createModel();
Map<String, Object> parameters = new HashMap<>();
m.fromMap(parameters);
ProcessInstance processInstance = p.createInstance(m);
processInstance.start();
uow.end();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ERROR);
List<DataEvent<?>> events = publisher.extract();
assertThat(events).isNotNull().hasSize(1);
ProcessInstanceEventBody body = assertProcessInstanceEvent(events.get(0), "ServiceProcessDifferentOperations", "Service Process", 5);
assertThat(body.getNodeInstances()).hasSize(2).extractingResultOf("getNodeType").contains("StartNode", "WorkItemNode");
assertThat(body.getNodeInstances()).extractingResultOf("getTriggerTime").allMatch(v -> v != null);
// human task is active
assertThat(body.getNodeInstances()).extractingResultOf("getLeaveTime").containsNull();
// thus null for leave
// time
assertThat(body.getErrors()).hasSize(1);
assertThat(body.getErrors().get(0).getNodeDefinitionId()).isEqualTo("_38E04E27-3CCA-47F9-927B-E37DC4B8CE25");
parameters.put("s", "john");
m.fromMap(parameters);
uow = app.unitOfWorkManager().newUnitOfWork();
uow.start();
processInstance.updateVariables(m);
uow.end();
events = publisher.extract();
assertThat(events).isNotNull().hasSize(1);
body = assertProcessInstanceEvent(events.get(0), "ServiceProcessDifferentOperations", "Service Process", 5);
assertThat(body.getErrors()).hasSize(1);
assertThat(body.getErrors().get(0).getNodeDefinitionId()).isEqualTo("_38E04E27-3CCA-47F9-927B-E37DC4B8CE25");
uow = app.unitOfWorkManager().newUnitOfWork();
uow.start();
if (processInstance.errors().isPresent()) {
((ProcessErrors) processInstance.errors().get()).retrigger();
}
uow.end();
events = publisher.extract();
assertThat(events).isNotNull().hasSize(1);
body = assertProcessInstanceEvent(events.get(0), "ServiceProcessDifferentOperations", "Service Process", 2);
assertThat(body.getErrors()).isEmpty();
assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_COMPLETED);
Model result = (Model) processInstance.variables();
assertThat(result.toMap()).hasSize(1).containsKeys("s");
assertThat(result.toMap().get("s")).isNotNull().isEqualTo("Goodbye Hello john!!");
}
Aggregations