Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
mtitek-process-core-runtime-default
|
ProcessRuntimeDefault.java
References
ProcessRuntimeDefault.java
References
See this page for more information on how you can build RuntimeManager and get RuntimeEngine:
http://docs.jboss.org/jbpm/release/latest/jbpm-docs/html_single/#_usage
See this unit-test java class for examples how to start a new ProcessInstance or resume an existing one:
https://github.com/droolsjbpm/droolsjbpm-integration/blob/master/kie-spring/src/test/java/org/kie/spring/jbpm/PerProcessInstanceSpringTest.java
ProcessRuntimeDefault.java
package mtitek.process.runtime; import java.util.EventListener; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; import java.util.Optional; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.jbpm.process.audit.AuditLogService; import org.jbpm.process.audit.NodeInstanceLog; import org.kie.api.event.process.ProcessEventListener; import org.kie.api.event.rule.AgendaEventListener; import org.kie.api.event.rule.RuleRuntimeEventListener; import org.kie.api.runtime.KieSession; import org.kie.api.runtime.manager.RuntimeEngine; import org.kie.api.runtime.manager.RuntimeManager; import org.kie.api.runtime.process.ProcessInstance; import org.kie.api.runtime.process.WorkItemHandler; import org.kie.internal.process.CorrelationAwareProcessRuntime; import org.kie.internal.process.CorrelationKey; import org.kie.internal.runtime.manager.context.ProcessInstanceIdContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import mtitek.process.runtime.exception.ProcessRuntimeException; import mtitek.process.runtime.utils.ProcessRuntimeConstants; import mtitek.process.runtime.utils.ProcessRuntimeUtils; @Transactional public class ProcessRuntimeDefault implements ProcessRuntime { private static final Logger LOG = LoggerFactory.getLogger(ProcessRuntimeDefault.class); @PersistenceContext(unitName = "org.jbpm.persistence.jpa") private EntityManager entityManager; private RuntimeManager runtimeManager; private String processDefinitionId; private Map<String, WorkItemHandler> workItemsHandlers; private List<EventListener> eventsListeners; private AuditLogService auditLogService; public EntityManager getEntityManager() { return entityManager; } public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } public RuntimeManager getRuntimeManager() { return runtimeManager; } public void setRuntimeManager(final RuntimeManager runtimeManager) { this.runtimeManager = runtimeManager; } public String getProcessDefinitionId() { return processDefinitionId; } public void setProcessDefinitionId(String processDefinitionId) { this.processDefinitionId = processDefinitionId; } public Map<String, WorkItemHandler> getWorkItemsHandlers() { return workItemsHandlers; } public void setWorkItemsHandlers(final Map<String, WorkItemHandler> workItemsHandlers) { this.workItemsHandlers = workItemsHandlers; } public List<EventListener> getEventsListeners() { return eventsListeners; } public void setEventsListeners(List<EventListener> eventsListeners) { this.eventsListeners = eventsListeners; } public AuditLogService getAuditLogService() { return auditLogService; } public void setAuditLogService(final AuditLogService auditLogService) { this.auditLogService = auditLogService; } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = ProcessRuntimeException.class) @Override public ProcessInstance startProcess(final Object payload, final Map<String, String> payloadParameters) throws ProcessRuntimeException { ProcessRuntimeDefault.LOG.trace("startProcess: parameters={}", payloadParameters); try { final ProcessRuntimePayload processRuntimePayload = new ProcessRuntimePayload(UUID.randomUUID().toString(), payload, payloadParameters); final Map<String, Object> params = new ConcurrentHashMap<>(); params.put(ProcessRuntimeConstants.PROCESS_RUNTIME_PAYLOAD_VAR_IN, processRuntimePayload); final RuntimeEngine runtimeEngine = runtimeManager.getRuntimeEngine(ProcessInstanceIdContext.get()); final KieSession kieSession = runtimeEngine.getKieSession(); ProcessRuntimeDefault.LOG.trace("KieSession identifier='{}'", kieSession.getIdentifier()); registerWorkItemHandlers(kieSession); addEventListeners(kieSession); final ProcessInstance processInstance = kieSession.startProcess(processDefinitionId, params); ProcessRuntimeDefault.LOG.trace("processInstance: {}", processInstance); if (processInstance != null) { ProcessRuntimeDefault.LOG.trace("processInstance: id='{}', processDefinitionId='{}', state='{}']", processInstance.getId(), processInstance.getProcessId(), processInstance.getState()); } return processInstance; } catch (final Exception e) { throw new ProcessRuntimeException(e); } } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = ProcessRuntimeException.class) @Override public ProcessInstance startProcess(final String payloadId, final Object payload, final Map<String, String> payloadParameters) throws ProcessRuntimeException { Objects.requireNonNull(payloadId, "The payloadId cannot be null!"); ProcessRuntimeDefault.LOG.trace("startProcess: payloadId='{}', parameters={}", payloadId, payloadParameters); try { final ProcessRuntimePayload processRuntimePayload = new ProcessRuntimePayload(payloadId, payload, payloadParameters); final Map<String, Object> params = new ConcurrentHashMap<>(); params.put(ProcessRuntimeConstants.PROCESS_RUNTIME_PAYLOAD_VAR_IN, processRuntimePayload); final RuntimeEngine runtimeEngine = runtimeManager.getRuntimeEngine(ProcessInstanceIdContext.get()); final KieSession kieSession = runtimeEngine.getKieSession(); ProcessRuntimeDefault.LOG.trace("KieSession identifier='{}'", kieSession.getIdentifier()); registerWorkItemHandlers(kieSession); addEventListeners(kieSession); final CorrelationKey correlationKey = ProcessRuntimeUtils.getCorrelationKey(payloadId); final ProcessInstance processInstance = ((CorrelationAwareProcessRuntime) kieSession) .startProcess(processDefinitionId, correlationKey, params); ProcessRuntimeDefault.LOG.trace("processInstance: {}", processInstance); if (processInstance != null) { ProcessRuntimeDefault.LOG.trace("processInstance: id='{}', processDefinitionId='{}', state='{}']", processInstance.getId(), processInstance.getProcessId(), processInstance.getState()); } return processInstance; } catch (final Exception e) { throw new ProcessRuntimeException(e); } } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = ProcessRuntimeException.class) @Override public ProcessInstance resumeProcess(final Long processInstanceId, final Map<String, String> workItemParameters) throws ProcessRuntimeException { Objects.requireNonNull(processInstanceId, "The processInstanceId cannot be null!"); ProcessRuntimeDefault.LOG.trace("resumeProcess: processInstanceId='{}', parameters={}", processInstanceId, workItemParameters); try { final Optional<Long> optionalWorkItemId = ProcessRuntimeUtils.getWorkItemId(entityManager, processInstanceId); if (!optionalWorkItemId.isPresent()) { throw new ProcessRuntimeException( "WorkItem not found for the processInstance '" + processInstanceId + "'"); } final Long workItemId = optionalWorkItemId.get(); ProcessRuntimeDefault.LOG.trace("WorkItem [id: {}]", workItemId); final RuntimeEngine runtimeEngine = runtimeManager .getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId)); final KieSession kieSession = runtimeEngine.getKieSession(); ProcessRuntimeDefault.LOG.trace("KieSession identifier='{}'", kieSession.getIdentifier()); registerWorkItemHandlers(kieSession); addEventListeners(kieSession); final Map<String, Object> params = new ConcurrentHashMap<>(); if (workItemParameters != null && !workItemParameters.isEmpty()) { for (Entry<String, String> entry : workItemParameters.entrySet()) { final String key = entry.getKey(); final String value = entry.getValue(); if (value != null) { params.put(key, value); } } } kieSession.getWorkItemManager().completeWorkItem(workItemId, params); final ProcessInstance processInstance = kieSession.getProcessInstance(processInstanceId); ProcessRuntimeDefault.LOG.trace("processInstance: {}", processInstance); if (processInstance != null) { ProcessRuntimeDefault.LOG.trace("processInstance: id='{}', processDefinitionId='{}', state='{}']", processInstance.getId(), processInstance.getProcessId(), processInstance.getState()); } return processInstance; } catch (final Exception e) { throw new ProcessRuntimeException(e); } } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = ProcessRuntimeException.class) @Override public ProcessInstance resumeProcess(final String payloadId, final Map<String, String> workItemParameters) throws ProcessRuntimeException { Objects.requireNonNull(payloadId, "The payloadId cannot be null!"); ProcessRuntimeDefault.LOG.trace("resumeProcess: payloadId='{}', parameters={}", payloadId, workItemParameters); try { final Long processInstanceId = ProcessRuntimeUtils.getProcessInstanceId(entityManager, payloadId); final Optional<Long> optionalWorkItemId = ProcessRuntimeUtils.getWorkItemId(entityManager, processInstanceId); if (!optionalWorkItemId.isPresent()) { throw new ProcessRuntimeException( "WorkItem not found for the processInstance '" + processInstanceId + "'"); } final Long workItemId = optionalWorkItemId.get(); ProcessRuntimeDefault.LOG.trace("WorkItem [id: {}]", workItemId); final RuntimeEngine runtimeEngine = runtimeManager .getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId)); final KieSession kieSession = runtimeEngine.getKieSession(); ProcessRuntimeDefault.LOG.trace("KieSession identifier='{}'", kieSession.getIdentifier()); registerWorkItemHandlers(kieSession); addEventListeners(kieSession); final Map<String, Object> params = new ConcurrentHashMap<>(); if (workItemParameters != null && !workItemParameters.isEmpty()) { for (Entry<String, String> entry : workItemParameters.entrySet()) { final String key = entry.getKey(); final String value = entry.getValue(); if (value != null) { params.put(key, value); } } } kieSession.getWorkItemManager().completeWorkItem(workItemId, params); final ProcessInstance processInstance = kieSession.getProcessInstance(processInstanceId); ProcessRuntimeDefault.LOG.trace("processInstance: {}", processInstance); if (processInstance != null) { ProcessRuntimeDefault.LOG.trace("processInstance: id='{}', processDefinitionId='{}', state='{}']", processInstance.getId(), processInstance.getProcessId(), processInstance.getState()); } return processInstance; } catch (final Exception e) { throw new ProcessRuntimeException(e); } } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = ProcessRuntimeException.class) @Override public ProcessInstance cancelProcess(final Long processInstanceId) throws ProcessRuntimeException { Objects.requireNonNull(processInstanceId, "The processInstanceId cannot be null!"); ProcessRuntimeDefault.LOG.trace("cancelProcess: processInstanceId='{}'", processInstanceId); try { final RuntimeEngine runtimeEngine = runtimeManager .getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId)); final KieSession kieSession = runtimeEngine.getKieSession(); ProcessRuntimeDefault.LOG.trace("KieSession identifier='{}'", kieSession.getIdentifier()); registerWorkItemHandlers(kieSession); addEventListeners(kieSession); kieSession.abortProcessInstance(processInstanceId); final ProcessInstance processInstance = kieSession.getProcessInstance(processInstanceId); ProcessRuntimeDefault.LOG.trace("processInstance: {}", processInstance); if (processInstance != null) { ProcessRuntimeDefault.LOG.trace("processInstance: id='{}', processDefinitionId='{}', state='{}']", processInstance.getId(), processInstance.getProcessId(), processInstance.getState()); } return processInstance; } catch (final Exception e) { throw new ProcessRuntimeException(e); } } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = ProcessRuntimeException.class) @Override public ProcessInstance cancelProcess(final String payloadId) throws ProcessRuntimeException { Objects.requireNonNull(payloadId, "The payloadId cannot be null!"); ProcessRuntimeDefault.LOG.trace("cancelProcess: payloadId='{}'", payloadId); try { final Long processInstanceId = ProcessRuntimeUtils.getProcessInstanceId(entityManager, payloadId); final RuntimeEngine runtimeEngine = runtimeManager .getRuntimeEngine(ProcessInstanceIdContext.get(processInstanceId)); final KieSession kieSession = runtimeEngine.getKieSession(); ProcessRuntimeDefault.LOG.trace("KieSession identifier='{}'", kieSession.getIdentifier()); registerWorkItemHandlers(kieSession); addEventListeners(kieSession); kieSession.abortProcessInstance(processInstanceId); final ProcessInstance processInstance = kieSession.getProcessInstance(processInstanceId); ProcessRuntimeDefault.LOG.trace("processInstance: {}", processInstance); if (processInstance != null) { ProcessRuntimeDefault.LOG.trace("processInstance: id='{}', processDefinitionId='{}', state='{}']", processInstance.getId(), processInstance.getProcessId(), processInstance.getState()); } return processInstance; } catch (final Exception e) { throw new ProcessRuntimeException(e); } } @Transactional(propagation = Propagation.REQUIRED, rollbackFor = ProcessRuntimeException.class) @Override public void clearAllAuditHistory() throws ProcessRuntimeException { ProcessRuntimeDefault.LOG.trace("clearAllAuditHistory ..."); try { auditLogService.clear(); } catch (final Exception e) { throw new ProcessRuntimeException(e); } } @Transactional(readOnly = true) @Override public List<? extends NodeInstanceLog> findNodeInstances(final Long processInstanceId) throws ProcessRuntimeException { ProcessRuntimeDefault.LOG.trace("findNodeInstances [processInstanceId: {}]", processInstanceId); try { final List<? extends NodeInstanceLog> nodeInstanceLogs = auditLogService .findNodeInstances(processInstanceId); return nodeInstanceLogs; } catch (final Exception e) { throw new ProcessRuntimeException(e); } } private void registerWorkItemHandlers(final KieSession kieSession) { if (workItemsHandlers != null) { for (final Map.Entry<String, WorkItemHandler> entry : workItemsHandlers.entrySet()) { kieSession.getWorkItemManager().registerWorkItemHandler(entry.getKey(), entry.getValue()); } } } private void addEventListeners(final KieSession kieSession) { if (eventsListeners != null) { for (final EventListener eventListener : eventsListeners) { if (eventListener instanceof RuleRuntimeEventListener) { kieSession.addEventListener((RuleRuntimeEventListener) eventListener); } else if (eventListener instanceof ProcessEventListener) { kieSession.addEventListener((ProcessEventListener) eventListener); } else if (eventListener instanceof AgendaEventListener) { kieSession.addEventListener((AgendaEventListener) eventListener); } } } } }
© 2010-2022
mti
tek