use of io.automatiko.engine.workflow.base.instance.InternalProcessRuntime in project automatiko-engine by automatiko-io.
the class WorkItemTest method testReachNonRegisteredWorkItemHandler.
@Test
public void testReachNonRegisteredWorkItemHandler() {
String processId = "org.company.actions";
String workName = "Unnexistent Task";
ExecutableProcess process = getWorkItemProcess(processId, workName);
InternalProcessRuntime ksession = createProcessRuntime(process);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("UserName", "John Doe");
parameters.put("Person", new Person("John Doe"));
ProcessInstance processInstance = null;
try {
processInstance = ksession.startProcess("org.company.actions", parameters);
fail("should fail if WorkItemHandler for" + workName + "is not registered");
} catch (Throwable e) {
}
assertEquals(ProcessInstance.STATE_ERROR, processInstance.getState());
}
use of io.automatiko.engine.workflow.base.instance.InternalProcessRuntime in project automatiko-engine by automatiko-io.
the class WorkItemTest method testCancelNonRegisteredWorkItemHandler.
@Test
public void testCancelNonRegisteredWorkItemHandler() {
String processId = "org.company.actions";
String workName = "Unnexistent Task";
ExecutableProcess process = getWorkItemProcess(processId, workName);
InternalProcessRuntime ksession = createProcessRuntime(process);
ksession.getWorkItemManager().registerWorkItemHandler(workName, new DoNothingWorkItemHandler());
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("UserName", "John Doe");
parameters.put("Person", new Person("John Doe"));
ProcessInstance processInstance = ksession.startProcess("org.company.actions", parameters);
String processInstanceId = processInstance.getId();
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
ksession.getWorkItemManager().registerWorkItemHandler(workName, null);
try {
ksession.abortProcessInstance(processInstanceId);
fail("should fail if WorkItemHandler for" + workName + "is not registered");
} catch (WorkItemHandlerNotFoundException wihnfe) {
}
assertEquals(ProcessInstance.STATE_ABORTED, processInstance.getState());
}
use of io.automatiko.engine.workflow.base.instance.InternalProcessRuntime in project automatiko-engine by automatiko-io.
the class StartNodeInstanceTest method testStartNode.
@Test
public void testStartNode() {
MockNode mockNode = new MockNode();
MockNodeInstanceFactory mockNodeFactory = new MockNodeInstanceFactory(new MockNodeInstance(mockNode));
NodeInstanceFactoryRegistry.getInstance().register(mockNode.getClass(), mockNodeFactory);
ExecutableProcess process = new ExecutableProcess();
process.setId("test");
InternalProcessRuntime processRuntime = createProcessRuntime(process);
StartNode startNode = new StartNode();
startNode.setId(1);
startNode.setName("start node");
mockNode.setId(2);
new ConnectionImpl(startNode, io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE, mockNode, io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE);
process.addNode(startNode);
process.addNode(mockNode);
ExecutableProcessInstance processInstance = new ExecutableProcessInstance();
processInstance.setProcess(process);
processInstance.setProcessRuntime(processRuntime);
assertEquals(ProcessInstance.STATE_PENDING, processInstance.getState());
processInstance.start();
assertEquals(ProcessInstance.STATE_ACTIVE, processInstance.getState());
MockNodeInstance mockNodeInstance = mockNodeFactory.getMockNodeInstance();
List<NodeInstance> triggeredBy = mockNodeInstance.getTriggers().get(io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE);
assertNotNull(triggeredBy);
assertEquals(1, triggeredBy.size());
assertSame(startNode.getId(), triggeredBy.get(0).getNodeId());
}
use of io.automatiko.engine.workflow.base.instance.InternalProcessRuntime in project automatiko-engine by automatiko-io.
the class WorkflowProcessInstanceImpl method setState.
@Override
public void setState(final int state, String outcome) {
// TODO move most of this to ProcessInstanceImpl
if (state == ProcessInstance.STATE_COMPLETED || state == ProcessInstance.STATE_ABORTED) {
this.endDate = new Date();
if (this.slaCompliance == SLA_PENDING) {
if (System.currentTimeMillis() > slaDueDate.getTime()) {
// completion of the process instance is after expected SLA due date, mark it
// accordingly
this.slaCompliance = SLA_VIOLATED;
} else {
this.slaCompliance = state == ProcessInstance.STATE_COMPLETED ? SLA_MET : SLA_ABORTED;
}
}
InternalProcessRuntime processRuntime = getProcessRuntime();
processRuntime.getProcessEventSupport().fireBeforeProcessCompleted(this, processRuntime);
// JBPM-8094 - set state after event
super.setState(state, outcome);
// deactivate all node instances of this process instance
while (!nodeInstances.isEmpty()) {
NodeInstance nodeInstance = nodeInstances.get(0);
nodeInstance.cancel();
}
if (this.slaTimerId != null && !slaTimerId.trim().isEmpty()) {
processRuntime.getJobsService().cancelJob(this.slaTimerId);
logger.debug("SLA Timer {} has been canceled", this.slaTimerId);
}
removeEventListeners();
processRuntime.getProcessInstanceManager().removeProcessInstance(this);
processRuntime.getProcessEventSupport().fireAfterProcessCompleted(this, processRuntime);
if (isSignalCompletion()) {
IdentityProvider identity = IdentityProvider.get();
try {
// make sure that identity is switched to trusted one as whoever executed this instance
// might not have access to parent process instance
IdentityProvider.set(new TrustedIdentityProvider("system"));
List<EventListener> listeners = eventListeners.get("processInstanceCompleted:" + getId());
if (listeners != null) {
for (EventListener listener : listeners) {
listener.signalEvent("processInstanceCompleted:" + getId(), this);
}
}
processRuntime.getSignalManager().signalEvent("processInstanceCompleted:" + getId(), this);
} finally {
IdentityProvider.set(identity);
}
}
} else {
super.setState(state, outcome);
}
}
use of io.automatiko.engine.workflow.base.instance.InternalProcessRuntime in project automatiko-engine by automatiko-io.
the class WorkflowProcessInstanceImpl method signalEvent.
@Override
@SuppressWarnings("unchecked")
public void signalEvent(String type, Object event) {
logger.debug("Signal {} received with data {} in process instance {}", type, event, getId());
synchronized (this) {
if (getState() != ProcessInstance.STATE_ACTIVE) {
return;
}
InternalProcessRuntime processRuntime = getProcessRuntime();
processRuntime.getProcessEventSupport().fireBeforeProcessSignaled(type, event, this, processRuntime);
if ("timerTriggered".equals(type)) {
TimerInstance timer = (TimerInstance) event;
if (timer.getId().equals(slaTimerId)) {
handleSLAViolation();
// no need to pass the event along as it was purely for SLA tracking
return;
}
}
if ("slaViolation".equals(type)) {
handleSLAViolation();
// no need to pass the event along as it was purely for SLA tracking
return;
}
List<NodeInstance> currentView = new ArrayList<>(this.nodeInstances);
try {
this.activatingNodeIds = new ArrayList<>();
List<EventListener> listeners = eventListeners.get(type);
if (listeners != null) {
for (EventListener listener : listeners) {
listener.signalEvent(type, event);
}
}
listeners = externalEventListeners.get(type);
if (listeners != null) {
for (EventListener listener : listeners) {
listener.signalEvent(type, event);
}
}
if (!type.startsWith("Compensation")) {
for (Node node : getWorkflowProcess().getNodes()) {
if (node instanceof EventNodeInterface && ((EventNodeInterface) node).acceptsEvent(type, event, getResolver(node, currentView))) {
if (node instanceof EventNode && ((EventNode) node).getFrom() == null) {
EventNodeInstance eventNodeInstance = (EventNodeInstance) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
} else {
if (node instanceof EventSubProcessNode && (resolveVariables(((EventSubProcessNode) node).getEvents()).contains(type))) {
EventSubProcessNodeInstance eventNodeInstance = (EventSubProcessNodeInstance) getNodeInstance(node);
eventNodeInstance.signalEvent(type, event);
} else {
List<NodeInstance> nodeInstances = getNodeInstances(node.getId(), currentView);
if (nodeInstances != null && !nodeInstances.isEmpty()) {
for (NodeInstance nodeInstance : nodeInstances) {
((EventNodeInstanceInterface) nodeInstance).signalEvent(type, event);
}
}
}
}
} else if (node instanceof StartNode && ((StartNode) node).getTriggers() != null) {
boolean accepted = ((StartNode) node).getTriggers().stream().filter(EventTrigger.class::isInstance).anyMatch(t -> ((EventTrigger) t).getEventFilters().stream().anyMatch(e -> e.acceptsEvent(type, event)));
if (accepted && node.getMetaData().get("acceptStartSignal") != null) {
StartNodeInstance startNodeInstance = (StartNodeInstance) getNodeInstance(node);
startNodeInstance.signalEvent(type, event);
}
}
}
if (((io.automatiko.engine.workflow.process.core.WorkflowProcess) getWorkflowProcess()).isDynamic()) {
for (Node node : getWorkflowProcess().getNodes()) {
if (node.hasMatchingEventListner(type) && node.getIncomingConnections().isEmpty()) {
NodeInstance nodeInstance = getNodeInstance(node);
if (nodeInstance != null) {
if (event != null) {
Map<String, Object> dynamicParams = new HashMap<>(getVariables());
if (event instanceof Map) {
dynamicParams.putAll((Map<String, Object>) event);
} else if (event instanceof WorkflowProcessInstance) {
// ignore variables of process instance type
} else {
dynamicParams.put("Data", event);
}
nodeInstance.setDynamicParameters(dynamicParams);
}
nodeInstance.trigger(null, io.automatiko.engine.workflow.process.core.Node.CONNECTION_DEFAULT_TYPE);
}
} else if (this instanceof ExecutableProcessInstance && node instanceof CompositeNode) {
Optional<NodeInstance> instance = this.nodeInstances.stream().filter(ni -> ni.getNodeId() == node.getId()).findFirst();
instance.ifPresent(n -> ((CompositeNodeInstance) n).signalEvent(type, event));
}
}
}
}
} finally {
processRuntime.getProcessEventSupport().fireAfterProcessSignaled(type, event, this, processRuntime);
if (this.activatingNodeIds != null) {
this.activatingNodeIds.clear();
this.activatingNodeIds = null;
}
}
}
}
Aggregations