Search in sources :

Example 1 with TimerInstance

use of org.kie.kogito.timer.TimerInstance in project kogito-runtimes by kiegroup.

the class WorkflowProcessInstanceImpl method configureSLA.

@Override
public void configureSLA() {
    String slaDueDateExpression = (String) getProcess().getMetaData().get(CUSTOM_SLA_DUE_DATE);
    if (slaDueDateExpression != null) {
        TimerInstance timer = configureSLATimer(slaDueDateExpression);
        if (timer != null) {
            this.slaTimerId = timer.getId();
            this.slaDueDate = new Date(System.currentTimeMillis() + timer.getDelay());
            this.slaCompliance = KogitoProcessInstance.SLA_PENDING;
            logger.debug("SLA for process instance {} is PENDING with due date {}", this.getStringId(), this.slaDueDate);
        }
    }
}
Also used : TimerInstance(org.kie.kogito.timer.TimerInstance) Date(java.util.Date)

Example 2 with TimerInstance

use of org.kie.kogito.timer.TimerInstance in project kogito-runtimes by kiegroup.

the class EventNodeInstance method configureSla.

protected void configureSla() {
    String slaDueDateExpression = (String) getNode().getMetaData().get("customSLADueDate");
    if (slaDueDateExpression != null) {
        TimerInstance timer = ((WorkflowProcessInstanceImpl) getProcessInstance()).configureSLATimer(slaDueDateExpression);
        if (timer != null) {
            this.slaTimerId = timer.getId();
            this.slaDueDate = new Date(System.currentTimeMillis() + timer.getDelay());
            this.slaCompliance = ProcessInstance.SLA_PENDING;
            logger.debug("SLA for node instance {} is PENDING with due date {}", this.getStringId(), this.slaDueDate);
        }
    }
}
Also used : TimerInstance(org.kie.kogito.timer.TimerInstance) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) Date(java.util.Date)

Example 3 with TimerInstance

use of org.kie.kogito.timer.TimerInstance in project kogito-runtimes by kiegroup.

the class StateBasedNodeInstance method configureSla.

@Override
protected void configureSla() {
    String slaDueDateExpression = (String) getNode().getMetaData().get("customSLADueDate");
    if (slaDueDateExpression != null) {
        TimerInstance timer = ((WorkflowProcessInstanceImpl) getProcessInstance()).configureSLATimer(slaDueDateExpression);
        if (timer != null) {
            this.slaTimerId = timer.getId();
            this.slaDueDate = new Date(System.currentTimeMillis() + timer.getDelay());
            this.slaCompliance = KogitoProcessInstance.SLA_PENDING;
            logger.debug("SLA for node instance {} is PENDING with due date {}", this.getStringId(), this.slaDueDate);
            addTimerListener();
        }
    }
}
Also used : TimerInstance(org.kie.kogito.timer.TimerInstance) WorkflowProcessInstanceImpl(org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl) Date(java.util.Date)

Example 4 with TimerInstance

use of org.kie.kogito.timer.TimerInstance in project kogito-runtimes by kiegroup.

the class TimerTest method testTimer.

@Test
@Disabled
public void testTimer() {
    KogitoProcessRuntime kruntime = createKogitoProcessRuntime();
    RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance() {

        private static final long serialVersionUID = 510l;

        public void signalEvent(String type, Object event) {
            if ("timerTriggered".equals(type)) {
                TimerInstance timer = (TimerInstance) event;
                logger.info("Timer {} triggered", timer.getId());
                counter++;
            }
        }
    };
    processInstance.setKnowledgeRuntime(((InternalWorkingMemory) kruntime.getKieSession()).getKnowledgeRuntime());
    processInstance.setId("1234");
    InternalProcessRuntime processRuntime = ((InternalProcessRuntime) ((InternalWorkingMemory) kruntime.getKieSession()).getProcessRuntime());
    processRuntime.getProcessInstanceManager().internalAddProcessInstance(processInstance);
    new Thread(() -> kruntime.getKieSession().fireUntilHalt()).start();
    JobsService jobService = new LegacyInMemoryJobService(kruntime, new DefaultUnitOfWorkManager(new CollectingUnitOfWorkFactory()));
    ProcessInstanceJobDescription desc = ProcessInstanceJobDescription.of(ExactExpirationTime.now(), processInstance.getStringId(), "test");
    String jobId = jobService.scheduleProcessInstanceJob(desc);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(1, counter);
    counter = 0;
    desc = ProcessInstanceJobDescription.of(DurationExpirationTime.after(500), processInstance.getStringId(), "test");
    jobId = jobService.scheduleProcessInstanceJob(desc);
    assertEquals(0, counter);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(1, counter);
    counter = 0;
    desc = ProcessInstanceJobDescription.of(DurationExpirationTime.repeat(500, 300L), processInstance.getStringId(), "test");
    jobId = jobService.scheduleProcessInstanceJob(desc);
    assertEquals(0, counter);
    try {
        Thread.sleep(700);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(1, counter);
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    // we can't know exactly how many times this will fire as timers are not precise, but should be at least 4
    assertTrue(counter >= 4);
    jobService.cancelJob(jobId);
    int lastCount = counter;
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    // do nothing
    }
    assertEquals(lastCount, counter);
}
Also used : RuleFlowProcessInstance(org.jbpm.ruleflow.instance.RuleFlowProcessInstance) TimerInstance(org.kie.kogito.timer.TimerInstance) KogitoProcessRuntime(org.kie.kogito.internal.process.runtime.KogitoProcessRuntime) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) JobsService(org.kie.kogito.jobs.JobsService) CollectingUnitOfWorkFactory(org.kie.kogito.services.uow.CollectingUnitOfWorkFactory) ProcessInstanceJobDescription(org.kie.kogito.jobs.ProcessInstanceJobDescription) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) DefaultUnitOfWorkManager(org.kie.kogito.services.uow.DefaultUnitOfWorkManager) LegacyInMemoryJobService(org.kie.services.jobs.impl.LegacyInMemoryJobService) Test(org.junit.jupiter.api.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Disabled(org.junit.jupiter.api.Disabled)

Example 5 with TimerInstance

use of org.kie.kogito.timer.TimerInstance in project kogito-runtimes by kiegroup.

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, getStringId());
    synchronized (this) {
        if (getState() != KogitoProcessInstance.STATE_ACTIVE) {
            return;
        }
        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<KogitoEventListener> listeners = eventListeners.get(type);
            if (listeners != null) {
                for (KogitoEventListener listener : listeners) {
                    listener.signalEvent(type, event);
                }
            }
            listeners = externalEventListeners.get(type);
            if (listeners != null) {
                for (KogitoEventListener listener : listeners) {
                    listener.signalEvent(type, event);
                }
            }
            for (org.kie.api.definition.process.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, getResolver(node, currentView));
                    } 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, getResolver(node, currentView));
                                }
                            }
                        }
                    }
                }
            }
            if (((org.jbpm.workflow.core.WorkflowProcess) getWorkflowProcess()).isDynamic()) {
                for (org.kie.api.definition.process.Node node : getWorkflowProcess().getNodes()) {
                    if (type.equals(node.getName()) && node.getIncomingConnections().isEmpty()) {
                        NodeInstance nodeInstance = getNodeInstance(node);
                        if (event != null) {
                            Map<String, Object> dynamicParams = new HashMap<>(getVariables());
                            if (event instanceof Map) {
                                dynamicParams.putAll((Map<String, Object>) event);
                            } else {
                                dynamicParams.put("Data", event);
                            }
                            nodeInstance.setDynamicParameters(dynamicParams);
                        }
                        nodeInstance.trigger(null, Node.CONNECTION_DEFAULT_TYPE);
                    } else if (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 {
            if (this.activatingNodeIds != null) {
                this.activatingNodeIds.clear();
                this.activatingNodeIds = null;
            }
        }
    }
}
Also used : Milestone(org.kie.kogito.process.flexible.Milestone) ProcessInstanceJobDescription(org.kie.kogito.jobs.ProcessInstanceJobDescription) Date(java.util.Date) EventNodeInstanceInterface(org.jbpm.workflow.instance.node.EventNodeInstanceInterface) EventBasedNodeInstanceInterface(org.jbpm.workflow.instance.node.EventBasedNodeInstanceInterface) LoggerFactory(org.slf4j.LoggerFactory) BoundaryEventNode(org.jbpm.workflow.core.node.BoundaryEventNode) DynamicNode(org.jbpm.workflow.core.node.DynamicNode) StartNode(org.jbpm.workflow.core.node.StartNode) KogitoProcessInstance(org.kie.kogito.internal.process.runtime.KogitoProcessInstance) PatternConstants(org.jbpm.util.PatternConstants) NamedDataType(org.kie.kogito.process.NamedDataType) NodeImpl(org.jbpm.workflow.core.impl.NodeImpl) CompositeNode(org.jbpm.workflow.core.node.CompositeNode) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) WorkflowProcessInstance(org.jbpm.workflow.instance.WorkflowProcessInstance) KogitoNodeInstanceContainer(org.kie.kogito.internal.process.runtime.KogitoNodeInstanceContainer) StateNode(org.jbpm.workflow.core.node.StateNode) Matcher(java.util.regex.Matcher) EventNodeInterface(org.jbpm.workflow.core.node.EventNodeInterface) ContextInstance(org.jbpm.process.instance.ContextInstance) CORRELATION_KEY(org.jbpm.ruleflow.core.Metadata.CORRELATION_KEY) Map(java.util.Map) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) UUID(java.util.UUID) KogitoNodeInstance(org.kie.kogito.internal.process.runtime.KogitoNodeInstance) DurationExpirationTime(org.kie.kogito.jobs.DurationExpirationTime) VariableScope(org.jbpm.process.core.context.variable.VariableScope) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) IS_FOR_COMPENSATION(org.jbpm.ruleflow.core.Metadata.IS_FOR_COMPENSATION) List(java.util.List) Stream(java.util.stream.Stream) ContextContainer(org.jbpm.process.core.ContextContainer) Node(org.jbpm.workflow.core.Node) COMPENSATION(org.jbpm.ruleflow.core.Metadata.COMPENSATION) BusinessCalendar(org.jbpm.process.core.timer.BusinessCalendar) MilestoneNode(org.jbpm.workflow.core.node.MilestoneNode) ACTIVE(org.kie.kogito.process.flexible.ItemDescription.Status.ACTIVE) Optional(java.util.Optional) COMPLETED(org.kie.kogito.process.flexible.ItemDescription.Status.COMPLETED) NodeContainer(org.kie.api.definition.process.NodeContainer) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) DateTimeUtils(org.jbpm.process.core.timer.DateTimeUtils) EndNodeInstance(org.jbpm.workflow.instance.node.EndNodeInstance) AdHocFragment(org.kie.kogito.process.flexible.AdHocFragment) HashMap(java.util.HashMap) Function(java.util.function.Function) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) ArrayList(java.util.ArrayList) Timer(org.jbpm.process.core.timer.Timer) NodeInstance(org.jbpm.workflow.instance.NodeInstance) FaultNodeInstance(org.jbpm.workflow.instance.node.FaultNodeInstance) CUSTOM_SLA_DUE_DATE(org.jbpm.ruleflow.core.Metadata.CUSTOM_SLA_DUE_DATE) CONDITION(org.jbpm.ruleflow.core.Metadata.CONDITION) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) DroolsAction(org.jbpm.workflow.core.DroolsAction) LinkedHashSet(java.util.LinkedHashSet) MVELEvaluator(org.drools.mvel.util.MVELEvaluator) Variable(org.jbpm.process.core.context.variable.Variable) AVAILABLE(org.kie.kogito.process.flexible.ItemDescription.Status.AVAILABLE) UNIQUE_ID(org.jbpm.ruleflow.core.Metadata.UNIQUE_ID) InternalKnowledgeRuntime(org.drools.core.common.InternalKnowledgeRuntime) Logger(org.slf4j.Logger) CorrelationKey(org.kie.internal.process.CorrelationKey) Metadata(org.jbpm.ruleflow.core.Metadata) VariableResolverFactory(org.mvel2.integration.VariableResolverFactory) EventNode(org.jbpm.workflow.core.node.EventNode) BaseEventDescription(org.kie.kogito.process.BaseEventDescription) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) AgendaFilter(org.kie.api.runtime.rule.AgendaFilter) ProcessInstanceImpl(org.jbpm.process.instance.impl.ProcessInstanceImpl) EVENT_TYPE_SIGNAL(org.jbpm.ruleflow.core.Metadata.EVENT_TYPE_SIGNAL) TimerInstance(org.kie.kogito.timer.TimerInstance) InternalProcessRuntime(org.jbpm.process.instance.InternalProcessRuntime) TimerJobId(org.kie.kogito.jobs.TimerJobId) KogitoEventListener(org.kie.kogito.internal.process.event.KogitoEventListener) ItemDescription(org.kie.kogito.process.flexible.ItemDescription) EventDescription(org.kie.kogito.process.EventDescription) KogitoWorkflowProcess(org.kie.kogito.internal.process.runtime.KogitoWorkflowProcess) Collections(java.util.Collections) EVENT_TYPE(org.jbpm.ruleflow.core.Metadata.EVENT_TYPE) EMPTY_EVENT_LISTENER(org.jbpm.workflow.instance.impl.DummyEventListener.EMPTY_EVENT_LISTENER) TimerInstance(org.kie.kogito.timer.TimerInstance) HashMap(java.util.HashMap) EventSubProcessNode(org.jbpm.workflow.core.node.EventSubProcessNode) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) KogitoEventListener(org.kie.kogito.internal.process.event.KogitoEventListener) BoundaryEventNode(org.jbpm.workflow.core.node.BoundaryEventNode) EventNode(org.jbpm.workflow.core.node.EventNode) EventNodeInstanceInterface(org.jbpm.workflow.instance.node.EventNodeInstanceInterface) KogitoWorkflowProcess(org.kie.kogito.internal.process.runtime.KogitoWorkflowProcess) Optional(java.util.Optional) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) EventNodeInterface(org.jbpm.workflow.core.node.EventNodeInterface) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) CompositeNode(org.jbpm.workflow.core.node.CompositeNode) EventSubProcessNodeInstance(org.jbpm.workflow.instance.node.EventSubProcessNodeInstance) KogitoNodeInstance(org.kie.kogito.internal.process.runtime.KogitoNodeInstance) StateBasedNodeInstance(org.jbpm.workflow.instance.node.StateBasedNodeInstance) EndNodeInstance(org.jbpm.workflow.instance.node.EndNodeInstance) NodeInstance(org.jbpm.workflow.instance.NodeInstance) FaultNodeInstance(org.jbpm.workflow.instance.node.FaultNodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) CompositeNodeInstance(org.jbpm.workflow.instance.node.CompositeNodeInstance) EventNodeInstance(org.jbpm.workflow.instance.node.EventNodeInstance) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

TimerInstance (org.kie.kogito.timer.TimerInstance)6 Date (java.util.Date)4 ProcessInstanceJobDescription (org.kie.kogito.jobs.ProcessInstanceJobDescription)3 InternalKnowledgeRuntime (org.drools.core.common.InternalKnowledgeRuntime)2 BusinessCalendar (org.jbpm.process.core.timer.BusinessCalendar)2 InternalProcessRuntime (org.jbpm.process.instance.InternalProcessRuntime)2 TimerJobId (org.kie.kogito.jobs.TimerJobId)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 UUID (java.util.UUID)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Function (java.util.function.Function)1